![]() |
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?
|
|
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. :confused:
|
Lists, Maps, etc. are all Collections that can be iterated over and can be expanded to fit your data.
Read this for more details: Chapter 11 of Thinking in Java, 3rd Edition It's long so I recommend saving it rather than viewing it on my server which will only be online for about 3 more hours today. |
ok bit confused still but not as much tell me how i would create a container add a string and get just that one string. Should just be 3 lines ok?
|
ok i looked it over and here is what i understand so far and i put together a example
List test = new ArrayList(); test.add("bob"); System.out.println("" + test.get(0)); but there errors are line one with the word List and ArrayList() help? |
For the sake of coming closer to what you'll probably want to use:
Code:
import java.util.*; |
I figured it out!
Needed import java.util.*; and i didn't know about it so thanks |
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. |
Quote:
Ok, here's the standard IO version Code:
import java.io.File;Code:
import java.nio.ByteBuffer; |
Read and write from and to a file in Java
/* For those who need this code to read from a file and write to a file */
/* This is a better and simpler approach to read and write file in java*/ import java.io.*; class Filetransfer {public static void main (String args[]) {try { FileReader fr = new FileReader("inputfile.txt"); // use any filename BufferedReader br = new BufferedReader(fr); int inputline; FileWriter fw = new FileWriter("outputfile.txt"); //use any filename while((inputline =br.read()) !=-1) // check to see if the file has ended { fw.flush(); fw.write(inputline); } fr.close(); fw.close(); }catch (IOException e){}; /* Program by Sumish Darak*/ } } |
I am trying to open a file where a file name is having spaces.
i am trying to do it by file= new File(path) but it is giving me an exception . illegalArgumentException at java.net.URI.create(URI.JAVA:842) could you suggest me any solutions |
| All times are GMT +1. The time now is 12:37 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.