|--需求说明
|--实现思路
见代码注释
|--代码内容
1 import java.io.*; 2 3 /** 4 * @auther::9527 5 * @Description: 第四题 6 * @program: 多线程 7 * @create: 2019-08-09 19:46 8 */ 9 public class Fourth { 10 public static void main(String[] args) { 11 //创建目录 12 File f = new File("c:/javabigdata"); 13 //判断目录是否存在,如果存在就提示已存在 14 if (f.exists()) { 15 System.out.println("目录已经存在"); 16 } else { 17 //如果不存在,就创建一个并提示已创建 18 f.mkdir(); 19 System.out.println("目录已创建"); 20 } 21 //在已经创建OK的文件夹下面创建一个文件a.txt 22 FileOutputStream fos = null; 23 //要输出的内容 24 String wantToWrite = "I love china,I love beiijng,the Beijing is the capatial of China."; 25 //将要输出的内容转换为byte数组 26 byte [] b = wantToWrite.getBytes(); 27 try { 28 //明确输出目标 29 fos = new FileOutputStream("c:/javabigdata/a.txt"); 30 //输出数据 31 fos.write(b); 32 System.out.println("内容输出成功"); 33 } catch (FileNotFoundException e) { 34 e.printStackTrace(); 35 } catch (IOException e) { 36 e.printStackTrace(); 37 } 38 try { 39 //关闭输出流 40 fos.close(); 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } 44 //按题目复制这个刚创建的txt文档 45 FileInputStream fis = null; 46 try { 47 //明确被复制文件的位置和复制目的地和文件名 48 fis = new FileInputStream("c:/javabigdata/a.txt"); 49 fos = new FileOutputStream("c:/javabigdata/b.txt"); 50 //根据语法规则设置转移变量numa 51 int num = 0; 52 //根据read()方法,如果读到不为-1的时候,表明读完 53 while ((num = fis.read()) !=-1){ 54 //写入文件 55 fos.write(num); 56 } 57 System.out.println("文件复制完毕"); 58 } catch (FileNotFoundException e) { 59 e.printStackTrace(); 60 } catch (IOException e) { 61 e.printStackTrace(); 62 } 63 try { 64 //关闭输入输出流 65 fos.close(); 66 fis.close(); 67 } catch (IOException e) { 68 e.printStackTrace(); 69 } 70 } 71 }
|--运行结果