• java读取txt文件


    描述:

    1.java读取指定txt文件并解析

      文件格式:

       代码:

    package com.thinkgem.wlw.modules.midea;
    
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Author: zhouhe
     * @Date: 2019/6/19 8:48
     */
    public class Test {
        public static void main(String[] args)  {
            // 文件夹路径
            String path = "D:\input.txt";
            try {
                List<String> scanListPath = readFile02(path);
    //            System.out.println(scanListPath);
                for (int i = 0; i < scanListPath.size(); i++) {
                    String mytext = scanListPath.get(i);
    
                    //替换所有制表符
                    mytext = mytext.replaceAll("	",",");
                    System.out.println(mytext);
                    //每一行都转化为新的数组,根据下标去判断参数值对应的参数是什么
                    String [] strArr= mytext.split(","); //注意分隔符是需要转译
                    for (int m = 0; m < strArr.length; m++) {
    //                    System.out.println(strArr[m]);
                        switch(m){
                            case 0:
                                System.out.println("时间:"+strArr[m]);
                                break;
                            case 1:
                                System.out.println("甲烷:"+strArr[m]);
                                break;
                            case 2:
                                System.out.println("总烃:"+strArr[m]);
                                break;
                            case 3:
                                System.out.println("非甲烷总烃:"+strArr[m]);
                                break;
                            case 4:
                                System.out.println("氨气:"+strArr[m]);
                                break;
                            case 5:
                                System.out.println("硫化氢:"+strArr[m]);
                                break;
                            case 6:
                                System.out.println("氧气:"+strArr[m]);
                                break;
                            default:
                                break;
                        }
                    }
                }
            } catch (IOException e) {
                System.out.println("有异常,无法读取!!!");
            }
        }
    
        /**
         * 读取一个文本 一行一行读取
         *
         * @param path
         * @return
         * @throws IOException
         */
        public static List<String> readFile02(String path) throws IOException {
            // 使用一个字符串集合来存储文本中的路径 ,也可用String []数组
            List<String> list = new ArrayList<String>();
            FileInputStream fis = new FileInputStream(path);
            // 防止路径乱码   如果utf-8 乱码  改GBK     eclipse里创建的txt  用UTF-8,在电脑上自己创建的txt  用GBK
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line = "";
            while ((line = br.readLine()) != null) {
                // 如果 t x t文件里的路径 不包含---字符串       这里是对里面的内容进行一个筛选
                if (line.lastIndexOf("---") < 0) {
                    list.add(line);
                }
            }
            br.close();
            isr.close();
            fis.close();
            return list;
        }
    }

      结果:

     

     2.java读取指定文件夹下的所有txt文件并输出内容(我这里一个文件夹下面有 2 个txt文件):

       代码:

    package com.thinkgem.wlw.modules.midea;
    
    import java.io.*;
    
    /**
     * @Author zhouhe
     * @Date 2019/10/10 13:10
     */
    public class Test2 {
    
    
        /**新建一个类把下面代码放进去,注意要设置basePath(你要读取的文件夹),读取和写入的方法也都写好了.你可以根据自己的需求掉用就行了**/
        static String basePath="D:\测试";
        /**
       * 查找文件夹下所有符合csv的文件
       *
       * @param dir 要查找的文件夹对象
       * */
        public static void findFile(File dir) throws IOException {
            File[] dirFiles = dir.listFiles();
            for(File temp : dirFiles){
                if(!temp.isFile()){
                    findFile(temp);
                }
            //查找指定的文件
            if(temp.isFile() && temp.getAbsolutePath().endsWith(".txt") ){
                    //获取文件路径,包含文件名
                    String filePath = temp.getAbsolutePath();
                    //获取文件名
                    String fileName = temp.getName();
                    System.out.println(temp.isFile() + " " + temp.getAbsolutePath());
            readFileContent(temp);
            }
            }
        }
    
        /**
       * @param file 要读取的文件对象
       * @return 返回文件的内容
       * */
        public static String readFileContent(File file) throws IOException{
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            StringBuffer sb = new StringBuffer();
            while(br.ready()){
    //            sb.append(br.readLine());
                System.out.println(br.readLine());
            }
            System.out.println(sb.toString());
            return sb.toString();
        }
    
        /**
       * @param file 要写入的文件对象
       * @param content 要写入的文件内容
       * */
        public static void writeFileContent(File file,String content) throws IOException{
            FileWriter fw = new FileWriter(file);
            fw.write(content);
            fw.flush();
            fw.close();
        }
    
        public static void main(String[] args) {
            try {
                findFile(new File(basePath));
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }
    }

      结果:

  • 相关阅读:
    Gitlab使用腾讯企业邮箱
    查看Binlog内容
    微信小游戏手记
    clickhouse手记
    腾讯云手记
    go框架gin
    go idea debug
    go手记
    crontab手记
    Laravel-cors 跨域
  • 原文地址:https://www.cnblogs.com/zhouheblog/p/11678150.html
Copyright © 2020-2023  润新知