The File class foundation for Java IO streams

Source: Internet
Author: User

Package IO;

Import Java.io.File;
Import java.io.IOException;

public class FileIO {


/**
* Build and get file name information
* @param args
*/
public static void FileName (string[] args) {
SYSTEM.OUT.PRINTLN ("Path delimiter:" +file.pathseparator); Path delimiter
System.out.println ("Name delimiter:" +file.separator);//Name delimiter

Relative path to the project as the starting path to find the corresponding file for example: 1.jpg
String path = "img"; Relative path
String name = "1.jpg";

Absolute path find the corresponding file from your computer's hard drive for example: C:\Users\Yonim\Pictures\Camera roll\1.jpg
In Java, "\" is a special character that needs to be deserialized.
String path2 = "C:\\users\\yonim\\pictures\\camera roll\\2.jpg";
String name2 = "";


File File = new file (path,name); This is the constructor that creates a relative path
File =new file (new file (path), name);//The second way of building

System.out.println ("Find file Name:" +file.getname ());
System.out.println ("Find file path:" +file.getpath ());

Constructors created with an absolute path

File F = new file (path2);
System.out.println ("File name for absolute path:" +f.getname ());
System.out.println ("File path to Absolute path:" +f.getpath ());
SYSTEM.OUT.PRINTLN ("Upper-level directory:" +f.getparent ());//If there is no upper-level directory returned empty
File URL = new file ("Users\\yonim\\pictures\\camera roll\\2.jpg");

Drive letter with the drive letter of the project file name as the absolute path in case the file does not have a drive letter
System.out.println ("File name for absolute path" +url.getabsolutepath ());

}

/**
* Determine file information
*/
public static void Isfile () {

String path = "img";
String name = "1.jpg";

File File = new file (path,name);

System.out.println (File.exists ()); Determines whether a file or folder exists and is writable, exists, and is writable to return true

/**
* Determines whether the file, when present, returns true
* Returns False when it is not a file or does not exist
*/
if (File.isfile ()) {//whether the file
SYSTEM.OUT.PRINTLN ("Existence of this file");
}else if (file.isdirectory ()) {//whether the folder
System.out.println ("is a folder");
}else{
SYSTEM.OUT.PRINTLN ("File does not exist");
}

/**
* Determine if absolute path
* Yes returns true otherwise returns false
*/
if (File.isabsolute ()) {
System.out.println ("is absolute path");
}else{
SYSTEM.OUT.PRINTLN ("relative path");
}

/**
* File size can be used to determine whether a file or a folder
* File.length cannot read folder size
*/
if (File.length () >0) {
System.out.println (File.getname () + "File size:" +file.length () + "byte");
}

}

/**
* Create and delete file information
* @throws IOException
*/
public static void NewFile () throws ioexception{

String Path = "D:\\myimg";
String name = "1.jpg";
File File = new file (path,name);

If it is not a file
if (!file.exists ()) {
Returns false if it exists
System.out.println ("Whether to create success:" +file.createnewfile ());
}

deleting files
if (File.delete ()) {
System.out.println ("delete succeeded");
}else{
System.out.println ("Delete error, file cannot be deleted");
}



}


/**
* Operating Directory
*/
public static void Operfile () {

String Path = "D:\\myimg\\test";
String name = "3.jpg";

File File = new file (path,name);
if (File.mkdir ()) {//If there is no parent directory, you cannot create

}

if (File.mkdirs ()) {//If no parent directory is created together with parent directory

}
}


public static void Main (string[] args) {

}



}

The File class foundation for Java IO streams

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.