摘要:包幾乎包含了所有操作輸入輸出需要的類。所有這些流類代表了輸入源和輸出目標(biāo)。輸入流表示從一個源讀取數(shù)據(jù),輸出流表示向一個目標(biāo)寫數(shù)據(jù)。但本節(jié)講述最基本的和流與相關(guān)的功能。讀取控制臺輸入的控制臺輸入由完成。有多種構(gòu)造方法可用來創(chuàng)建對象。
package com.learn; import org.omg.CORBA.PUBLIC_MEMBER; import java.io.*; /** * Created by fiz on 7/10/17. ** Java.io 包幾乎包含了所有操作輸入、輸出需要的類。所有這些流類代表了輸入源和輸出目標(biāo)。 * Java.io 包中的流支持很多種格式,比如:基本類型、對象、本地化字符集等等。 * 一個流可以理解為一個數(shù)據(jù)的序列。輸入流表示從一個源讀取數(shù)據(jù),輸出流表示向一個目標(biāo)寫數(shù)據(jù)。 * Java 為 I/O 提供了強大的而靈活的支持,使其更廣泛地應(yīng)用到文件傳輸和網(wǎng)絡(luò)編程中。 * 但本節(jié)講述最基本的和流與 I/O 相關(guān)的功能。我們將通過一個個例子來學(xué)習(xí)這些功能。 *
*
*
* 讀取控制臺輸入: *
* Java 的控制臺輸入由 System.in 完成。 * 為了獲得一個綁定到控制臺的字符流,你可以把 System.in 包裝在一個 BufferedReader 對象中來創(chuàng)建一個字符流。 * 下面是創(chuàng)建 BufferedReader 的基本語法: */ public class FileStreamTest { public static void readDir(String dir) { /** * 讀取指定目錄下的文件夾,并只返回文件, */ File file = new File(dir); if (file.isDirectory()) { System.out.println("這個是個文件夾"); } else { System.out.println(); } } public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("請輸入你的要輸入的內(nèi)容"); String input = bufferedReader.readLine(); System.out.println(input); /** * FileInputStream *該流用于從文件讀取數(shù)據(jù),它的對象可以用關(guān)鍵字 new 來創(chuàng)建。 *有多種構(gòu)造方法可用來創(chuàng)建對象。 *可以使用字符串類型的文件名來創(chuàng)建一個輸入流對象來讀取文件 */ InputStream inputStream = new FileInputStream("/Users/fiz/Documents/JavaProject/JavaProject.iml"); byte[] buffer = new byte[1000]; while (inputStream.read(buffer) > 0) { System.out.println(inputStream.read(buffer)); } /** * 輸出文件 */ File f = new File("/Users/fiz/Documents/JavaProject/demo.txt"); FileOutputStream fop = new FileOutputStream(f); // 構(gòu)建FileOutputStream對象,文件不存在會自動新建 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fop); outputStreamWriter.write("demo"); outputStreamWriter.close(); /** * PipedOutputStream/PipedInputStream * 不能用于同一個線程中,主要是要去不同線程獲取 */ final PipedOutputStream pipedOutputStream = new PipedOutputStream(); final PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream); Thread thread1 = new Thread(new Runnable() { @Override public void run() { try { pipedOutputStream.write("Hello world, pipe!".getBytes()); } catch (IOException e) { } } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { int data = pipedInputStream.read(); while (data != -1) { System.out.print((char) data); data = pipedInputStream.read(); } } catch (IOException e) { } } }); thread1.start(); thread2.start(); /** * InputStream */ InputStream inputStream1 = new FileInputStream("/Users/fiz/Documents/JavaProject/JavaProject.iml"); byte[] bytesRead = new byte[1024]; int data = inputStream1.read(bytesRead); while (data != -1) { //do something with data... System.out.println((char) data); data = inputStream1.read(); } inputStream1.close(); /** * FileOutputStream */ File file = new File("demo.txt"); FileOutputStream fileOutputStream = new FileOutputStream(file); int num = 10; while (num > 0) { fileOutputStream.write("deno+ ".getBytes()); num = num - 1; } fileOutputStream.close(); /** *FileInputStream 讀取文件 */ FileInputStream fileInputStream = new FileInputStream(file); int read = fileInputStream.read(); while (read != -1) { System.out.println((char) fileInputStream.read()); read = fileInputStream.read(); } /** * inputStreamReader */ InputStream inputStream2 = new FileInputStream("demo.txt"); Reader inputStreamReader = new InputStreamReader(inputStream2); int data2 = inputStreamReader.read(); while (data2 != -1) { char theChar = (char) data2; System.out.println("inputStreamReader"); System.out.println(theChar); data2 = inputStreamReader.read(); } /** * */ OutputStream outputStream = new FileOutputStream("demo.txt"); Writer outputStreamWriter1 = new OutputStreamWriter(outputStream); outputStreamWriter1.write("Hello World"); outputStreamWriter1.close(); /** * FileWriter */ FileWriter fileWriter = new FileWriter(file); fileWriter.write("This is an example "); fileWriter.flush();// 刷掉緩沖區(qū)的內(nèi)容 fileWriter.close(); /** * FileReader */ FileReader fileReader = new FileReader(file); int fileReader_data = fileReader.read(); while (fileReader_data != -1) { fileReader_data = fileReader.read(); System.out.println((char) fileReader_data); } /** *BufferedReader */ BufferedReader bufferedReader1 = new BufferedReader(new FileReader(file)); String line = bufferedReader1.readLine(); System.out.println("BufferedReader" + line); bufferedReader1.close(); // 循環(huán)讀取 // while (line != null) { // // process line // } /** *BufferedWriter */ BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("demo.txt", true)); bufferedWriter.write("this is a demo process"); int count = 10; while (count > 0) { bufferedWriter.write("this is a demo process "); count = count - 1; } bufferedWriter.close(); File file1 = new File("test.txt"); FileWriter fw = new FileWriter(file1); BufferedWriter bw = new BufferedWriter(fw); bw.write("demo"); System.out.println("File written Successfully"); /** *FilterWriter 虛擬類 */ /** * */ LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(file1)); String line1 = lineNumberReader.readLine(); while (line != null) { line = lineNumberReader.readLine(); System.out.println(line + lineNumberReader.getLineNumber()); } } }
做個記錄
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/67342.html
摘要:不建議對這兩個對象嘗試使用單個線程,因為這樣可能會造成該線程死鎖。構(gòu)造函數(shù)創(chuàng)建尚未連接到管道輸入流的管道輸出流。通常,數(shù)據(jù)由某個線程從對象讀取,并由其他線程將其寫入到相應(yīng)的。 管道流 管道流的主要作用是可以進行兩個線程間的通訊,分為管道輸出流(PipedOutputStream)、管道輸入流(PipedInputStream),如果想要進行管道輸出,則必須要把輸出流連在輸入流之上,在...
閱讀 1865·2019-08-30 15:53
閱讀 3193·2019-08-30 15:44
閱讀 2806·2019-08-26 13:31
閱讀 1949·2019-08-26 12:10
閱讀 792·2019-08-26 11:01
閱讀 2120·2019-08-23 15:32
閱讀 1585·2019-08-23 13:43
閱讀 2529·2019-08-23 11:58