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: I/O stream for reading and editing a file full of numbers

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default I/O stream for reading and editing a file full of numbers

    I have a file with a lot of numbers in it and I want to read the numbers from the file one by one, test them and then either leave them or delete them from the file. What I/O stream would be good for that? It would need to be able to remove the number without leaving whitespace, for example from 1 2 3 4 5 to 1 3 5.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I/O stream for reading and editing a file full of numbers

    Read the file and parse as desired into a StringBuilder/Buffer, then over-write the file with the new data. You could wrap a FileInputStream and FileOutputStream in a BufferedReader/BufferedWriter to do the reading/writing, respectively.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: I/O stream for reading and editing a file full of numbers

    Thanks, but that's not quite what I want. I intend for the file to have a few million numbers in it so reading and overwriting everything every time would take a bit long. What I'm looking for should be able to read a number, mark the position of the first digit and then erase the bit of file from that position to the end of the number (like write an empty string for every digit in the number or something).
    Is something like this possible?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I/O stream for reading and editing a file full of numbers

    I intend for the file to have a few million numbers in it so reading and overwriting everything every time would take a bit long.
    Not necessarily. Did you try it? You can add a slight twist to the method I mentioned if you're files are huge and you worry about reading it all into memory...as you read the first file, parse it line by line, and write these lines out to a temp file. When complete, delete the first and rename the second the that of the first.

Similar Threads

  1. Getting audio stream to play from jar file instead of system directory?
    By nivangerow in forum What's Wrong With My Code?
    Replies: 26
    Last Post: October 5th, 2011, 09:51 AM
  2. reading from text file produces extra s p a c e s, even on numbers 1 0 1
    By Spidey1980 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 3rd, 2011, 09:18 PM
  3. problem with File Stream
    By amr in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 11th, 2011, 09:57 PM
  4. Object and text file stream code problmes
    By Kakashi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 04:34 PM
  5. reading stream of bytes from serial port
    By KrisTheSavage in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 3rd, 2011, 11:02 AM