国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java.io徹底研究

2019-11-18 11:33:01
字體:
來源:轉載
供稿:網友

  一. Input和Output

  1. stream代表的是任何有能力產出數據的數據源,或是任何有能力接收數據的接收源。在java的IO中,所有的stream(包括Input和Out stream)都包括兩種類型:

  1.1 以字節為導向的stream

  以字節為導向的stream,表示以字節為單位從stream中讀取或往stream中寫入信息。以字節為導向的stream包括下面幾種類型:

  1) input stream:

  1) ByteArrayInputStream:把內存中的一個緩沖區作為InputStream使用

  2) StringBufferInputStream:把一個String對象作為InputStream

  3) FileInputStream:把一個文件作為InputStream,實現對文件的讀取操作

  4) PipedInputStream:實現了pipe的概念,主要在線程中使用

  5) SequenceInputStream:把多個InputStream合并為一個InputStream

  2) Out stream

  1) ByteArrayOutputStream:把信息存入內存中的一個緩沖區中

  2) FileOutputStream:把信息存入文件中

  3) PipedOutputStream:實現了pipe的概念,主要在線程中使用

  4) SequenceOutputStream:把多個OutStream合并為一個OutStream

  1.2 以Unicode字符為導向的stream

  以Unicode字符為導向的stream,表示以Unicode字符為單位從stream中讀取或往stream中寫入信息。以Unicode字符為導向的stream包括下面幾種類型:

  1) Input Stream

  1) CharArrayReader:與ByteArrayInputStream對應

  2) StringReader:與StringBufferInputStream對應

  3) FileReader:與FileInputStream對應

  4) PipedReader:與PipedInputStream對應

  2) Out Stream

  1) CharArrayWrite:與ByteArrayOutputStream對應

  2) StringWrite:無與之對應的以字節為導向的stream

  3) FileWrite:與FileOutputStream對應

  4) PipedWrite:與PipedOutputStream對應

  以字符為導向的stream基本上對有與之相對應的以字節為導向的stream。兩個對應類實現的功能相同,字是在操作時的導向不同。如CharArrayReader:和ByteArrayInputStream的作用都是把內存中的一個緩沖區作為InputStream使用,所不同的是前者每次從內存中讀取一個字節的信息,而后者每次從內存中讀取一個字符。

  1.3 兩種不現導向的stream之間的轉換

  InputStreamReader和OutputStreamReader:把一個以字節為導向的stream轉換成一個以字符為導向的stream。

  2. stream添加屬性

  2.1 “為stream添加屬性”的作用

  運用上面介紹的Java中操作IO的API,我們就可完成我們想完成的任何操作了。但通過FilterInputStream和FilterOutStream的子類,我們可以為stream添加屬性。下面以一個例子來說明這種功能的作用。

  假如我們要往一個文件中寫入數據,我們可以這樣操作:

FileOutStream fs = new FileOutStream(“test.txt”);

  然后就可以通過產生的fs對象調用write()函數來往test.txt文件中寫入數據了。但是,假如我們想實現“先把要寫入文件的數據先緩存到內存中,再把緩存中的數據寫入文件中”的功能時,上面的API就沒有一個能滿足我們的需求了。但是通過FilterInputStream和FilterOutStream的子類,為FileOutStream添加我們所需要的功能。

  2.2 FilterInputStream的各種類型

  2.2.1 用于封裝以字節為導向的InputStream

  1) DataInputStream:從stream中讀取基本類型(int、char等)數據。

  2) BufferedInputStream:使用緩沖區

  3) LineNumberInputStream:會記錄input stream內的行數,然后可以調用getLineNumber()和setLineNumber(int)

  4) PushbackInputStream:很少用到,一般用于編譯器開發

  2.2.2 用于封裝以字符為導向的InputStream

  1) 沒有與DataInputStream對應的類。除非在要使用readLine()時改用BufferedReader,否則使用DataInputStream

  2) BufferedReader:與BufferedInputStream對應

  3) LineNumberReader:與LineNumberInputStream對應

  4) PushBackReader:與PushbackInputStream對應

  2.3 FilterOutStream的各種類型

  2.2.3 用于封裝以字節為導向的OutputStream

  1) DataIOutStream:往stream中輸出基本類型(int、char等)數據。

  2) BufferedOutStream:使用緩沖區

  3) PRintStream:產生格式化輸出

  2.2.4 用于封裝以字符為導向的OutputStream

  1) BufferedWrite:與對應

  2) PrintWrite:與對應

  3. RandomaccessFile

  1) 可通過RandomAccessFile對象完成對文件的讀寫操作

  2) 在產生一個對象時,可指明要打開的文件的性質:r,只讀;w,只寫;rw可讀寫

  3) 可以直接跳到文件中指定的位置

