File 工具类的常用方法
创建文件
判断文件夹是否存在,不存在则创建
1 | File file = new File(path); |
读取文件
1 | File file = new File(filePath); |
写文件
追加
1
2
3
4
5
6
7
8
9try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
writer.write(content + "\t\n");
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}覆盖
1
2
3
4
5
6
7
8
9try {
File file = new File("src/main/resources/application.properties");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
writer.write(text);
writer.close();
return null;
} catch (Exception e) {
e.printStackTrace();
}
文件大小
1 | readableFileSize(file.length()) |
获取属性值
1 | public static String getProperty(String configProperties, String key) { |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 颜不喜!