Results 1 to 7 of 7
  1. #1
    getbiplab is offline Member
    Join Date
    Mar 2008
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  3. #3
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    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:

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  6. #6
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    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 me! :cool:

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

Similar Threads

  1. problem with event handling!!!
    By ahdus in forum Java Applets
    Replies: 1
    Last Post: 11-17-2007, 06:24 PM
  2. problem with jar file pls help
    By jinu5 in forum New To Java
    Replies: 0
    Last Post: 08-15-2007, 10:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •