DataInputStream的readLine方法已经deprecated, 推荐使用BufferedReader来读取行
InputStream is fundamentally a binary construct. If you want to read text data (e.g. from the console) you should use a Reader of some description. To convert an InputStream into a Reader, useInputStreamReader. Then create a BufferedReader around the Reader, and you can read a line using BufferedReader.readLine().
More alternatives:
- Use a Scanner built round System.in, and call Scanner.nextLine
- Use a Console (obtained from System.console()) and call Console.readLine
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
public class DataInputStreamTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
File file = new File("C:" + File.separator + "workspace"+File.separator+"Test"
+ File.separator + "JavaIO" + File.separator + "src"
+ File.separator + "BufferedReaderTest.java");
FileInputStream fstream = new FileInputStream(file);
BufferedInputStream stream = new BufferedInputStream(fstream);
DataInputStream dstream = new DataInputStream(stream);
String str = "";
while((str = dstream.readLine()) != null){
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}