• 测试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

  • 相关阅读:
    工作也是一样,认真对待,你是在为自己工作
    程序员学习能力提升三要素(转载)
    该读些啥书
    每个程序员都应读的书
    微博时光机定时发送微博
    WordPress快速建站
    Tweenlite的用法
    Away3D粒子系统中文快速上手指南
    操盘手 李彪 照片[转]
    URLClassLoader加载class到当前线程类加载器【zt】
  • 原文地址:https://www.cnblogs.com/lpbk/p/11352717.html
Copyright © 2020-2023  润新知