Transfer from http://shameant.iteye.com/blog/163007
Java file Operations I personally think that the important issues are:
A: How to cross-platform issues
B: File encoding issues, especially in the case of multi-lingual platform how to work properly.
C: File reading and writing efficiency, operational efficiency
D: File encryption and file security
E: Fast file retrieval, it is strongly recommended to use Lence for file retrieval and file management.
Here are some of the things I do:
One: Document creation
File File1 = new file ("C:\\temp\\mynote.txt"); In Windows this is the method under Windows file system
File File2 = new file ("/tmp/mynote.txt"); In Linux/unix Unix file system methods
The safest way to build a file:
File MyFile = new file ("C:" + file.separator + "jdk1.5.0" + file.separator, "File.java");
File.separator is the file path symbol.
System.out.println (Myfile.getname ());//method of obtaining the file name
System.out.println (Myfile.getpath ());//method of obtaining a file path
System.out.println (Myfile.isabsolute ());//Determine if the file is complete
System.out.println (Myfile.getparent ());//Get the root directory of the file
System.out.println (Myfile.exists ());//Determine if the file exists
System.out.println (Myfile.isdirectory ());//Determine if it is a directory
System.out.println (Myfile.isfile ());//Determine if it is a file
System.out.println (Myfile.ishidden ());//Determine if it is a hidden file
System.out.println (Myfile.canread ());//Judging whether it is readable
System.out.println (Myfile.canwrite ());//Judging whether it can be written
File Myfile_a = new file ("C:" + file.separator);
For (String s:myfile_a.list ()) {//Read all files under a directory
System.out.println (s);
}
File Myfile_c=new file ("D:/test.txt");
System.out.println (New Date (Myfile_c.lastmodified ()));//Last Edit time
Myfile_c.renameto (New File ("C:/text.txt.bak"));//from naming
Myfile_c.setreadonly ();//set to read-only
Two: File Filtering method
Java provides a good file filtering excuse: FilenameFilter filtered files can be displayed with Listfiles. The efficiency is still very high.
Import Java.io.File;
Import Java.io.FilenameFilter;
Import Java.util.Date;
/**
* File filter Filter class
* @author FB
*/
Class Filelistfilter implements FilenameFilter {
private String name;
Private String extension;
Public Filelistfilter (string name, String extension) {
THIS.name = name;
this.extension = extension;
}
Public Boolean accept (File directory, String filename) {
Boolean FileOK = true;
if (name! = null) {
FileOK = Filename.startswith (name);
}
if (extension! = null) {
FileOK = Filename.endswith ('. ' + extension);
}
return FileOK;
}
}
Test method:
Import Java.io.File;
Import Java.io.FilenameFilter;
Import Java.util.Date;
/**
* File filter Run function
* @author FB
*/
public class Run_filelistfilter {
public static void Main (string[] args) {
File MyDir = new file ("d:/");
FilenameFilter select = New Filelistfilter ("F", "txt");
file[] Contents = mydir.listfiles (select);
for (File file:contents) {
System.out.println (file + "is a" + (File.isdirectory ()? ") Directory ":" File ")
+ "Last modified on\n" + New Date (File.lastmodified ()));
}
}
}
Three: Create directories, files, temporary files, delete files or directories
Import Java.io.File;
Import java.io.IOException;
public class MakeDir {
public static void Main (string[] args) {
File Myfile=new file ("d:/myfubin/");
if (Myfile.mkdir ()) {//single-level directory
SYSTEM.OUT.PRINTLN ("Establish directory Success");
}else{
System.out.println ("Failed to create directory");
}
File Myfile_a=new file ("d:/myfubin/test/");
if (Myfile_a.mkdirs ()) {//Multilevel directory
SYSTEM.OUT.PRINTLN ("Establish directory Success");
}else{
System.out.println ("Failed to create directory");
}
File File = new file ("D:\\myfubin\\test.txt");
try {
File.createnewfile ();//Create an empty file
} catch (IOException e) {
E.printstacktrace ();
}
System.out.println (File.canread ());
if (File.delete ()) {//delete file or delete directory
Another way to delete a file: File.deleteonexit () This method is to delete the file when the program exits
System.out.println ("delete succeeded");
}else{
System.out.println ("delete failed");
}
try {
File TMP = file.createtempfile ("foo", "tmp");//Create temporary file
System.out.println ("Just created temporary file in:" + Tmp.getcanonicalpath ());
} catch (IOException e) {
E.printstacktrace ();
}
}
}
The file class in Java