• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Master Rancher
Posts: 5149
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 5149
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22832
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Dhan Priyan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22832
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


What is the type of children? Hint: it's not String[] anymore.
 
Dhan Priyan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 5149
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic