首页 > 代码库 > Java:逐行读取文件的用法

Java:逐行读取文件的用法

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;public class HelloWord {    public static void main(String[] args) throws IOException {        String filePath = "my File.txt";        File file = new File(filePath);        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),"utf-8");        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);        List<String> lineItems=new ArrayList<String>();        String line = null;        while ((line = bufferedReader.readLine()) != null) {            String[] items = line.split(",");                        String item= items[1];            if(item.indexOf("花园")!=-1){                lineItems.add(line);            }        }        bufferedReader.close();        inputStreamReader.close();                for(String my : lineItems){            System.out.println(my);        }    }}

 

Java:逐行读取文件的用法