QQ病毒 騰訊QQ空間代碼專題 PPT教程專題 ADSL應用面面俱到 fireworks教程專題 計算機和網絡技術基礎知識 校園網專題 網吧技術專題


  4. I/O應用的一個例子

import java.io.*;
public class TestIO{
public static void main(String[] args)
throws IOException{
//1.以行為單位從一個文件讀取數據
BufferedReader in =
new BufferedReader(
new FileReader("F://nepalon//TestIO.java"));
String s, s2 = new String();
while((s = in.readLine()) != null)
s2 += s + "/n";
in.close();
//1b. 接收鍵盤的輸入
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter a line:");
System.out.println(stdin.readLine());

//2. 從一個String對象中讀取數據
StringReader in2 = new StringReader(s2);
int c;
while((c = in2.read()) != -1)
System.out.println((char)c);
in2.close();

//3. 從內存取出格式化輸入
try{
DataInputStream in3 =
new DataInputStream(
new ByteArrayInputStream(s2.getBytes()));
while(true)
System.out.println((char)in3.readByte());
}
catch(EOFException e){
System.out.println("End of stream");
}

//4. 輸出到文件
try{
BufferedReader in4 =
new BufferedReader(
new StringReader(s2));
PrintWriter out1 =
new PrintWriter(
new BufferedWriter(
new FileWriter("F://nepalon// TestIO.out")));
int lineCount = 1;
while((s = in4.readLine()) != null)
out1.println(lineCount++ + ":" + s);
out1.close();
in4.close();
}
catch(EOFException ex){
System.out.println("End of stream");
}

//5. 數據的存儲和恢復
try{
DataOutputStream out2 =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("F://nepalon// Data.txt")));
out2.writeDouble(3.1415926);
out2.writeChars("/nThas was pi:writeChars/n");
out2.writeBytes("Thas was pi:writeByte/n");
out2.close();
DataInputStream in5 =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("F://nepalon// Data.txt")));
BufferedReader in5br =
new BufferedReader(
new InputStreamReader(in5));
System.out.println(in5.readDouble());
System.out.println(in5br.readLine());
System.out.println(in5br.readLine());
}
catch(EOFException e){
System.out.println("End of stream");
}

//6. 通過RandomAccessFile操作文件
RandomAccessFile rf =
new RandomAccessFile("F://nepalon// rtest.dat", "rw");
for(int i=0; i<10; i++)
rf.writeDouble(i*1.414);
rf.close();

rf = new RandomAccessFile("F://nepalon// rtest.dat", "r");
for(int i=0; i<10; i++)
System.out.println("Value " + i + ":" + rf.readDouble());
rf.close();

rf = new RandomAccessFile("F://nepalon// rtest.dat", "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();

rf = new RandomAccessFile("F://nepalon// rtest.dat", "r");
for(int i=0; i<10; i++)
System.out.println("Value " + i + ":" + rf.readDouble());
rf.close();
}
}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 周口市| 泸州市| 阆中市| 漳平市| 简阳市| 江城| 临泉县| 江川县| 深州市| 靖西县| 赤峰市| 高邮市| 铅山县| 西安市| 江源县| 宁强县| 资中县| 东台市| 舒城县| 久治县| 石河子市| 新丰县| 阆中市| 绍兴县| 宁晋县| 屯门区| 博白县| 天镇县| 湾仔区| 桐庐县| 邻水| 邛崃市| 眉山市| 永顺县| 宝丰县| 肇源县| 普宁市| 陇川县| 图片| 拜泉县| 瑞丽市|