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

Thread: APpending a file with an Integer(Question Inside)

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default APpending a file with an Integer(Question Inside)

    Hello,

    I want to fiqure why this simple code dose not work. I want to append data to an existing file but it has to be a number not a String.

       File f = new File("C:\\Test\\kkk.txt");
      FileWriter wf = new FileWriter(f,true);
     BufferedWriter br = new BufferedWriter(wf);
     int x = 50;
    br.write(x);
    br.close();
        }
        }

    I am still trying to learn so if it's a simple mistake I'm sorry. The following code writes a random number to a text file. I am making an educated guess that it's reading it as binary and in some shape form or fashion 50 equals 2 or something in binary. How do I get to just to write 50 at the end of the file. I know I can use the formatter class but that doesn't have a method for appending my data.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: APpending a file with an Integer(Question Inside)

    Note that you are using a FileWriter and a BufferedWriter, and thus anything written to this file with any type of Writer *must* be a String. Also the file is a text file, so you really have no choice -- you *must* write a String and only a String to this file. To not do so makes no sense at all.

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: APpending a file with an Integer(Question Inside)

    Okay, I understand what you're saying. Is it at all possible,to convert my number to a string and insert it in the file. After that read it back in as a String and convert it as a integer, so then I can treat it as number. I have tried this using the scanner method and it throws me an exception. Please respond back.

    Thanks

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: APpending a file with an Integer(Question Inside)

    Yes, ints can be easily changed to String via the String.valueOf(...) method. And int Strings can be read in with a Scanner, if done properly, or as a String and then converted to int via Integer.parseInt(...).

  5. #5
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: APpending a file with an Integer(Question Inside)

    This is what I got i tried but i still having trouble
        File f = new File("C:\\Test\\kkk.txt");
      FileWriter wf = new FileWriter(f,true);
      FileOutputStream fos = new FileOutputStream(f);
     DataOutputStream dos = new DataOutputStream(fos);
     dos.writeUTF(50);
    Scanner sr = null;
    sr = new Scanner(f);
    String a = "";
    while(sr.hasNext()){
    a =   sr.next();
     
     
    }
    int b = Integer.parseInt(a);
    System.out.print(a);
        }
        }
    It throws a number format exception

    What am I doing wrong here.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: APpending a file with an Integer(Question Inside)

    If it throws a NumberFormatException, then you're trying to parse something that's not a number into a number. Solution: don't do that. Only parse a number String.

  7. #7
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: APpending a file with an Integer(Question Inside)

    Then I am confused on how to proceed. How can I store " dos.writeUTF(50);" and change it to a int?

Similar Threads

  1. Run a jar file inside a batch file with Windows 7 Task schduler
    By kingnachi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2012, 09:20 AM
  2. Not able to put some files inside Zip file
    By chinnu in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 23rd, 2011, 05:17 AM
  3. [SOLVED] Pointing to file inside project
    By M3ss1ah in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 9th, 2011, 07:18 AM
  4. how to read file name inside .gz file....
    By kathir0301 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 6th, 2010, 09:06 AM
  5. [SOLVED] Why can't I write to file inside a doGet() method?
    By FailMouse in forum Java Servlet
    Replies: 1
    Last Post: July 7th, 2010, 01:15 AM