import java.io.FileInputStream;
import java.io.FileOutputStream;
//import org.apache.commons.lang.CharEncoding;
/**
* file读写工具类
*/
public class FileReadAndWrite {
public static void main(String[] args) {
// FileReadAndWrite.read("E:\\java-study\\src\\com\\streamTest\\fis2.txt");
// FileReadAndWrite.write("E:\\java-study\\src\\com\\streamTest\\fos.txt", "linge加油", WriteType.ADD);
FileReadAndWrite.copy("C:\\Users\\lucklin\\Desktop\\abc.png", "E:\\java-study\\src\\com\\streamTest\\abc.png");
File source = new File("..\\java_file");
File target = new File("..\\Desktop");
filesCopy(source, target);
}
/**
* @description 读取文件内容
* @param path 文件路径
*/
static String read(String path) {
String res = null;
try {
FileInputStream fis = new FileInputStream(path);
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b)) != -1) {
res = new String(b,0, len);
}
fis.close();
} catch(Exception e) {
System.out.println("文件读取异常:"+e.getMessage());
}
return res;
}
/**
* @description 写入文件内容
* @param path 文件路径
* @param content 写入内容
*/
static void write(String path, String content) {
try {
//new FileOutputStream在文件不存在的情况下会默认创建,所以虽然会抛出不存在异常但是不会阻塞后续执行,前提path要正确
FileOutputStream fos = new FileOutputStream(path);
byte[] b = content.getBytes();
// System.out.println(b.length);
// fos.write(b,0, b.length);//写入内存
fos.write(b);//写入内存
fos.flush();//写入硬盘
fos.close();
System.out.println("写入成功");
} catch (Exception e) {
System.out.println("文件写入异常:"+e.getMessage());
}
}
/**
* @description 写入文件内容
* @param path 文件路径
* @param content 写入内容
* @param type 写入方式
*/
static void write(String path, String content, WriteType type) {
String oldContent = read(path);
content = oldContent != null && type.equals(WriteType.ADD) ? oldContent + content : content;
write(path, content);
}
/**
* @description 复制文件
* @param source 文件路径
* @param target 目标文件路径
*/
// static void copy(String source, String target) {//new String()和String.getBytes的编码问题有时间再研究
// String content = read(source);
// if(content != null) write(target, content);
// }
static void copy(String source, String target) {
try {
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(target);
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fos.flush();
fis.close();
fos.close();
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* @description 递归拷贝文件及目录
* @param source 文件路径
* @param target 目标文件路径
*/
static void filesCopy(File source, File target) {
if(source.isFile()) {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(source);
String path = target.getAbsolutePath() + source.getAbsolutePath().substring(source.getAbsolutePath().indexOf("\\java_file"));
out = new FileOutputStream(path);
byte[] temp = new byte[1024 * 1024];
int num = 0;
while ((num = in.read(temp)) > -1) {
out.write(temp, 0, num);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(in != null) in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(out != null) out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}else{
for (File f : source.listFiles()) {
if(f.isDirectory()) {
String f_path = f.getAbsolutePath();
String dist_path = target.getAbsolutePath() + f_path.substring(f_path.indexOf("\\java_file"));
File new_file = new File(dist_path);
if(!new_file.exists()) new_file.mkdirs();
}
filesCopy(f, target);
};
}
}
}
enum WriteType{
// ADD,
// REPLACE
ADD("add", "追加写入"),
REPLACE("replace", "覆盖写入");
private final String TYPE;
private final String DESC;
WriteType(String type, String desc) {
TYPE = type;
DESC = desc;
}
}