i have few txt files in one folder. i need a program that will open that folder and read all files one by one to find a string and give me output.

so far i got code to do this from only one particular txt file. but i cant make it from many files in a folder.

can anybody help me pls.................

FileReader fr;				BufferedReader br;
String result="";
String word= new String();
String target = "friend";


try{ //read one text file 
fr = new FileReader ("C:/Users/user/Desktop/java/test.txt");
br = new BufferedReader(fr);
Scanner scan = new Scanner(br); 


while(scan.hasNext()){  
result = scan.findWithinHorizon(target,0); 

if(result!=null) {
word = (scan.next() + scan.findWithinHorizon("", 0));

ArrayList<String> names = new ArrayList<String>();
names.add(word);
for (int i=0; i< names.size() ; i++) {
System.out.println(names.get(i));}

You can create Java File that refers to a directory just like you can for a file, ie new File(directory path). Once you've got the File object for the directory, you can use its listFiles method to get all the files (optionally matching a filter for .txt files only) in it, so you can then loop thru them as you wish.

i just did it... it's not showing any error but no output
can u pls tell me where need to edit ??

in addition, i have only txt files in my folder.
so far i did;

File directory = new File("C:/Users/user/Desktop/java/test");
String filename[] = directory.list();
for (int i = 0; i < filename.length; i++) {
listFilenames = filename;}
//    System.out.print(listFilenames);
fr = new FileReader (listFilenames);
br = new BufferedReader(fr);
Scanner scan = new Scanner(br);

What is your directory name? Looking at the first code you posted it should be C:/Users/user/Desktop/java. Did you create a sub-directory called test?
You can call directory.exists() after your new File(...) to see if you've got a valid directory reference.

What is your directory name? Looking at the first code you posted it should be C:/Users/user/Desktop/java. Did you create a sub-directory called test?
You can call directory.exists() after your new File(...) to see if you've got a valid directory reference.

thanks and sorry for late reply...
yes i have made a sub-directory called test which contains three txt files, these are test1.txt, test2.txt, test3.txt.....

now my programing is giving list for these txt files but cant open and read the files one by one. would u pls tell where should i have to change or what need to add .............

probably easiest to get the files directly, and loop thru them, as in

File files[] = directory.listFiles();
for (File f : files) {
   // do whatever you want with each  File f
}

probably easiest to get the files directly, and loop thru them, as in

File files[] = directory.listFiles();
for (File f : files) {
   // do whatever you want with each  File f
}

thank u so much....

it's working now, i m really appreciate u.

probably easiest to get the files directly, and loop thru them, as in

File files[] = directory.listFiles();
for (File f : files) {
   // do whatever you want with each  File f
}

thank u so much....

it's working now, i m really appreciate u.

Hi all I am writing a client and server side programm using socket programming and I want download by browsing files from server using the socket to the client side.
Can you give me a hint code

Thank you................So Much.............................
It works Perfectly............!!!!!!

Dear
Could you post the piece of code to read all the .txt's from a folder ?

I am having the same problem over here

File f = new File("D:\\Resources");
    File[] files = f.listFiles();
    for (File file : files) {
        System.out.println(file.getName());
    }

Pls help urgently, i want to read text files from directory but keeps giving me 3 errors

C:\Users\teejay\Documents\OneFile.java:52: reached end of file while parsing
}

C:\Users\teejay\Documents\OneFile.java:18: 'try' without 'catch' or 'finally'
try{ //read one text file

C:\Users\teejay\Documents\OneFile.java:18: 'try' without 'catch' or 'finally'
try{ //reached end of file whele parsing.

This is the code below

import java.io.*;
public class OneFile
{
    public static void main( String args[] )
    {

FileReader fr; BufferedReader br;

String result="";

String word= new String();

String target = "friend";



try{ //read one text file


fr = new FileReader ("C:/Users/teejay/Documents/Java/Tunji.txt");

br = new BufferedReader(fr);

Scanner scan = new Scanner(br);






while(scan.hasNext()){

result = scan.findWithinHorizon(target,0);



if(result!=null) {

word = (scan.next() + scan.findWithinHorizon("", 0));



ArrayList<String> names = new ArrayList<String>();

names.add(word);

for (int i=0; i< names.size() ; i++) {

System.out.println(names.get(i));}
}
}

Pls help urgently, i want to read text files from directory but keeps giving me 3 errors

C:\Users\teejay\Documents\OneFile.java:52: reached end of file while parsing
}

C:\Users\teejay\Documents\OneFile.java:18: 'try' without 'catch' or 'finally'
try{ //read one text file

C:\Users\teejay\Documents\OneFile.java:18: 'try' without 'catch' or 'finally'
try{ //reached end of file whele parsing.

This is the code below

import java.io.*;
public class OneFile
{
    public static void main( String args[] )
    {

FileReader fr; BufferedReader br;

String result="";

String word= new String();

String target = "friend";



try{ //read one text file


fr = new FileReader ("C:/Users/teejay/Documents/Java/Tunji.txt");

br = new BufferedReader(fr);

Scanner scan = new Scanner(br);






while(scan.hasNext()){

result = scan.findWithinHorizon(target,0);



if(result!=null) {

word = (scan.next() + scan.findWithinHorizon("", 0));



ArrayList<String> names = new ArrayList<String>();

names.add(word);

for (int i=0; i< names.size() ; i++) {

System.out.println(names.get(i));}
}
}

end quote.

Start a new thread and use code tags. Press the (code) button and put code inside

Hi,
I want to open the text files present on hard disk....
For Example: it should check the files in C:/ then in C:/Users/ then in C:/Users/student/ then in C:/Users/student/Documents/ then in C:/Users/student/Documents/Java/..... and so on...

Can anybody tell me how to do this...

And first i want to do this on windows then on Linux also... So also tell the usage of file path in both Operating System..

Thanksin Advance

can we have something like thins in any other prgramming except java?? e.g MATLAB .. or C++ ??

Pls. can anybody help me with

Design java classes and methods necessary for implementation of student record and result processing system.

Thank you.

DaniWeb Member Rules (which you agreed to when you signed up) include:

"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules
Please start your own new thread for your question

"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules
Post what you have done so far and someone will help you from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.