Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Writing long variables to file

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing long variables to file

    The attach program works if I reduce the countLimit, but if it is above 10 trillion it gives me the following error: cannot find symbol
    symbol : method write(long)
    location: class java.io.FileOutputStream
    output.write(number);

    import java.io.*;
     
    public class primenumbers {
     
        public static void main(String[] args) throws IOException {
            // Create an output stream to the file
            FileOutputStream output = new FileOutputStream("Exercise23_7.dat");
            long countLimit = 10000000000L;
            long count = 0;
            long number = 2;
            while (count <= countLimit) {
             // Assume the number is prime
             boolean isPrime = true;
             for (int divisor = 2; divisor <= number / 2; divisor++){
                 if (number % divisor == 0){
                 isPrime = false;
                 break;
                 }
             }
             if (isPrime){
                //write prime number to a file
                 output.write(number);
                 System.out.println(number);
             }
            number++;
            count++;
            }
         output.close();
         FileInputStream input = new FileInputStream("Exercise23_7.dat");
         int value;
         while((value = input.read()) != -1)
             System.out.print(value + " ");
     
         input.close();
        }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Writing long variables to file

    There is no write(long) method inside of FileOutputStream. Are you trying to write out the string representations of the number, or the actual value? Unicode only allows for 16-bit characters (some new "unicode" formats I think have 32-bits, don't quote me on this), so any value you try to write out that's greater than 0xFFFF will become truncated to 0xFFFF.

    If you actually want to write out the string representation of the number, I would recommend using a PrintStream to wrap around your FileOutputStream.

    PrintStream output = new PrintStream(new FileOutputStream("data.dat"));
    output.print(10000000000L); // write out "10000000000" into the file
    output.close();

    If you do indeed want to write out the value 10000000000, you will need to split the number into 16-bit chunks (so you'll end up with 16 characters in total) and write out each chunk as it's own character (use bitwise and shift operators). There is an issue of endian-ness that you need to be careful of, though.

Similar Threads

  1. Help writing to a text file on a website!
    By straw in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: September 11th, 2011, 11:02 AM
  2. [SOLVED] Writing " to a File
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2010, 05:21 AM
  3. [SOLVED] Writing integers to a file
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 06:18 PM
  4. Writing Output To New File
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 6th, 2010, 07:25 PM
  5. Creating variables from a text file
    By Larz0rz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 4th, 2009, 11:57 PM