By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,676 Members | 1,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,676 IT Pros & Developers. It's quick & easy.

Read file from directory, update contents of the each file

P: 18
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
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataInputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.RandomAccessFile;
  9. import java.util.ArrayList;
  10.  
  11.  
  12. public class CompareList
  13.     {
  14.  
  15.     public static void main(String[] args){
  16.  
  17.          ArrayList a1 = new ArrayList();
  18.          ArrayList a2 = new ArrayList();
  19.          a1.add("1");
  20.          a1.add("2");
  21.          a1.add("3");
  22.          a1.add("4");
  23.          a1.add("5");
  24.          a1.add("6");
  25.          a1.add("7");
  26.          a1.add("8");
  27.          a1.add("9");
  28.          a1.add("10");
  29.          a1.add("11");
  30.          a1.add("12");
  31.          a1.add("13");
  32.          a1.add("14");
  33.          a1.add("15");
  34.          a1.add("16");
  35.          a1.add("17");
  36.          a1.add("18");
  37.          a1.add("19");
  38.          a1.add("20");
  39.          a1.add("21");
  40.          a1.add("22");
  41.          a1.add("23");
  42.          a1.add("24");
  43.          a1.add("25");
  44.          a1.add("26");
  45.          a1.add("27");
  46.          a1.add("28");
  47.          a1.add("29");
  48.          a1.add("30");
  49.          a1.add("31");
  50.          a1.add("32");
  51.          a1.add("33");
  52.          a1.add("34");
  53.          a1.add("35");
  54.          a1.add("36");
  55.          a1.add("37");
  56.          a1.add("38");
  57.          a1.add("39");
  58.          a1.add("40");
  59.          a1.add("41");
  60.          a1.add("42");
  61.          a1.add("43");
  62.          a1.add("44");
  63.          a1.add("45");
  64.          a1.add("46");
  65.          a1.add("47");
  66.          a1.add("48");
  67.          a1.add("49");
  68.          a1.add("50");
  69.          a1.add("51");
  70.          a1.add("52");
  71.  
  72.           try{
  73.  
  74.                 File dir = new File("E:\\java\\check");
  75.  
  76.                 File[] children = dir.listFiles();
  77.                 if (children == null) {
  78.                       System.out.println("does not exist or is not a directory");
  79.                  }
  80.  
  81.     else {
  82.            for (int i = 0; i < children.length; i++) {
  83.                    System.out.println( children[i]);
  84.                     FileInputStream fstream = new FileInputStream("children[i]");
  85.                     DataInputStream in = new DataInputStream(fstream);
  86.                       BufferedReader br = new BufferedReader(new InputStreamReader(in));
  87.                     String strLine;
  88.                     while ((strLine = br.readLine()) != null)     {
  89.                    String line =strLine.substring(0,2);
  90.                     // Print the content on the console
  91.                      //System.out.println(line);
  92.                      a2.add(line);
  93.                   } // end of add list2
  94.  
  95.            if (a1.size() > a2.size())
  96.          {
  97.              int k = 0;
  98.              for (int l = 0; l < a2.size(); l++)
  99.              {
  100.                  if (!((String)a1.get(l)).equals((String)a2.get(l)))
  101.                  {
  102.                      //System.out.println((String)a2.get(i));
  103.                       // System.out.println("dd");
  104.                  }
  105.                  k = l;
  106.              }
  107.              k++;
  108.              for (int l = k; l < a1.size(); l++)
  109.              {
  110.                  System.out.println((String)a1.get(l));
  111.                 String str = "children[l]";
  112.  
  113.                 File file = new File(str);
  114.                  RandomAccessFile rand = new RandomAccessFile(file,"rw");
  115.  
  116.                   rand.seek(file.length());  //Seek to end of file
  117.                   rand.writeBytes((String)a1.get(i));  //Write end of file
  118.                   rand.writeBytes("., 0.");
  119.                   rand.writeBytes("\n");
  120.  
  121.  
  122.              }
  123.          }// end of comparing and updating the list2
  124.  
  125.         in.close();
  126.         }
  127.     }
  128.      }
  129.               catch (Exception e){//Catch exception if any
  130.                 System.err.println("Error: " + e.getMessage());
  131.         }
  132.     }
  133. }
  134.  
  135.  
May 11 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.