Some help with some code??
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
Some help with some code??
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.
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
- talleyrand
- Posts: 624
- Joined: Mon Jul 21, 2003 6:56 pm
- Location: Kansas City, MO, USA
- Contact:
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
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.
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();
}
}
}
}
}
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();
}
}
}
}
}