Java files File Operation summary __java

Source: Internet
Author: User

Because the Java file feature has been in use, but it's always confusing, today is a summary of all the features of Java file.

Java file Operations I personally think that the important issues are:

A: How to cross platform issues
B: File coding issues, especially how to work properly in the case of multi-language platforms.
C: File read and write efficiency, operational efficiency
D: File encryption and file security
E: File fast retrieval, it is strongly recommended to use Lence for file retrieval and file management.

Here are some of my finishing:

One: Create a document

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 create a file:

File MyFile = new file ("C:" + file.separator + "jdk1.5.0" + file.separator, "File.java");

File.separator is a file path symbol.
System.out.println (Myfile.getname ());//Get File name method
System.out.println (Myfile.getpath ());//method of obtaining file path
System.out.println (Myfile.isabsolute ())//To determine whether the file is complete
System.out.println (Myfile.getparent ());//Get the root directory of the file
System.out.println (Myfile.exists ())//To determine whether the file exists
System.out.println (Myfile.isdirectory ());//Judge whether it is a directory
System.out.println (Myfile.isfile ());//Judge whether it is a file
System.out.println (Myfile.ishidden ());//Determine if the file is hidden
System.out.println (Myfile.canread ())//To determine whether it is readable
System.out.println (Myfile.canwrite ());//To determine whether or not to write

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 named
Myfile_c.setreadonly ()//set to read-only
Second: File Filtering method

Java provides a good excuse for file filtering: 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 ()));
}
}
}
Third: 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 ("Create directory Success");
}else{
SYSTEM.OUT.PRINTLN ("Build directory Failed");
}

File Myfile_a=new file ("d:/myfubin/test/");
if (Myfile_a.mkdirs ()) {//Multilevel directory
SYSTEM.OUT.PRINTLN ("Create directory Success");
}else{
SYSTEM.OUT.PRINTLN ("Build directory Failed");
}

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 files
System.out.println ("The temporary document just established in:" + Tmp.getcanonicalpath ());
catch (IOException e) {
E.printstacktrace ();
}


}

}

Import Java.io.File;
/**
* @author fb  www.cujava.com
* File Operation Java 1.6 new features: Query disk space
*/
public class Spacechecker {

  public static void Main (string[] args) {
  file[] roots = file.listroots ()//Get all the roots, If it is a Windows system then it will get all the disks
      for (int i = 0; i < roots.length; i++) {
     &NB Sp  system.out.println (Roots[i]);
        system.out.println ("free spaces =" + roots[i].getfreespace ());
        system.out.println ("Usable space =" + Roots[i].getusablespace ());
       &NBSP;SYSTEM.OUT.PRINTLN ("total spaces =" + roots[i].gettotalspace ());
        system.out.println ();
      }
  }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.