Page 1 of 1
Some help with some code??
Posted: Sat Jan 17, 2004 6:54 pm
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.
Posted: Sat Jan 17, 2004 7:53 pm
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......
Posted: Sat Jan 17, 2004 10:48 pm
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
Posted: Sun Jan 25, 2004 2:39 am
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();
}
}
}
}
}