假设该输入流从一个文件中获取,然后每次读取一行输出,直到读取完毕。用Java IO中的BufferedReader的readLine(),“顾名思义,读取一行”实现:
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; public class MainClass { public static void main(String[] args) { try { InputStream is = new FileInputStream("file.txt"); BufferedReader reader = new BufferedReader( new InputStreamReader(is)); String str = null; while (true) { str = reader.readLine(); if(str!=null) System.out.println(str); else break; } is.close(); } catch (Exception e) { e.printStackTrace(); } } }
file.txt文件内容:
1 2 3 4 5
代码运行输出:
1 2 3 4 5
时间: 2024-10-19 17:27:28