测试4--创建文件夹,在文件夹里创建文件并复制

 1 package com.review;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 
 9 /**
10  * @program: com.review
11  * @description:
12  * @author: Mr.Lin
13  * @create: 2019年8月14日
14  **/
15 public class Xcopy04 {
16     public static void main(String[] args) {
17         //创建目录
18         File f = new File("c:/javabigdata");
19         //判断目录是否存在
20         if(f.exists()) {
21             System.out.println("目录已存在");            
22         }else {
23             //若不存在,这创建一个并提示已创建
24             f.mkdir();
25             System.out.println("目录已创建");
26         }
27         //在创建完成的目录下创建一个a.txt
28         FileOutputStream fos = null;
29         //要输出的内容
30         String content = "I love china,I love beiijng,the Beijing is the capatial of China.";
31         //要输出的内容装换为byte数组
32         byte b[] = content.getBytes();
33         try {
34             //明确输出目标
35             fos = new FileOutputStream("c:/javabigdata/a.txt");    
36             //输出数据
37             fos.write(b);
38             System.out.println("内容输出成功");
39         }catch (FileNotFoundException e) {
40             e.printStackTrace();
41         }catch (IOException e) {
42             e.printStackTrace();
43         }
44         try {
45             //关闭输出流
46             fos.close();
47         }catch (IOException e) {
48             e.printStackTrace();
49         }
50         //按题目复制这个刚创建的txt文档
51         FileInputStream fis = null;
52         try {
53             //明确被复制文件的位置和复制目的地和文件名
54             fis = new FileInputStream("c:/javabigdata/a.txt");
55             fos = new FileOutputStream("c:/javabigdata/b.txt");
56             //根据语法规则设置转移变量numa
57             int num = 0;
58             //根据read()方法,如果读到不为-1的时候,表明读完
59             while ((num = fis.read()) !=-1){
60                 //写入文件
61                 fos.write(num);
62             }
63             System.out.println("文件复制完毕");
64         }catch (FileNotFoundException e) {
65             e.printStackTrace();
66         }catch (IOException e) {
67             e.printStackTrace();
68         }
69         try {
70              //关闭输入输出流
71             fos.close();
72             fis.close();
73         }catch (IOException e) {
74             e.printStackTrace();
75         }
76     }
77 
78 }
View Code

 

 

转载于:https://www.cnblogs.com/lpbk/p/11352717.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值