Results 1 to 2 of 2
  1. #1
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Trouble writing an Integer to a text file and reading back with scanner and storing

    What I have been trying to do is write an int to a file. Seems easy enough, but there is a catch like everything in life. I have to append it to the eof. Now I have tried RandomAccessFile, Formatter, FileWriter etc... c nothing I seem to do works. I have to be able to write an int because it stores the user value and I need to be able to manipulate it in the future. The scanner class seems to be only good for Strings, so I thought read it in as a String and convert using the parase method. I have been struggling with this problem for quite sometime. I am almost finished with the program and hit this wall. I am new to Java, so I don't know how to really work through this problem. Every time I have had an idea it has been a fail. Here is a small program I was trying to do:
    Java Code:
    File f = new File("C:\\Test\\kkk.txt");  
      FileWriter wf = new FileWriter(f,true);  
     
     
    
    Scanner sr = null;  
    sr = new Scanner(f);  
    String a = "";  
    while(sr.hasNext()){  
    a =   sr.next();  
       
       
    }  
    int b = Integer.parseInt(a);  
    System.out.print(a);  
        }  
    }
    Can someone please give me something of substance to work with. I really need to know how to write int in binary, and able to store it in a variable so I can manipulate with my program. It has to be at the end of the file or a new line, and RandomAccessFile has seek method, which makes this ideal class to use. Is there a way to write an Int using RandomAccessFile or FileWriter as an int and use something like scanner class or another class to read that binary code and store it as a variable as an int. Thank you in advance.

  2. #2
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Trouble writing an Integer to a text file and reading back with scanner and stori

    I really could use some help.

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 03:38 PM
  2. Reading/Writing Integer Bytes
    By AndrewM16921 in forum New To Java
    Replies: 1
    Last Post: 04-04-2011, 12:56 AM
  3. Reading a txt file and then storing it in a 2d array
    By blkshp1990 in forum New To Java
    Replies: 2
    Last Post: 11-05-2009, 01:31 AM
  4. Replies: 6
    Last Post: 12-12-2008, 04:35 PM
  5. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •