?
Solved

Multiple File Reader

Posted on 2003-03-27
5
Medium Priority
?
246 Views
Last Modified: 2012-08-13
How do i read all text files in a directory and load them into seperate text areas respectively?

prefer codes.thanks!

0
Comment
Question by:hellopanda
5 Comments
 
LVL 2

Expert Comment

by:glottis
ID: 8222631
If you know how many text files you have to read, you can create that many textarea array. say you have 5 files, create an array of 5 elements like:

JTextArea[] textArea = new JTextArea[5];
then iterate a loop like
for(int i=0; i<5; i++) {
  String s = readFile();
  textArea[i].setText(s);
}
0
 

Author Comment

by:hellopanda
ID: 8222965
wat if i dunno how many txt files i have to read?
0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8222972
Iterate through the files in the directory, and create an object to handle each file.  You have to use a layout manager to place your text areas, and if you expect to have more than a few, put them on a scrollable, resizable pane.
I'd give code, except this sounds like a homework problem...

brian
0
 

Author Comment

by:hellopanda
ID: 8223493
hrm bkrahmer,can u just enlighten me the part on how to iterate through files in the directory?

i am just stuck at that logic
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 150 total points
ID: 8223615
You can try using File....

File obj = new File ( path ) ;

Here, path is a string - it can represent the path of a directory or of a file

if ( obj.isDirectory () )
 // 'path' specified a directory

else
 // 'path' specified an individual file

Also, in case 'path' specified a directory, then all files in it can be obtained as:

String s[] = obj.list () ;

for ( int i = 0 ; i < s.length ; i ++ )
{
 File temp = new File ( path + "\\" + s[i] ) ; // For UNIX: new File ( path + "/" + s[i] ) ;

 if ( ! temp.isDirectory () )
   // s[i] is a file
   // check if it has a ".txt" sub--string in the end: if yes, then its a text-file

} // end for


Hope that helps!

Mayank.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question