This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes read the contents of the Directory & then read a file from the directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "read the contents of the Directory & then read a file from the directory" Watch "read the contents of the Directory & then read a file from the directory" New topic
Author

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

Dhan Priyan
Greenhorn

Joined: May 10, 2009
Posts: 12
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
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

"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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dhan Priyan
Greenhorn

Joined: May 10, 2009
Posts: 12
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

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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

What is the type of children? Hint: it's not String[] anymore.
Dhan Priyan
Greenhorn

Joined: May 10, 2009
Posts: 12
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
Dhan Priyan
Greenhorn

Joined: May 10, 2009
Posts: 12
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
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read the contents of the Directory & then read a file from the directory
 
Similar Threads
command line arguments for a file
Regarding reading a file in a jsp
Reading one line from a text
to read lines inside brackets
Why do i get NoClassDefFound error??