Custom I/O toolkit and I/O Toolkit

Source: Internet
Author: User

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

}

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.