Java File operations (file), java File Operations file

Source: Internet
Author: User

Java File operations (file), java File Operations file

/** Create a file
* File is always on (File. separator (\), File. pathSeparator (;))
* In windows, you can use \ for segmentation.
* In linux, It is not.
* If we want to make our code more robust across platforms, we can use these two constants.
**/
Public static void createFileT (){
File file = new File ("D:" + File. separator + "IO" + File. separator + "file01.txt ");
// File file = new File ("D: \ IO \ file01.txt"); available in Windows
Try {
File. createNewFile ();
} Catch (IOException e ){
System. out. println ("IO exception ");
E. printStackTrace ();
}
}
/**
* Delete an object
**/
Public static void delectFile (){
File file = new File ("D:" + File. separator + "IO" + File. separator + "file01.txt ");
If (file. exists ()){
File. delete ();
}
Else {
System. out. println ("file does not exist ");
}
}
/**
* Create a folder
**/
Public static void createFileMix (){
File file = new File ("D:" + File. separator + "IO" + File. separator + "file01 ");
File. mkdir ();
}
/**
* Obtain all files in the specified directory (including hidden files ):
**/
Public static void getFile (){
File file = new File ("D:" + File. separator );
String [] str = file. list ();
For (int I = 0; I <str. length; I ++ ){
System. out. println (str [I]);
}
}
/**
* Determine whether the specified path is a directory
**/
Public static void FileDirectory (){
File file = new File ("D:" + File. separator + "IO ");
String str = file. isDirectory ()? "Yes": "no ";
System. out. println (str );
}
/**
* Search all contents of a specified directory
**/
Public static void print (File file ){
If (file! = Null ){
If (file. isDirectory ()){
File [] fileArray = file. listFiles ();
If (fileArray! = Null ){
For (int I = 0; I <fileArray. length; I ++ ){
Print (fileArray [I]);
}
}
}
Else {
System. out. println (file );
}
}
}

Related Article

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.