没有合适的资源?快使用搜索试试~ 我知道了~
1. 写文件 String filePath = System.getProperty(user.dir)+File.separator+files;//当前项目的运行目录/files,File.separator根据系统的不同,路径的分隔符也不同 String fileName = test.txt; File pathFile = new File(filePath); BufferedWriter out = null; //1:创建文件夹 if (!pathFile.exis
资源详情
资源评论
资源推荐
Java读文件写文件读文件写文件txt
1. 写文件
String filePath = System.getProperty("user.dir")+File.separator+"files";//当前项目的运行目录/files,File.separator根据系统的不同,路径的分隔符也不同
String fileName = "test.txt";
File pathFile = new File(filePath);
BufferedWriter out = null;
//1:创建文件夹
if (!pathFile.exists()) {
pathFile.mkdirs();
}
String relFilePath = filePath + File.separator + fileName;
File file = new File(relFilePath);
//2:创建文件
if (!file.exists()) {
file.createNewFile();
}
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "GBK"));
String value = "this is a test !"
//3:写入文件
out.write(value);
out.newLine();
//4:关闭流
out.close();
2. 读文件
String json = "";
String base = "D:\workspace\zgs\json.json";
BufferedReader br = new BufferedReader(new FileReader(base));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null){
sb.append(line);
}
br.close();
json = sb.toString();
3. 递归删除文件夹及文件夹内的字文件夹
public void deleteFile(File file){
//判断文件不为null或文件目录存在
if (file == null || !file.exists()){
System.out.println("文件删除失败,请检查文件路径是否正确");
weixin_38576811
- 粉丝: 5
- 资源: 890
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- tensorflow-gpu-2.7.4-cp37-cp37m-manylinux2010-x86-64.whl
- 多段线、 圆、弧转多段线(仅我可见)
- tensorflow-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- yeyue-p8Yi4-ve4a83792.apk
- tensorflow-gpu-2.7.3-cp38-cp38-manylinux2010-x86-64.whl
- 五相感应电机矢量控制模型MATLAB
- RGLED (1) (1).circ
- IMG_20240427_215747.jpg
- python下前端WEB学习笔记
- 田间种植行排号自动生成工具
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0