reading files in a directory...

Hi techies,,I am trying to read all the text files in a particular directory.I tried using the File class. but it didnt help..Can anyone provide any help....thanks in advance..Nadir
[216 byte] By [Nadir_khana] at [2007-11-27 4:39:00]
# 1

> Hi techies,,

> I am trying to read all the text files in a

> particular directory.I tried using the File class.

> but it didnt help..

>

> Can anyone provide any help....

Sure.

Post the code that "didnt help" and I (or someone else) can probably see why it "didnt help".

When posting code, please use code tags:

http://forum.java.sun.com/help.jspa?sec=formatting

Also post compiler/runtime errors exactly as they are printed.

Thanks.

prometheuzza at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi just a snapshot of the codes

if (directory.isDirectory() == false)

{

if (directory.exists() == false)

{

System.out.println("There is no such directory!");

}

else

{

System.out.println("That file is not a directory.");

}

}

else

{

files = directory.list();

System.out.println("Files in directory \"" + directory + "\":");

for (int i = 0; i < files.length; i++)

{

//do something

System.out.println(" " + files);

}

}

iam also trying to use this..

import java.io.*;

import java.util.*;

public class Poland

{

public static void main(String args[])

{

try {

String path = "c:\\nadir\\.txt";

BufferedReader in = new BufferedReader(new FileReader(path));

String str;

while ((str = in.readLine()) != null) {

System.out.println(str);

}

//File file = new File("16052007-963.txt");

// Get the number of bytes in the file

//long length = file.length();

//System.out.println(length);

if(in.readLine().equals("963"))

{

System.out.println("try");

}

else

{

System.out.println("Not found");

}

in.close();

} catch (IOException e) {

System.out.println(e);

}

i am bit confused which one will be suitable... please advice

Nadir_khana at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 3
> Hi just a snapshot of the codes> > ...And what is the problem?
prometheuzza at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 4

i want to read all the files in a directory..

i exactly dont know how to go about it...

i am using this way..

String path = "c:\\nadir\\.txt";

BufferedReader in = new BufferedReader(new FileReader(path));

.

to read the complete directory...

is this right..

can u provide some suggestions

Nadir_khana at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 5

> i want to read all the files in a directory..

> i exactly dont know how to go about it...

> i am using this way..

> ...

No. First read specific files from a directory, then process those files.

Here's how you read .txt files from a specific directory:

import java.io.File;

import java.util.ArrayList;

import java.util.List;

public class Main {

private static List<File> readDirectory(File directory, String extension) {

List<File> found = new ArrayList<File>();

if(!directory.isDirectory()) {

throw new IllegalArgumentException("Invalid directory: "+directory);

}

File[] contents = directory.listFiles();

for(File file : contents) {

if(file.getName().endsWith(extension)) {

found.add(file);

}

}

return found;

}

public static void main(String[] args) {

// Get all txt files from C:\Temp

List<File> files = readDirectory(new File("C:\\Temp"), ".txt");

System.out.println("Found: "+files);

// your code here to process the files

}

}

prometheuzza at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 6
i will try this code..but with what will i replace the <FILE> tag...
Nadir_khana at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 7
thanksit worked..it displays me the file types..but how do i read all the files....
Nadir_khana at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 8

> thanks

> it worked..

You're welcome.

> it displays me the file types..

> but how do i read all the files....

How about you try that yourself, and ask a specific question here when you get stuck, OK?

http://www.google.com/search?hl=en&q=reading+files+java&btnG=Google+Search

prometheuzza at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 9

An alternative that I use is the FilenameFilter class:

import java.io.File;

import java.io.FilenameFilter;

public class Main {

public static class TXTFilenameFilter implements FilenameFilter {

public boolean accept( File file, String name ) {

return name.endsWith( "txt" );

}

}

public static void main( String[] args ) {

File file = new File( "c:\\somedir\\" );

String[] list = file.list( new TXTFilenameFilter() );

}

}

remcolliera at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 10
i am trying it myself...just wanted to know some alternatives...thanks for your help once again..i greatly appreciate your help..Many Thanks.Nadir Firfire
Nadir_khana at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 11
> An alternative that I use is the FilenameFilter> class:> > ...Good call remcollier!Next time when posting code, use code tags*: it makes it so much easier to read.; )* http://forum.java.sun.com/help.jspa?sec=formatting
prometheuzza at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 12
no problems!thanks for the heads up :o)
remcolliera at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...
# 13

> i am trying it myself...

> just wanted to know some alternatives...

> thanks for your help once again..

>

> i greatly appreciate your help..

> Many Thanks.

> Nadir Firfire

No problem, you're most welcome.

You can always post follow-up question here if you get stuck. Be sure to post the code you're working on and explain exactly what it is you don't understand.

prometheuzza at 2007-7-12 9:49:31 > top of Java-index,Java Essentials,Java Programming...