Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-2008, 09:55 AM
Member
 
Join Date: Mar 2008
Posts: 1
getbiplab is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-31-2008, 10:04 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-31-2008, 12:46 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
Heres a quick example to get you started.

This code reads a file in line by line and adds each value to an array.

Code:
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();
Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-31-2008, 01:05 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-31-2008, 01:26 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-31-2008, 01:57 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-31-2008, 02:02 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by DonCash View Post
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling ASCII character set in Java (III) JavaForums Java Blogs 0 02-08-2008 11:31 AM
Handling ASCII character set in Java (II) JavaForums Java Blogs 0 02-07-2008 03:00 PM
Handling ASCII character set in Java (I) JavaForums Java Blogs 0 02-07-2008 03:00 PM
problem with event handling!!! ahdus Java Applets 1 11-17-2007 08:24 PM
problem with jar file pls help jinu5 New To Java 0 08-16-2007 12:41 AM


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