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));
}
}
联系客服