• java读取本地文件内容TXT文件


    本地新建多个文本并分别写入内容,如下:

    代码:

    package com.test.controller;
    
    import java.io.*;
    
    /**
     * 报文批量推送
     *
     * @author liuwenlong
     * @create 2021-11-30 21:01:26
     */
    @SuppressWarnings("all")
    public class BatchPacketPush {
    
    
        public static void main(String[] args) {
            try {
    
                //获取绝对路径下的文件
                for (int i = 1; i <= 3; i++) {
                    String s1 = BatchPacketPush.readTxt(new File("D:\\报文推送\\" + i + ".txt"));
                    System.out.println(s1);
                    Thread.sleep(5000);  //5000毫秒就是5秒
                }
    
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * 读文件
         *
         * @param file 文件
         * @return
         * @throws IOException
         * @throws IOException
         */
        public static String readTxt(File file) throws IOException, IOException {
            String s = "";
            InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8");
            BufferedReader br = new BufferedReader(in);
            StringBuffer content = new StringBuffer();
            while ((s = br.readLine()) != null) {
                content = content.append(s);
            }
            return content.toString();
        }
    }

    运行效果

    原创文章,转载请说明出处,谢谢合作
  • 相关阅读:
    19-background
    18-超链接导航栏案例
    17-文本属性和字体属性
    16-margin的用法
    15-浮动
    14-块级元素和行内元素
    13-标准文档流
    12-简单认识下margin
    11-border(边框)
    10-padding(内边距)
  • 原文地址:https://www.cnblogs.com/lwl80/p/15626299.html
Copyright © 2020-2023  润新知