operation on Io stream (file size, copy, move, delete)

Source: Internet
Author: User

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.SequenceInputStream;

Class Ljyfileclass {

/*ljyfileclass Tool class use need to know:

*
* 1. Calculate file Size: Long size = getfilesize (file file)
* File: Pass in a path, calculate all the file size in the folder,
* The return value is a long-type number of bytes.
*
*
* 2. Copy folder: Getcopyfile (File filepath,file Newfilepath)
* FilePath: The file path to be copied,
* Newfilepath: The path for storing files
* Only the path to be copied and a stored path are required for use.
*
*
* 3. Byte stream copy picture, video, audio and other files: getcopyfilecontent (file File,file file2)
* File: path of files to be copied
* File2: Storage path
* Byte stream for copying pictures, video, audio and other files that do not need decoding,
* Only the path to be copied and a stored path are required for use.
*
*
* 4. Character stream Copy file: Getfileread (file file, file file2)
* File: path of files to be copied
* File2: Storage path
* Character stream is suitable for copying some files that need decoding operation, but cannot use character stream to copy pictures, audio,
* Video and so on. Using a character stream may cause the file to be lost. And not open normally.
*
*
* 5.MergeFile (file file, file File2,file file3): Merge two files
* File: The path of files that need to be merged
* File2: File paths that need to be merged
* File3: The file path after merging
*
* This method can be used to splice two files and return a new file.
*
*
* 6.MoveFileToNewPath (file file, file file2): Move the target file to the specified path
* File: path of files that need to be moved
* File2: Receive the file path moved over
* This method can move a folder to the specified path and delete the original file.
*
*
* 7.getDeleteFile (file file): Delete entire folder
* File: The folder path to delete
* This method can be used to delete the whole folder.
*
*
*
* Method Summary:
* 1.getFileSize (file file) return size
* 2.getCopyFile (File filepath,file newfilepath) Copy Folder
* 3.getCopyFileContent (file file,file file2) byte stream copy picture, video, audio and other files
* 4.getFileRead (file file, file file2) character stream copy file
* 5.MergeFile (file file, file File2,file file3) merge two files
* 6.MoveFileToNewPath (file file, file file2) to move the target file to the specified path
* 7.getDeleteFile (file file) Delete entire folder
*/

To get the file size, simply pass the argument of a file path, the return value is the file size (long), and the return value represents only the file's byte size.
Public long getfilesize (file file) {
Long fileSize = 0;
file[] files = file.listfiles ();

if (File.isfile ()) {
return File.length ();
}
if (file! = null) {
for (File file2:files) {
FileSize + = GetFileSize (file2);
}
}
return fileSize;
}


Copy a folder to another folder
public void Getcopyfile (File filepath,file newfilepath) throws ioexception{
file[] files = filepath.listfiles ();
File tempfile = null;
if (! ( Newfilepath.isdirectory ())) {
Newfilepath.mkdir ();
}
if (FilePath! = null) {
for (File file:files) {
if (File.isdirectory ()) {
String string = File.getname ();
Tempfile = new File (newfilepath+ "\ \" +string);
Tempfile.mkdir ();
Getcopyfile (file, tempfile);

}else if (File.isfile ()) {
String string = File.getname ();
Tempfile = new File (newfilepath+ "\ \" +string);
Tempfile.createnewfile ();
Getcopyfilecontent (File,tempfile);
}
}
}

}

Copy images, videos, audio, etc. without decoding the files
public void Getcopyfilecontent (File file,file file2) throws ioexception{

FileInputStream InputStream = new FileInputStream (file);
FileOutputStream outputstream = new FileOutputStream (file2);

Byte[] B = new byte[1024];
int count = 0;
while ((count = Inputstream.read (b))! =-1) {
Outputstream.write (B,0,count);
}
Inputstream.close ();
Outputstream.close ();
}

Use character stream to copy, when using this method to copy pictures, videos, audio and other files, these files will not be opened.
public void Getfileread (file file, file File2) throws IOException {
FileReader FileReader = new FileReader (file);
FileWriter FileWriter = new FileWriter (file2);
Char[] B = new char[1024];
int count = 0;
while ((count = Filereader.read (b))! =-1) {
Filewriter.write (B,0,count);
}

Filewriter.close ();
Filereader.close ();
}

Merging files
public void Mergefile (file file, file file2, file file3) throws IOException {

FileInputStream FileInputStream = new FileInputStream (file);
FileInputStream fileInputStream2 = new FileInputStream (file2);
FileOutputStream outputstream = new FileOutputStream (FILE3);
Sequenceinputstream InputStream = new Sequenceinputstream (FileInputStream, fileInputStream2);

Byte[] b = new byte[1024];
while (Inputstream.read (b)! =-1) {
Outputstream.write (b);
}
Outputstream.close ();
Inputstream.close ();
}

//Move files to the new path file: path to move file2: Receive moved path
public void Movefiletonewpath (file file, file file2) throws ioexception{
File File3 = new file (File2.getpath () + "\ \" + file.getname ());
Copies the file to the specified path
getcopyfile (file, file3);
Empty the original folder and delete
Getdeletefile (file);
}

//Recursively delete entire folder
public void getdeletefile (file file) {

file[] files = file.listfiles ();
if (file! = null) {
for (File file2:files) {
if (File2.isdirectory ()) {
Getdeletefile (file2);
}else {
File2.delete ();
}
}
}
File.delete ();
}


Cut files
public void Shearfile () {

}

}

public class Demo {

/**
* 1. Create a file
* 2. Create a folder
* 3. Find with End
* 4. Determine if it is a file
* 5. Determine if it is a folder
* 6.
*
*/
public static void Main (string[] args) throws IOException {
Ljyfileclass Ljyfileclass = new Ljyfileclass ();

File File = new file ("c:\\users\\pc-luo\\desktop\\ new Folder \\1.jpg");
File File2 = new file ("c:\\users\\pc-luo\\desktop\\ new folder \ \ New Folder \\1.jpg");
//
Ljyfileclass.getcopyfilecontent (file, file2);

File File3 = new file ("c:\\1111");
File3.mkdir ();
}

}

operation on Io stream (file size, copy, move, delete)

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.