- package com.heima.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.util.Scanner;
- public class Test2_Key {
- /**
- * @param args
- *
- *
- * 分析:
- *
- * 1,定义方法对键盘录入的路径进行判断,如果是文件就返回
- * 2,在主方法中接收该文件
- * 3,读和写该文件
- * @throws FileNotFoundException
- */
- public static void main(String[] args) throws IOException {
- File file = getFile();
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
- int b;
- while ((b = bis.read()) != -1) {
- bos.write(b);
- }
- bis.close();
- bos.close();
- }
- public static File getFile () {
- 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 if (file.isDirectory()) {
- System.out.println("您输入的是一个文件夹路径,请重新输入:");
- }else {
- return file ;
- }
- }
- }
-
- }
复制代码 |
|