• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

yxchun

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

读取文本文件中所有内容,java

1、使用Files.lines() 方法,逐行读取,然后拼接

        //        读取 
        String interfaceTxtPath="D:\\基础.postman_collection.json";
        
        List<String> lines = Files.readAllLines(Paths.get(interfaceTxtPath), StandardCharsets.UTF_8);
        String content= String.join(System.lineSeparator(),lines);

2、使用BufferedReader 逐行读取,然后以字符串存储

    public static void main(String[] args) throws IOException {
        String path = "D:\\基础.postman_collection.json"; 
        StringBuilder contentBuilder = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
            String line;
            while ((line = br.readLine()) != null) {
                contentBuilder.append(line).append(System.lineSeparator());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        String fileContent = contentBuilder.toString();
        System.out.println(fileContent);

    }

3、使用Files.readString()

 

posted on 2024-07-19 10:48  yxchun  阅读(180)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3