Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 2nd, 2004, 10:04 AM   #1
FidI
Newbie
 
Join Date: Nov 2004
Posts: 7
Rep Power: 0 FidI is on a distinguished road
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
FidI is offline   Reply With Quote
Old Nov 2nd, 2004, 10:21 AM   #2
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
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! */
sykkn is offline   Reply With Quote
Old Nov 2nd, 2004, 10:29 AM   #3
FidI
Newbie
 
Join Date: Nov 2004
Posts: 7
Rep Power: 0 FidI is on a distinguished road
thank you very much mate!i ll check them out!
FidI is offline   Reply With Quote
Old Nov 4th, 2004, 6:04 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:26 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC