Custom I/O toolkit and I/O Toolkit
Package com. jredu. week;
Import java. io. BufferedInputStream;
Import java. io. BufferedOutputStream;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Public class FileOUtil {
Public static BufferedInputStream bis = null;
Public static BufferedOutputStream bos = null;
Public static byte [] B = null;
Public static File file = null;
Public static int fileSize = 0;
Public static void main (String [] args ){
// Copy ("F:/demoio/copy1.txt", "F:/demoio/copy2.txt ");
Move ("F:/demoio/copy3.txt", "F:/copy1.txt ");
// Delete ("F:/demoio/qq.png ");
// Creat ("F:/ssss.txt ");
// File = new File ("F:/demoio ");
// Size (file );
// System. out. println ("the size of all files is" + (fileSize/1024 ));
// Log ("F:/demoio/abc ");
}
/**
* File copy
* @ Param file
*/
Public static void copy (String oldFile, String newFile ){
Try {
Bis = new BufferedInputStream (new FileInputStream (oldFile ));
B = new byte [bis. available ()];
Bis. read (B );
System. out. println ("Copied successfully ");
} Catch (FileNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
Try {
Bis. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Try {
Bos = new BufferedOutputStream (new FileOutputStream (newFile ));
Bos. write (B );
System. out. println ("written successfully ");
} Catch (FileNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
Try {
Bos. flush ();
Bos. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
/**
* File moving and cutting
*/
Public static void move (String oldFile, String newFile ){
File file = new File (oldFile );
If (file. renameTo (new File (newFile ))){
System. out. println ("file moved successfully! ");
} Else {
System. out. println ("moving failed! ");
}
}
/**
* Delete an object
*/
Public static void delete (String fileName ){
File = new File (fileName );
If (file. exists ()){
System. out. println (file. delete ()? "Deleted successfully! ":" Deletion failed ");
}
}
/**
* Create a file
*/
Public static void creat (String fileName ){
File = new File (fileName );
If (file. exists ()){
System. out. println ("the file already exists ");
} Else {
Try {
System. out. println (file. createNewFile ()? "Created successfully": "failed to create ");
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
/**
* File or folder size
*/
Public static void size (File file ){
File [] files = file. listFiles ();
For (File f: files ){
If (! F. isDirectory ()){
FileSize + = f. length (); // the file size is calculated instead of a directory.
} Else {
Size (f); // indicates the recursive call of the Directory itself.
}
}
}
/**
* File directory information display
*/
Public static void log (String f ){
File = new File (f );
System. out. println ("file directory name" + file. getParent ());
}
}