?
Solved

Multiple File Reader

Posted on 2003-03-27
5
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

770 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