Results 1 to 7 of 7
Thread: Problem in File Handling in Java
- 03-31-2008, 07:55 AM #1
Member
- Join Date
- Mar 2008
- Posts
- 1
- Rep Power
- 0
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, 08:04 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 03-31-2008, 10:46 AM #3
Heres a quick example to get you started.
This code reads a file in line by line and adds each value to an array.
Java 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();Java Code:System.out.println(myarray[0]); System.out.println(myarray[1]); System.out.println(myarray[2]); etc..
Did this post help you? Please
me! :cool:
- 03-31-2008, 11:05 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes it's nice work. Do the same thing for you next file and when you write to the file compare elements and write.
Last edited by Eranga; 03-31-2008 at 11:24 AM.
- 03-31-2008, 11:26 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
Java Code:while((firstFile[i] = br.readLine()) != null){ System.out.println(firstFile[i]); }Last edited by Eranga; 03-31-2008 at 11:30 AM.
- 03-31-2008, 11:57 AM #6
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... lolDid this post help you? Please
me! :cool:
- 03-31-2008, 12:02 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
problem with event handling!!!
By ahdus in forum Java AppletsReplies: 1Last Post: 11-17-2007, 06:24 PM -
problem with jar file pls help
By jinu5 in forum New To JavaReplies: 0Last Post: 08-15-2007, 10:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks