|
|
|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

03-31-2008, 09:55 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 1
|
|
|
Problem in File Handling in Java
We have 3 files file1, file2, file3......Say, File1 contains 10 lines (absolute path names of 10 different files ) and file2 has 4 lines .......all the lines of file2 are there in file1 also.......What we want is to copy the content of file1 in file3 without those lines which are there in file2....Can anybody please give the java code for this....
file1
/x/guest/english/1.txt
/x/guest/english/2.txt
/x/guest/english/3.txt
/x/guest/english/4.txt
/x/guest/english/5.txt
/x/guest/english/6.txt
/x/guest/english/7.txt
/x/guest/english/8.txt
/x/guest/english/9.txt
/x/guest/english/10.txt
file2
/x/guest/english/10.txt
/x/guest/english/6.txt
/x/guest/english/3.txt
/x/guest/english/8.txt
file3
/x/guest/english/1.txt
/x/guest/english/2.txt
/x/guest/english/4.txt
/x/guest/english/5.txt
/x/guest/english/7.txt
/x/guest/english/9.txt
|
|

03-31-2008, 10:04 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
|
|
|
First read all files line-by-line and store the read values in separate arrays. Then by simply comparing write values to any file you want.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

03-31-2008, 12:46 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 239
|
|
Heres a quick example to get you started.
This code reads a file in line by line and adds each value to an array.
FileInputStream in = new FileInputStream("file1.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String[] myarray;
myarray = new String[10];
while ((strLine = br.readLine()) != null) {
for (int j = 0; j < myarray.length; j++){
myarray[j] = br.readLine();
}
}
in.close();
System.out.println(myarray[0]);
System.out.println(myarray[1]);
System.out.println(myarray[2]);
etc..
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

03-31-2008, 01:05 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
|
|
|
Yes it's nice work. Do the same thing for you next file and when you write to the file compare elements and write.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by Eranga : 03-31-2008 at 01:24 PM.
|
|

03-31-2008, 01:26 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
|
|
No pal, how bad me. My dear Don, check your read line. You store in a string in the array by skipping first line. So you should have null pointer exception in this code. Isn't it?
How about something like this.
while((firstFile[i] = br.readLine()) != null){
System.out.println(firstFile[i]);
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by Eranga : 03-31-2008 at 01:30 PM.
|
|

03-31-2008, 01:57 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 239
|
|
|
Ah yeah Eranga, your right.
This does actually skip the first line. I didn't test this first as I did it from memory.
You get the idea though... lol
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

03-31-2008, 02:02 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
|
|
Originally Posted by DonCash
Ah yeah Eranga, your right.
This does actually skip the first line. I didn't test this first as I did it from memory.
You get the idea though... lol
Me too pal, not tested at all. Seen it in last minute actually.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
All times are GMT +3. The time now is 02:11 PM.
|
|
VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org