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 4 of 4

Thread: Writing integers to a file

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Writing integers to a file

    Hello,
    I am simply trying to print a random integer to a text file. However when I open up the text it just shows= square shapes and not the numbers. I attached the .txt file

    public void writefile(int amountLines, File filename) throws IOException
        {
        BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
        Random generator = new Random();
     
            for (int i = 0; i < amountLines; i++)
            {
                writer.write( generator.nextInt(amountLines -1) + 1);
     
                writer.newLine();
            }
     
                if (writer != null) 
                {
                    writer.flush();
                    writer.close();
                }
        }
    Attached Files Attached Files


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Writing integers to a file

    The reason for this is that the BufferedWriter has a method called write(int c) which takes an int. If you pass an int into this method it will automatically try to figure out what character that int represents and write that character to the stream instead of actually writing the int itself.

    To write the int to the stream you should do this.

    writer.write([b]String.valueOf([/b]generator.nextInt(amountLines -1) + 1[b])[/b]);

    See the String.valueOf(). This will turn the int into a string and the BufferedWriters write(String str) method will be used instead.

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    dubois.ford (March 18th, 2010)

  4. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Writing integers to a file

    Thanks that solved it. So the BufferedWriter wrtie(int c) method tries to look up the corresponding ASCII value of the int?

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

    Default Re: Writing integers to a file

    technically Unicode, but yes, it's the same idea.

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    dubois.ford (March 18th, 2010)

Similar Threads

  1. Help writing some Scanner code
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 3rd, 2010, 04:27 AM
  2. 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
  3. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 04:14 AM
  4. Replies: 5
    Last Post: May 21st, 2009, 03:45 AM
  5. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 07:26 PM