Results 1 to 3 of 3
- 05-11-2009, 06:21 AM #1
Member
- Join Date
- Mar 2009
- Location
- London
- Posts
- 8
- Rep Power
- 0
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
i can see from the code that children[i] only gives the error when i use that in the second part of the code.
run:
E:\java\check\100130.ixf
Error: children[i] (The system cannot find the file specified)
BUILD SUCCESSFUL (total time: 2 seconds)
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()); } } }
- 05-11-2009, 06:53 AM #2
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
RamyaRamya:cool:
- 05-11-2009, 10:07 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Is there a way to read a file directory
By willemjav in forum Java AppletsReplies: 3Last Post: 09-05-2011, 01:01 AM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
swapping the contents of the file and writing to another file
By Ms.Ranjan in forum New To JavaReplies: 9Last Post: 07-10-2008, 04:52 PM -
[SOLVED] File chooser selecting file from directory...?
By prabhurangan in forum AWT / SwingReplies: 12Last Post: 06-18-2008, 04:08 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks