package com.befery.oams.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class FileYmlUtil {
/**
* 创建文件
* @author zhangxuewei
* @throws IOException
*/
public static String creatYmlFile(String name, String path) throws IOException {
path = path + name + ".yml";
File filename = new File(path);
if (!filename.exists()) {
filename.createNewFile();
return name;
} else {
return name;
}
}
/**
* 写文件
* @author zhangxuewei
* @param newStr 新内容
* @throws IOException
*/
public static boolean writeYmlFile(String newStr, String path, String name) throws IOException {
// 先读取原有文件内容,然后进行写入操作
boolean flag = false;
// 读取本地文件
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
// 文件路径
File file = new File(path + name + ".yml");
// 将文件读入输入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 添加新的内容
buf.append(newStr);
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
flag = true;
} catch (IOException e1) {
// TODO 自动生成 catch 块
throw e1;
} finally {
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return flag;
}
/**
* 读文件
* @author zhangxuewei
* @param path
* @return
* @throws IOException
*/
public static String read(String path) throws IOException {
File file = new File(path + ".yml");
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
// 将文件读入输入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
String temp;
for (int j = 1; (temp = br.readLine()) != null; j++) {
buf = buf.append(temp);
// System.getProperty("line.separator")
// 行与行之间的分隔符 相当于“\n”
buf = buf.append(System.getProperty("line.separator"));
}
return buf.toString();
}
/**
* 删除服务上的文件
*
* @author zhangxuewei
* @param filePath 路径
* @param fileName 文件名
*/
public static boolean deleteServerFile(String filePath, String fileName) {
boolean delete_flag = false;
File file = new File(filePath + fileName + ".yml");
if (file.exists() && file.isFile() && file.delete()) {
delete_flag = true;
} else {
delete_flag = false;
}
return delete_flag;
}
}
下面引入网友的一个 拼接方法,其实上面的操作我也是参照他的方法来的,然后自己做了点改动 https://blog.csdn.net/u014801403/article/details/70227624
/**
* 写文件 【他这个相当于拼接】
*
* @param newStr
* 新内容
* @throws IOException
*/
public static boolean writeTxtFile(String newStr) throws IOException {
// 先读取原有文件内容,然后进行写入操作
boolean flag = false;
String filein = newStr + "\r\n";
String temp = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
// 文件路径
File file = new File(filenameTemp);
// 将文件读入输入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该文件原有的内容
for (int j = 1; (temp = br.readLine()) != null; j++) {
buf = buf.append(temp);
// System.getProperty("line.separator")
// 行与行之间的分隔符 相当于“\n”
buf = buf.append(System.getProperty("line.separator"));
}
buf.append(filein);
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
flag = true;
} catch (IOException e1) {
// TODO 自动生成 catch 块
throw e1;
} finally {
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return flag;
}
}