定义读取与写入文件的路径

     File classesFile = ResourceUtils.getFile("classpath:");if (!classesFile.exists()){System.out.println("spring获取classesFile不存在");classesFile = new File("");}String classesFilePath = classesFile.getAbsolutePath();String folder = "testio";String srcPath_string = Paths.get(classesFilePath, folder, "john.txt").toFile().getAbsolutePath();Path srcPath_path = Paths.get(classesFilePath, folder, "john.txt");File file = Paths.get(classesFilePath, folder, "writer").toFile();if (!file.exists()) System.out.println("mkdir:"+file.mkdir());System.out.println("file:"+file);File file1 = Paths.get(classesFilePath, folder, "writer", "buffered.txt").toFile();if (!file1.exists()) System.out.println("createNewFile-file1:"+file1.createNewFile());String path1_string = file1.getAbsolutePath();File file2 = Paths.get(classesFilePath, folder, "writer", "closable.txt").toFile();if (!file2.exists()) System.out.println("createNewFile-file2:"+file2.createNewFile());String path2_string = file2.getAbsolutePath();Path path3_path = Paths.get(classesFilePath, folder, "writer", "closable+newBuffered.txt");File file3 = path3_path.toFile();if (!file3.exists()) System.out.println("createNewFile-file3:"+file3.createNewFile());Path path4_path = Paths.get(classesFilePath, folder, "writer", "filesBytes.txt");File file4 = path4_path.toFile();if (!file4.exists()) System.out.println("createNewFile-file4:"+file4.createNewFile());Path path5_path = Paths.get(classesFilePath, folder, "writer", "filesString.txt");File file5 = path5_path.toFile();if (!file5.exists()) System.out.println("createNewFile-file5:"+file5.createNewFile());

方式一 BufferedReader/BufferedWriter

     StringBuilder sb = new StringBuilder();BufferedReader br = null;BufferedWriter bw = null;try {br = new BufferedReader(new InputStreamReader(new FileInputStream(srcPath_string),"UTF-8"));bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path1_string),"UTF-8"));String buffer;while((buffer = br.readLine()) != null){
//          sb.append(buffer);
//          sb.append("\r\n");bw.write(buffer);bw.newLine();bw.flush();}} finally {if(br != null)br.close();if(bw != null)bw.close();}

方式二 closable 条件种的resource自动关闭

     try(BufferedReader br2 = new BufferedReader(new FileReader(srcPath_string));BufferedWriter bw2 = new BufferedWriter(new FileWriter(path2_string))){String buffer;while((buffer = br2.readLine()) != null){bw2.write(buffer);bw2.newLine();bw2.flush();}}

方式三 closeable + new BufferedWriter/new BufferdReader

     try(BufferedWriter bw3 = Files.newBufferedWriter(path3_path);BufferedReader br3 = Files.newBufferedReader(srcPath_path)){String buffer = null;while((buffer = br3.readLine()) != null){bw3.write(buffer);bw3.newLine();bw3.flush();}}

方式四 filesBytes

     byte[] src_bytes = Files.readAllBytes(srcPath_path);Files.write(path4_path, src_bytes,StandardOpenOption.CREATE);

方式5 filesString

     List<String> src_list = Files.readAllLines(srcPath_path);Files.write(path5_path, src_list, StandardOpenOption.APPEND);

[备忘]java读取与写入文件的五种方式相关推荐

  1. java中写入文件_java中创建、写入文件的5种方式

    在java中有很多的方法可以创建文件写文件,你是否真的认真的总结过?下面笔者就帮大家总结一下java中创建文件的五种方法. Files.newBufferedWriter(Java 8) Files. ...

  2. Python实现将内容写入文件的五种方法总结

    本篇带你详细看一下python将内容写入文件的方法以及细节,主要包括write()方法.writelines() 方法.print() 函数.使用 csv 模块.使用 json 模块,需要的可以参考一 ...

  3. Java File.createNewFile 创建文件的四种方式小笔记

    本文为joshua317原创文章,转载请注明:转载自joshua317博客 Java File.createNewFile 创建文件的四种方式小笔记 - joshua317的博客 1.File(Str ...

  4. java中读取properties文件内容五种方式

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  5. java加载properties文件的几种方式,java高级面试笔试题

    我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家. 扫描二维码或搜索下图红色VX号,加VX好友,拉你进[程序员面试学习交流群]免费领取.也欢迎各位一起 ...

  6. java List去除重复数据的五种方式

    以下介绍五种-不同的方法去除Java中ArrayList中的重复数据 1.使用LinkedHashSet删除arraylist中的重复数据 **LinkedHashSet**是在一个ArrayList ...

  7. springboot读取linux文件_SpringBoot读取Resource下文件的几种方式

    最近在项目中涉及到Excle的导入功能,通常是我们定义完模板供用户下载,用户按照模板填写完后上传:这里待下载模板位置为resource/excelTemplate/test.xlsx,尝试了四种读取方 ...

  8. java加载资源文件的3种方式

    使用绝对路径加载(不推荐) 直接写死路径,使用FileInputStream加载资源文件,但是路径就不能动了 public static void main(String[] args) throws ...

  9. 将字符串写入文件的五种方法

    public void WriteStringToFile(String filePath) {try {File file = new File(filePath);PrintStream ps = ...

  10. Java解压RAR文件的几种方式

    第一种: public class fileZipUtil {/*** zip文件解压* @param inputFile 待解压文件夹/文件* @param destDirPath 解压路径*/pu ...

最新文章

  1. 算法 - 快速排序(C#)
  2. python3 32位_Python3.8.2安装教程
  3. Spark(2)——小用RDD
  4. 移动wabAPP 开发 viewport 注意事项
  5. 小白配置QConf遇到的问题
  6. Unix安装BerkeleyDB
  7. 都是宝宝:北京孩子3成不玩电子游戏睡眠状况最好 江苏孩子起得最早
  8. 数据结构笔记-----链表
  9. PHP—通过HTML网页请求,PHP页面显示源码不能解析
  10. FPGA实现BP神经网络-原理
  11. Java编程入门:使用Eclipse快捷键
  12. javascript 忽略 报错_JavaScript数据类型中易被忽略的点
  13. lodop打印html上下居中,lodop打印横向纵向
  14. 自定义View之案列篇(三):仿QQ小红点
  15. 借助ChatGPT自动生成PPT
  16. .browser的解释
  17. codewars练习(5)backwardsPrime--提交失败-Execution Timed Out (12000 ms)
  18. Lightroom导入JPG格式的图像
  19. 关于gyp ERR node-gyp gyp ERR node -v错误记录
  20. idea导出oracle完整_idea 导出数据库表

热门文章

  1. iOS底层探索之类的加载(二): realizeClassWithoutSwift分析
  2. oracle 体系结构及内存管理 14_锁
  3. bootstrap-引用-命名来源
  4. 算法(0)—— 打造一个C开发库
  5. UISearchController使用方法及注意事项
  6. c#中用声音提示报警
  7. CentOS下安装beanstalkd服务
  8. jQuery 使用收集
  9. 「leetcode」35.搜索插入位置:每次遇到二分法,都是一看就会,一写就废
  10. 如何在 MacBook Pro 上调整显示设置?