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("\t",",");
                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();
        }
    }
}

  結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM