Results 1 to 3 of 3
  1. #1
    svpriyan is offline Member
    Join Date
    Mar 2009
    Location
    London
    Posts
    8
    Rep Power
    0

    Default Read file from directory, update contents of the each file

    hai,
    I could not understand how to debug the error i have now.
    target
    1- read the files from directory // i able ot do this part
    2- for each file
    for each file read the content & compare with the existing List & update the list // i able to do this part

    when i try to combine both parts , i got some following error

    run:
    E:\java\check\100130.ixf
    Error: children[i] (The system cannot find the file specified)
    BUILD SUCCESSFUL (total time: 2 seconds)
    i can see from the code that children[i] only gives the error when i use that in the second part of the code.

    can any one help me on this??
    Thanks
    Priyan


    the original code
    Java Code:
     
    
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.io.RandomAccessFile;
    import java.util.ArrayList;
    
    
    public class CompareList
    	{
    
    	public static void main(String[] args){
    
             ArrayList a1 = new ArrayList();
             ArrayList a2 = new ArrayList();
             a1.add("1");
             a1.add("2");
             a1.add("3");
             a1.add("4");
             a1.add("5");
             a1.add("6");
             a1.add("7");
             a1.add("8");
             a1.add("9");
             a1.add("10");
             a1.add("11");
             a1.add("12");
             a1.add("13");
             a1.add("14");
             a1.add("15");
             a1.add("16");
             a1.add("17");
             a1.add("18");
             a1.add("19");
             a1.add("20");
             a1.add("21");
             a1.add("22");
             a1.add("23");
             a1.add("24");
             a1.add("25");
             a1.add("26");
             a1.add("27");
             a1.add("28");
             a1.add("29");
             a1.add("30");
             a1.add("31");
             a1.add("32");
             a1.add("33");
             a1.add("34");
             a1.add("35");
             a1.add("36");
             a1.add("37");
             a1.add("38");
             a1.add("39");
             a1.add("40");
             a1.add("41");
             a1.add("42");
             a1.add("43");
             a1.add("44");
             a1.add("45");
             a1.add("46");
             a1.add("47");
             a1.add("48");
             a1.add("49");
             a1.add("50");
             a1.add("51");
             a1.add("52");
    
              try{
                  
                    File dir = new File("E:\\java\\check");
    
                    File[] children = dir.listFiles();
                    if (children == null) {
                          System.out.println("does not exist or is not a directory");
                     }
    
        else {
               for (int i = 0; i < children.length; i++) {
                       System.out.println( children[i]);
                        FileInputStream fstream = new FileInputStream("children[i]");
                        DataInputStream in = new DataInputStream(fstream);
                          BufferedReader br = new BufferedReader(new InputStreamReader(in));
                        String strLine;
                        while ((strLine = br.readLine()) != null) 	{
                       String line =strLine.substring(0,2);
                        // Print the content on the console
                         //System.out.println(line);
                         a2.add(line);
                      } // end of add list2
    
               if (a1.size() > a2.size())
             {
                 int k = 0;
                 for (int l = 0; l < a2.size(); l++)
                 {
                     if (!((String)a1.get(l)).equals((String)a2.get(l)))
                     {
                         //System.out.println((String)a2.get(i));
                          // System.out.println("dd");
                     }
                     k = l;
                 }
                 k++;
                 for (int l = k; l < a1.size(); l++)
                 {
                     System.out.println((String)a1.get(l));
                    String str = "children[l]";
     
                    File file = new File(str);
                     RandomAccessFile rand = new RandomAccessFile(file,"rw");
    
                      rand.seek(file.length());  //Seek to end of file
                      rand.writeBytes((String)a1.get(i));  //Write end of file
                      rand.writeBytes("., 0.");
                      rand.writeBytes("\n");
    
    
                 }
             }// end of comparing and updating the list2
    
    		in.close();
    		}
        }
         }
                  catch (Exception e){//Catch exception if any
                    System.err.println("Error: " + e.getMessage());
    		}
    	}
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    828
    Rep Power
    6

    Default

    Hi,
    Seeing your code I can say u are very new to Java.

    One small suggestion.
    Why are u adding 1,2,3...like this ...U can put a loop know?


    Don't put quotes around this childern[i].
    FileInputStream fstream = new FileInputStream(children[i]);

    In your code ,lot of comments are there.But as a beginner I don't want to.Go thru some good Java material before starting something.

    -Regards
    Ramya
    Ramya:cool:

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

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Don't put quotes around this childern[i].
    FileInputStream fstream = new FileInputStream(children[i]);
    Java doc explain about this more clearly, what are the correct arguments you have to pass.

Similar Threads

  1. Is there a way to read a file directory
    By willemjav in forum Java Applets
    Replies: 3
    Last Post: 09-05-2011, 01:01 AM
  2. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  3. Replies: 9
    Last Post: 07-10-2008, 04:52 PM
  4. [SOLVED] File chooser selecting file from directory...?
    By prabhurangan in forum AWT / Swing
    Replies: 12
    Last Post: 06-18-2008, 04:08 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

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