Java 读取 TXT文件

package io;

import jxl.Workbook;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;

public class OpenFile {

    public static void main(String[] args) throws IOException {
        String read = readFileContent("C:\\Users\\Administrator\\Desktop\\140.txt");

        List<String> readBig = readBigFileContent("C:\\Users\\Administrator\\Desktop\\140.txt");
        List<String> readBig1 = readBigFileContent("C:\\Users\\Administrator\\Desktop\\144.txt");
        List<String> readBig2 = readBigFileContent("C:\\Users\\Administrator\\Desktop\\147.txt");
        List<String> readBig3 = readBigFileContent("C:\\Users\\Administrator\\Desktop\\151.txt");

        Set<String> set = new HashSet();
        for(String temp : readBig){
            temp = temp.split("= ")[2];
            temp = temp.split(",")[0];
            set.add(temp);
            //System.out.println(temp);
        }

        for(String str : set){
            System.out.println(str);
        }

        System.out.println("==========================================");


        for(String temp : readBig1){
            temp = temp.split("= ")[2];
            temp = temp.split(",")[0];
            set.add(temp);
            //System.out.println(temp);
        }

        for(String str : set){
            System.out.println(str);
        }

        System.out.println("==========================================");



        for(String temp : readBig2){
            temp = temp.split("= ")[2];
            temp = temp.split(",")[0];
            set.add(temp);
            //System.out.println(temp);
        }

        for(String str : set){
            System.out.println(str);
        }

        System.out.println("==========================================");



        for(String temp : readBig3){
            temp = temp.split("= ")[2];
            temp = temp.split(",")[0];
            set.add(temp);
            //System.out.println(temp);
        }
        for(String str : set){
            System.out.println(str);
        }

        System.out.println("==========================================");


//        System.out.println(read);
    }



    /**
     * 读取一个TXT文件内容
     * @param fileName
     * @return
     */
    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuilder sbf = new StringBuilder();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }


    public static List<String> readBigFileContent(String fileName) throws IOException {

        List<String> list = new ArrayList<>();
        FileInputStream fileInputStream = new FileInputStream(fileName);
        Scanner scanner = new Scanner(fileInputStream, "utf-8");
        while (scanner.hasNext()) {
            String result = scanner.nextLine();
            list.add(result);
        }
        fileInputStream.close();
        scanner.close();


        return list;
    }

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页