IO流--利用bufferedstream在控制台中输入文件路径,并将文件复制到桌面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40567229/article/details/86235309

class hello {

	public static void main(String[] args) throws IOException {				
		BufferedInputStream  input = new BufferedInputStream(new FileInputStream(InputPath()));
		BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Lee\\Desktop\\"+InputPath().getName()));
		
		int Byte;
		while((Byte =input.read()) != -1) {
			output.write(Byte);
		}
		
		input.close();
		output.close();
	}
	
	public static File InputPath() {
		Scanner sc = new Scanner(System.in);				
		System.out.println("输入一个文件的路径:");
		while(true) {
			String line = sc.nextLine();				
			File file = new File(line);						
			if(!file.exists()) {
				System.out.println("文件不存在,重新录入:");
			
			}else {
				return file;
			}
		}
	}

在控制台中输入文件路径: 

 

运行结果:

 

猜你喜欢

转载自blog.csdn.net/weixin_40567229/article/details/86235309