JAVA IO - DataInputStream

DataInputStream的readLine方法已經deprecated, 推薦使用BufferedReader來讀取行 html

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(). java

More alternatives: api

  • 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();
		}
	}

}
相關文章
相關標籤/搜索