![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 7
Rep Power: 0
![]() |
i ve been trying to find a way to successfully read text files and the be able either to read their data on screen or write their data to another text file.unfortunately i have not achieved yet.could anybody please write me a simple example of java code which reads for example the first,third and fourth line of a text file and another program that reads and writes the same lines to another text file?how can i read the entire file contents?
i am sorry i am asking so many things but i am disappointed and confused.... ![]() thanks in advance guys ![]() |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
Reading a file's contents: (from http://javaalmanac.com/egs/java.io/R...sFromFile.html)
* *try {
* * * *BufferedReader in = new BufferedReader(new FileReader("infilename"));
* * * *String str;
* * * *while ((str = in.readLine()) != null) {
* * * * * *process(str);
* * * *}
* * * *in.close();
* *} catch (IOException e) {
* *}Read from one file and write to another: (from http://javaalmanac.com/egs/java.io/CopyFile.html?l=new) * *// Copies src file to dst file.
* *// If the dst file does not exist, it is created
* *void copy(File src, File dst) throws IOException {
* * * *InputStream in = new FileInputStream(src);
* * * *OutputStream out = new FileOutputStream(dst);
* *
* * * *// Transfer bytes from in to out
* * * *byte[] buf = new byte[1024];
* * * *int len;
* * * *while ((len = in.read(buf)) > 0) {
* * * * * *out.write(buf, 0, len);
* * * *}
* * * *in.close();
* * * *out.close();
* *}hope that will help get your started. Note the classes used to read and write. You can find them in the JavaTM 2 Platform, Standard Edition, v 1.4.2 API Specification under java.io
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ]; /* Don't make me use it! */ |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2004
Posts: 7
Rep Power: 0
![]() |
thank you very much mate!i ll check them out!
![]() |
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Also, check out the new Scanner class. Much easier for input, in my opinion.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|