Java implements file copy, paste, and cut based on eclipse

Java implements file copy, paste, and cut based on eclipse

Hello, everyone, I am Xiaoyuer. Yanxiao, I am very honored to come to my blog. Recently, I was on winter vacation. I was a little bit painful when I was idle, and I returned to the learning state. I shared a wave of code today. If you like this article, remember the three waves, and everyone is welcome to speak. Not much nonsense and walk away.

Preface

Article java code version "1.8.0_202"
This code uses java cache stream and byte stream


Tip: The following is the content of this article, the following cases are for reference

Complete code:

package chaper13.test;
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;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * @作者 YanXiaolxy
 * @版本 2020.03
 * @时间 2021年1月18日 下午10:57:28
 */
public class CopyDemo {
    
    
	//文件路径
    private static final String FilePath1 =  "E:/sounds/theSpectre.mp3";
    private static final String FilePath2 =  "C:/Users/Administrator/Desktop/theSpectre的复制文件.mp3";
	public static void main(String[] args) throws IOException {
    
    
		long time = CopyFileByBufferedStream();
		System.out.println("拷贝用时: " + time + "毫秒");
	}
	/**
	 * 使用缓存流拷贝文件
	 * @return 拷贝过程的用时(毫秒)
	 */
	private static long CopyFileByBufferedStream() {
    
    
		File file = new File(FilePath1);//要复制的文件
		//删除,实现剪切
//		if (file.exists()) {
    
    
//			file.delete();      //立即删除文件或目录
//			file.deleteOnExit();//在程序结束的时候将文件删除,进程没有结束时新创建的文件都会被删除
//			System.out.println("文件删除成功!");
//		}
		if (!file.exists()) {
    
    
			System.out.println("文件不存在!");
			return 0;
		}
		long startTime = System.currentTimeMillis();
		InputStream inStream = null;
		BufferedInputStream bInStream = null;
		OutputStream outStream = null;
		BufferedOutputStream bOutStream = null;
		try {
    
    
			inStream = new FileInputStream(file);
			bInStream = new BufferedInputStream(inStream);
			outStream = new FileOutputStream(FilePath2);
			bOutStream = new BufferedOutputStream(outStream);
			int i = -1;//读取出的临时变量
			while ((i = bInStream.read())!= -1) {
    
    
				bOutStream.write(i);//注意:读取一个字节,就写入到了缓存中!
			}
			System.out.println("文件复制成功!");
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			try {
    
    
				//全部关一下
				//close()内部已经自动调用了flush()方法,直接调用close()方法
				bOutStream.close();//需要在最后,将缓存中的内容写入到文件中,即清空缓冲区-flush()方法
				outStream.close();
				bInStream.close();
				inStream.close();//怎么打开的就反着关
			} catch (IOException e) {
    
    
				e.printStackTrace();
			}
		}
		long endTime = System.currentTimeMillis();
		return endTime - startTime;
	}
}

Code interpretation:

Definition file copy and paste path (copy path):

private static final String FilePath1 =  "E:/sounds/theSpectre.mp3";
    private static final String FilePath2 =  "C:/Users/Administrator/Desktop/theSpectre的复制文件.mp3";

Copy timing code block:

public static void main(String[] args) throws IOException {
    
    
		long time = CopyFileByBufferedStream();
		System.out.println("拷贝用时: " + time + "毫秒");
	}

Read to judge the copy result:

int i = -1;//读取出的临时变量
			while ((i = bInStream.read())!= -1) {
    
    
				bOutStream.write(i);//注意:读取一个字节,就写入到了缓存中!
			}
			System.out.println("文件复制成功!");

Paste and rename:

outStream = new FileOutputStream(FilePath2);

Close resource:

bOutStream.close();
outStream.close();
bInStream.close();
inStream.close();

Catch and throw exception code block:

} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			try {
    
    
				//全部关一下
				//close()内部已经自动调用了flush()方法,直接调用close()方法
				bOutStream.close();//需要在最后,将缓存中的内容写入到文件中,即清空缓冲区-flush()方法
				outStream.close();
				bInStream.close();
				inStream.close();//怎么打开的就反着关
			} catch (IOException e) {
    
    
				e.printStackTrace();
			}
		}

Cut the code block:

//		if (file.exists()) {
    
    
//			file.delete();      //立即删除文件或目录
//			file.deleteOnExit();//在程序结束的时候将文件删除,进程没有结束时新创建的文件都会被删除
//			System.out.println("文件删除成功!");
//		}

to sum up

Nothing to do during the holidays, it is better to look at the code.
Programming knowledge is not a mountain, you can read it like a dick.
Although the code is cumbersome, javaC++ makes a big difference.
Uncertain construction is not to move bricks.

Excuse me, goodbye.

Guess you like

Origin blog.csdn.net/yanxiaolxy/article/details/112797949