Forums Register Login

read the contents of the Directory & then read a file from the directory

+Pie Number of slices to send: Send
Hai Implemented a code that will read the 100s of files from a directory.
Each time when i read a file , i will take that file and check the content of the file( i put them in to a List )and compare with the original List and if there is a different, then i update the array List with write function.
this is what i need
but i get error when i run the code, i coul dread only one file, later it stopped. it does not read the second file in the directory.
could any 1 help me o this

the code what i have is given below
+Pie Number of slices to send: Send
From your code, I'm betting there's an error message being printed out. What does it say?

In general, using e.getMessage() is a poor way to print an exception. The problem is that it can lose important information, such as the name of the exception class, and the stack trace. You will be better off if you use e.printStackTrace() instead, which also includes the message and class name. Or if you use a logging framework (like log4j, highly recommended), use one of the logging methods that takes a Throwable as a parameter.
+Pie Number of slices to send: Send
Also, please indent your code to make it more readable. People here will be much more likely to read it if it's in a familiar format.
+Pie Number of slices to send: Send
"loly party",

Please read your private messages regarding an important announcement.

Thank you,

Rob

Now, getting back to your problem. I think the cause is in this line:

loly party wrote:FileInputStream fstream = new FileInputStream(filename);


filename is just the name of the file; e.g. Document1.txt. It no longer contains the path information. Therefore, it will only work if there is a file with the same name in the folder you are executing your application from.

If you switch to listFiles() instead of list(), you get File objects back instead of only the file names. File object do keep their full path. You can then use the FileInputStream constructor that takes a File instead of a String.
+Pie Number of slices to send: Send
Thanks for the messages, sent to me,
i updated to my Original Name
Sorry for the trouble

as you mentioned l changed to Listfiles() instead of lst. but i get still error on the flowing part of the code

.


that is incompatible types
on

String filename = children[i];




can you please explain me little more.
i could not figure out it

also
Earlier the output was



run:
1., 3171.
Error: 3934.ixf (The system cannot find the file specified)
2., 3426.
3., 3780.
4., 4462.
5., 4899.
6., 4681.
7., 4761.
8., 3546.
9., 3525.
10., 3735.
11., 3658.
12., 5701.
13., 3198.
14., 3543.
15., 3751.
16., 5403.
17., 6186.
18., 5167.
19., 4894.
20., 3832.
21., 3829.
22., 4437.
23., 4126.
24., 3821.
25., 4070.
26., 4058.
27., 4126.
28., 3641.
29., 3387.
30., 3315.
31., 4018.
32., 5231.
33., 4919.
34., 3587.
35., 3859.
36., 4001.
37., 3811.
38., 4157.
39., 4191.
40., 4187.
41., 3142.
42., 195.
BUILD SUCCESSFUL (total time: 0 seconds)



thanks

+Pie Number of slices to send: Send
 

Dhan Priyan wrote:String filename = children[i];


What is the type of children? Hint: it's not String[] anymore.
+Pie Number of slices to send: Send
I understood it cannot be ;
but with poor knowledge i could not figure out how to solve it,
sorry for it

// The error in the other thread
i removed that already and i got some error with substring



run:
E:\java\check\100130.ixf
java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.substring(String.java:1935)
at CompareList.main(CompareList.java:84)
BUILD SUCCESSFUL (total time: 0 seconds)



do you have any suggestions
+Pie Number of slices to send: Send
I understood it cannot be ;
but with poor knowledge i could not figure out how to solve it,
sorry for it

// The error in the other thread
i removed that already and i got some error with substring



run:
E:\java\check\100130.ixf
java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.substring(String.java:1935)
at CompareList.main(CompareList.java:84)
BUILD SUCCESSFUL (total time: 0 seconds)



do you have any suggestions
+Pie Number of slices to send: Send
Based on the stack trace, on line 84 of CompareList.java, you are calling substring(), and one of the indices is 2. This index does not exist in the string, probably because it is too short. Have you tried reading the substring() API to see how the indices are used?

Now, I have no idea why you are calling substring(), since you haven't shown us. Is CompareList even the same class as you've shown us previously? Perhaps you've just renamed it? Or is this something completely new?
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1569 times.
Similar Threads
command line arguments for a file
Why do i get NoClassDefFound error??
Reading one line from a text
Regarding reading a file in a jsp
to read lines inside brackets
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:08:41.