java--利用Filereader BufferedReader读取文本文档

import java.io.*;
import java.util.*;
public class test {
    public static void readFile() throws IOException {
        File f = new File("C:\\Users\\15773\\Desktop\\投递\\新建文本文档.txt");
        FileReader reader =  new FileReader(f);
        BufferedReader br = new BufferedReader(reader);
        //按行读取
        String line;
        while ((line = br.readLine()) != null){
            System.out.println(line);
        }
        br.close();
    }
    public static void main(String[] args) throws IOException{
        test.readFile();
    }
}

读取文件正则

import java.io.*;
import java.util.*;
import java.util.regex.*;
public class test {
    public static void readFile() throws IOException {
        File f = new File("C:\\Users\\15773\\Desktop\\投递\\新建文本文档.txt");
        FileReader reader =  new FileReader(f);
        BufferedReader br = new BufferedReader(reader);
        //按行读取
        String line;
        String regex="\\d+";
        while ((line = br.readLine()) != null){
//            System.out.println(line);
            if (line.matches(regex)){
                System.out.println(line+":matching regex");
            }else {
                System.out.println(line+":not matching regex");
            }
        }
        br.close();
    }
    public static void main(String[] args) throws IOException{
        test.readFile();
    }
}

 

posted @ 2021-03-07 20:40  朵朵奇fa  阅读(292)  评论(0编辑  收藏  举报