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 );
}
}
}