java中io流实现按行读取文件

在读文件时,有的时候我们想一条数据一条数据地读,比如文件中每一行数据是一条数据,我们就要按行读取:

​public static void main(String[] args){
    File readFile = new File("C:\\wyh\\it\\java");
    File writeFile = new File("C:\\wyh\\it\\test");
    BufferedReader reader = null;
    OutputStream writer = null;
    try{
        reader = new BufferedReader(new FileReader(readFile));
        writer = new FileOutputStream(writeFile);
        String tempString = null;
        int line = 1;
        while ((tempString = reader.readLine()) != null){//BufferedReader有readLine(),可以实现按行读取
            System.out.println(line+tempString);
            line++;
        }
    reader.close();
    }catch(IOException e){
        e.printStackTrace();
    }finally{
        if(reader != null){
            try{
                reader.close();
            }catch(IOException e){
            }
        }
    }
}

​

 

你可能感兴趣的:(java)