If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Java Help-File Reading & Writing

I am having problem and just need some coding lines that reads a file into an array so it can be edited in the array and than rewrites it to the file later on. Also another question, is there a way to add more to an array with out creating a new one and so forth?

BufferedReader has a readLine method. nio probably doesn't have anything as high-level as that, but it should be capable of offering similar functionality with some additional code (detecting '\n' in a CharBuffer I think). When you can read the file one line at a time, you can index each line in an array or Collection. PrintWriter offers a writeln() method for putting it all back in.

ok ok a bit confused about that whole collection thing and the file read and write. I first need to get it to read the file and add each line into an array and than it runs the rest. When you add some more stuff to the array it adds to entrys to the array so that the length goes from like 6 to 9 and the new ones get filled. Than i want to get it to erase the document and create it again and write the whole array to it so that each part of the array is on a new line. If possible get it to replace the documents content so that it like scans it to see if any changes accured and than only add those changes in the right lines. I think now i am may be confusing some people but if you have a sample program you know of that reads a files than buts it into an array and you can add more strings to the array untill you close the program and so forth if you understand that thank you for understanding it.

Although using java.util.* is perfectly acceptable, it is better to only import exactly what you need, in this case: import java.util.ArrayList. This way you only pull in what you need and not the everything in Collections.

In Java 1.5+, you can also specify the type of data you will be storing in there (autoboxing) like so: ArrrayList foo = new ArrayList<String>(1024); This makes it so that you can only place Strings into the ArrayList, but you also don't have to cast the object to a String when you pull it out. You'll find that by doing that, you will save yourself some typing down the road.

And here's the NIO version. We're just reading in and printing out. If you want "readline" capability, you have to build that in which requires a bit more coding and is slightly more complex. I HIGHLY recommend the standard version for the original poster.