打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java 创建文件夹以及文本文件

package com.softeem.j2se.io;

//在D盘新建一个文件夹stream
//在文件夹中新建三个文本文件
//第一个( a.txt )内容为“java”
//第二个( b.txt )内容为“学习”
//将第一个文本文件的内容和第二个文本文件的内容复制到第三个文本文件(c.txt)中
//将第三个文本文件拷贝一份(d.txt)到当前目录中
//将第四个文本文件(d.txt)在控制台打印出
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.util.Date;

public class IAndO {
/**
* 无参构造器
*/
IAndO() {
};

/**
* 创建文件夹
*/
public void creatFolder(String path) {
   File dirFile;
   try {
    dirFile = new File(path);
    if (dirFile.exists() == true) {
     System.out.println("The folder exists.");
    } else {
     System.out
       .println("The folder do not exist,now trying to create a one...");
     if (dirFile.mkdir() == true) {
      System.out.println("Create successfully!");
     } else {
      System.out
        .println("Disable to make the folder,please check the disk is full or not.");
      System.exit(1);
     }
    }
    System.out.println("Now we put files in to the folder...");
   } catch (Exception e) {
    e.printStackTrace();
   }

}

/**
* 创建文本文件

* @param txtname
*/
public void creatTxt(String path, String txtname) {
   File txt = new File(path + txtname);
   if (!txt.exists()) {
    try {
     txt.createNewFile();
     System.out.println(txtname+"文本文件创建成功");
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
}

/**
* 编写文本文件的内容

* @param file
*            文本文件名字
* @param str
*            输入内容
*/
public void editTxtContain(File file, String str) {
   try {
    OutputStream txt = new FileOutputStream(file);
    txt.write(str.getBytes());
    txt.close();
    System.out.println("内容添加成功");
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }

}

/**
* @param fi1
* @param fi2
* @param fo3
*            将第一个文本文件的内容和第二个文本文件的内容复制到第三个文本文件中
*/
public void combinationTxt(File fi1, File fi2, File fo3) {

   try {
    InputStream f1 = new FileInputStream(fi1);
    InputStream f2 = new FileInputStream(fi2);
    OutputStream f3 = new FileOutputStream(fo3);
    byte[] buff = new byte[1024];
    int len = 0;
    while ((len = f1.read(buff)) != -1) {
     f3.write(buff, 0, len);// 0控制起始位置,len控制长度
    }
    f1.close();
    while ((len = f2.read(buff)) != -1) {
     f3.write(buff, 0, len);
    }
    f2.close();
    System.out.println("文件合并成功");
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
}

/**
* @param srcPath
* @param targetPath
*            文件备份
*/
public void copyTxt(File srcPath, File targetPath) {
   InputStream is = null;
   OutputStream os = null;
   byte[] buff = new byte[100];
   int len = 0;
   try {
    is = new FileInputStream(srcPath);
    os = new FileOutputStream(targetPath);
    while ((len = is.read(buff)) != -1) {
     os.write(buff);
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   try {
    is.close();
    os.close();
    System.out.println("文件复制成功");
   } catch (IOException e) {
    e.printStackTrace();
   }
}

@SuppressWarnings("deprecation")
public static void main(String[] args) {
   String path = "D:/stream";
   String txtname1 = "a.txt";
   String txtname2 = "b.txt";
   String txtname3 = "c.txt";
   String txtname4 = "d.txt";
   Date cal=new Date();
   IAndO test = new IAndO();
   // 创建文件夹
   test.creatFolder(path);
   // 创建文件
   test.creatTxt(path +"/", txtname1);
   test.creatTxt(path +"/", txtname2);
   // 写入文件内容
//  
   String str1 = "java \n 123"+"\r"+cal.getYear()+" "+cal.getMonth()+" "+cal.getDate();
   String str2 = "learn";
   test.editTxtContain(new File(path +"/"+ txtname1), str1);
   test.editTxtContain(new File(path +"/"+ txtname2), str2);
   // 复制文件内容
   test.combinationTxt(new File(path +"/"+ txtname1),
     new File(path +"/"+ txtname2), new File(path +"/"+ txtname3));
   // 复制文件
   test.copyTxt(new File(path + "/"+txtname3), new File(path + "/"+txtname4));

}
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java实现文档在线预览(openoffice+swfTools+FlexPaper)
文件夹操作及zip的子文件读取,解压-文件下载
如何使用装饰设计模式读取指定路径下的之纯文本文件的实现代码
JAVA 如何创建\删除\修改\复制目录及文件
java读写文件
Java操作文件、目录
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服