class FileEntry {
/**
* 读取文件的所有内容
* @param filePath 文件路径
* @param charset 编码
* @return 文件的全部内容,作为一个字符串返回
*/
public static String readFile(String filePath, String charset) {
File file = new File(filePath);
Long filelength = file.length();
byte[] filecontent = new byte[filelength.intValue()];
FileInputStream in = null;
String result = null;
try {
in = new FileInputStream(file);
in.read(filecontent);
result = new String(filecontent, charset);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
} finally {
try {
if (null != in)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 读取文件内容
* @param filePath 文件路径
* @param charset 编码
* @return 文件的每一行
*/
public static ArrayList<String> readFileLines(String filePath, String charset) {
BufferedReader reader = null;
ArrayList<String> lines = new ArrayList<String>();
try {
FileInputStream fis = new FileInputStream(filePath);
InputStreamReader isr = new InputStreamReader(fis, charset);
reader = new BufferedReader(isr);
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
lines.add(tempString);
}
reader.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return lines;
}
/**
* 获取指定文件夹下的所有文件名称(递归)
* @param folderPath 文件夹路径
* @return 每个文件的绝对路径
*/
public static ArrayList<String> getFolderFiles(String folderPath) {
ArrayList<String> ret = new ArrayList<String>();
File fileObject = new File(folderPath);
if (fileObject.isFile())
ret.add(fileObject.getAbsolutePath());
else {
for (String subFilePath: fileObject.list()) {
ret.addAll(getFolderFiles(folderPath + File.separatorChar + subFilePath));
}
}
return ret;
}
/**
* 将指定内容写入文件中
* @param filePath 文件路径
* @param content 待写入内容
* @param charset 编码
*/
public static void writeFile(String filePath, String content, String charset) {
BufferedWriter bw = null;
try {
File file = new File(filePath);
file.getParentFile().mkdirs();
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), charset));
bw.write(content);
bw.flush();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (null != bw)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 在文件尾追加内容
* @param content 待追加文本
* @param fileDes 文件路径
* @param charset 编码
*/
public static void appendToFile(String filePath, String content, String charset) {
BufferedWriter bw = null;
try {
File file = new File(filePath);
file.getParentFile().mkdirs();
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), charset));
bw.write(content);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != bw)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
JAVA-文件读写模板
最新推荐文章于 2023-05-18 18:19:29 发布