/** * does not use recursion to traverse a folder. Speed does not have recursive effect fast */private static list<file> GetFile (String rootdir) {list<
file> files = new arraylist<file> ();
linkedlist<file> list = new linkedlist<file> ();
File dir = new file (RootDir);
File file[] = Dir.listfiles ();
for (int i = 0; i < file.length i++) {if (File[i].isdirectory ()) {List.add (file[i));
} else{//system.out.println (File[i].getabsolutepath ());
Files.add (File[i]);
} File tmp;
while (!list.isempty ()) {tmp = List.removefirst ();/loop operation, delete element.
if (Tmp.isdirectory ()) {file = Tmp.listfiles ();
if (file = = null) continue;
for (int i = 0; i < file.length i++) {if (File[i].isdirectory ()) {List.add (file[i));
else {//system.out.println (File[i].getabsolutepath ());
Files.add (File[i]);
}} else {//system.out.println (Tmp.getabsolutepath ());
Files.add (TMP);
} return to files; }
--------------------------------------------------------------------------------------------- **
* All files and directory names in the output directory
* @param FilePath
*/
public void Readfolderbyfile (String filePath)
{
File File = new file (FilePath);
file[] Tempfile = File.listfiles ();
for (int i = 0;i<tempfile.length;i++)
{
if (Tempfile[i].isfile ())
{
System.out.println ("File:" +tempfile[i].getname ());
}
if (Tempfile[i].isdirectory ())
{
System.out.println ("Directory:" +tempfile[i].getname ());
}
} 2. How to use Java to modify the file name extension file File=new file ("C:/test.txt");
File.renameto (New File ("C:/test.log")); Import java.io.*;
public class Test1
{
public static void Main (string[] args) {
File File=new file ("C:/test.rtf"); Specify file name and path
String Filename=file.getabsolutepath ();
if (Filename.indexof (".") >=0)
{
filename = filename.substring (0, Filename.lastindexof ("."));
}
File.renameto (New File (filename+ ". Doc")); Renamed
}
}
3. Folder traversal
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ page import= "java.io.*"%>
<%
String url1= "C:/AAA";
File f= (new file (URL1));
if (F.isdirectory ()) {
File [] Fe = F.listfiles ();
GO_ON:
for (int i = 0;i<fe.length;i++) {
if (Fe[i].isdirectory ()) {
File [] Fe1 = Fe[i].listfiles ();
for (int j = 0;j<fe1.length;j++) {
if (Fe1[j].isdirectory ())
Continue go_on;
Out.println (Fe1[j].tostring ());
}
}
Else Out.println (fe[i].tostring ());
}
}
} **
* Create and delete files
* @param FilePath
* @param fileName
* @return Create Success returns True
* @throws IOException
*/
public boolean createanddeletefile (String filepath,string fileName) throws IOException
{
Boolean result = false;
File File = new file (filepath,filename);
if (File.exists ())
{
File.delete ();
result = true;
System.out.println ("The file has been deleted.) ");
}
Else
{
File.createnewfile ();
result = true;
System.out.println ("The file has been created.) ");
}
return result;
}
/**
* Create and delete directories
* @param folderName
* @param FilePath
* @return Delete Successful return true
*/
public boolean Createanddeletefolder (String foldername,string FilePath)
{
Boolean result = false;
Try
{
File File = new file (filepath+foldername);
if (File.exists ())
{
File.delete ();
System.out.println ("Directory already exists, deleted!");
result = true;
}
Else
{
File.mkdir ();
SYSTEM.OUT.PRINTLN ("Directory does not exist, has been established!");
result = true;
}
}
catch (Exception ex)
{
result = false;
System.out.println ("Createanddeletefolder is error:" +ex);
}
return result;
}
/**
* All files and directory names in the output directory
* @param FilePath
*/
public void Readfolderbyfile (String filePath)
{
File File = new file (FilePath);
file[] Tempfile = File.listfiles ();
for (int i = 0;i<tempfile.length;i++)
{
if (Tempfile[i].isfile ())
{
System.out.println ("File:" +tempfile[i].getname ());
}
if (Tempfile[i].isdirectory ())
{
System.out.println ("Directory:" +tempfile[i].getname ());
}
}
}
/**
* Check if the file is an empty
* @param FilePath
* @param fileName
* @return return true for null
* @throws IOException
*/
public boolean fileisnull (String filepath,string fileName) throws IOException
{
Boolean result = false;
FileReader FR = new FileReader (filepath+filename);
if (fr.read () = =-1)
{
result = true;
System.out.println (filename+ "No data in file!");
}
Else
{
System.out.println (filename+ "Data in file!");
}
Fr.close ();
return result;
}
/**
* Read all content in the file
* @param FilePath
* @param fileName
* @throws IOException
*/
public void Readallfile (String filepath,string fileName) throws IOException
{
FileReader FR = new FileReader (filepath+filename);
int count = Fr.read ();
while (count!=-1)
{
System.out.print ((char) count);
Count = Fr.read ();
if (count = = 13)
{
Fr.skip (1);
}
}
Fr.close ();
}
/**
* Read the data in the file line by row
* @param FilePath
* @param fileName
* @throws IOException
*/
public void Readlinefile (String filepath,string fileName) throws IOException
{
FileReader FR = new FileReader (filepath+filename);
BufferedReader br = new BufferedReader (FR);
String line = Br.readline ();
while (line!= null)
{
System.out.println (line);
line = Br.readline ();
}
Br.close ();
Fr.close ();
}
}