Some help with some code??

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
big_barks
Posts: 1
Joined: Sat Jan 17, 2004 6:49 pm

Some help with some code??

Post by big_barks »

i am working on a project for myself and was wondering if i could get some help. im writting code the will allow a user to serch a particular folder and read the files/subdirectories in it and get a list of them. the problem im trying to figure out is how to only read the names of the files(NOT write to them) and if there are sub directories how to show the subdirectory and then show the files in that. If you can help in any way that would be greatly appreciated.
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

DIR *.* /s does that now.
You could pipe it to FIND to filter out unwanted lines.



=============================
Edited after submission:
Oops, you probably wanted java code, not my area, sorry......
Hope this was helpful.............good luck,
Bob
User avatar
talleyrand
Posts: 624
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

Post by talleyrand »

Too lazy to do much testing but this seems about right

Create a java.io.File for the directory, then ask for the list of files with one of the following:

public java.lang.String[] list();
public java.lang.String[] list(java.io.FilenameFilter);
public java.io.File[] listFiles();
public java.io.File[] listFiles(java.io.FilenameFilter);
public java.io.File[] listFiles(java.io.FileFilter);

this and more from jguru
I choose to fight with a sack of angry cats.
tat
Posts: 3
Joined: Sun Jan 25, 2004 1:12 am
Location: nj

Post by tat »

here is the class which looks into directory C://MyJava/ and lists all files, subdirect. in that directory. it is also writes to the console one of the files

import java.io.*;
import java.util.*;

public class TestFile
{
public static void main (String [] args) throws Exception
{
File listFiles = new File ("C://MyJava/");
File tmpFile = null;
String [] str = listFiles.list();
String strRead = null;
for (int i = 0; i < str.length ;i++)
{
strRead = "";
tmpFile = new File(str);
if (tmpFile.isFile())
{
System.out.println("next file for i = "+i +"is "+ str);
if (i == 3)
{
FileReader fr = new FileReader(tmpFile);
BufferedReader buf = new BufferedReader(fr);
String strTmp = null;
while ((strTmp = buf.readLine())!= null)
strRead += "\n"+strTmp;

System.out.println(strRead);
buf.close();
}

}


}


}
}
Post Reply