Java I/O 流的幾種典型用法
阿新 • • 發佈:2019-01-07
一.緩衝輸入檔案
public class BufferedReaderFile {
public static String read (String filePath){
//確定流物件是一個檔案故通過FileReaderf封裝檔案(在java中將有能力產生資料的資料來源物件或有能力接收資料的接收端物件抽象成"流")
FileReader in = new FileReader(filePath);
//BufferedReader是一個裝飾器物件,他提供一個用來一個按行讀取的方式,最重要的是這個物件對檔案進行了快取,能提高讀取的速度
BufferedReader reader = new BufferedReader(in);
String s;
StringBuffered buffer = new StringBuffered();
while((s = reader.readLine()) != null){
buffer.append(s);
}
return buffer.toString();
}
}
二.從記憶體中輸入
public class MemoryInput {
public static void main(String [] args){
//通過read()方法我們獲取的是字串故我們的源物件是字串型別的,所以我們用StringReader來封裝我們的流物件
StringReader in = new StringReader(BufferedReaderFile.read(filePath));
int c;
//read()方法是已int型別返回下一個位元組
while ((c = in.read()) != -1){
System.out.print((char ) c);
}
}
}
三.記憶體輸入格式化
//要讀取格式化資料,可以使用DataInputStream,它是一個面向位元組I/O類(並且他是一個裝飾器類繼承FilterInputStream類),因為我們的資料來源是字串故我們將資料來源轉換成位元組。之所以DataInputStream 能讀取格式化資料,是因為它提供了諸如readDouble(),readeUTF,readByte()等方法,在格式資料前你必須清除資料來源中的資料都是什麼型別的。
public class FormatterMemoryInput {
public static void main (String [] args){
DataInputStream in = new DataInputStream(new ByteArrayInputStream(BufferedReader.read(filePath).getBytes))
//available()方法字面意思是"在沒有阻塞的情況下所能讀取的位元組數".對於檔案,意味著整個檔案;但對於不同型別的流,可能就不是這樣的了,因此要謹慎使用
while (in.available() != 0){
System.out.print((char)in.readByte());
}
}
}
四.基本的檔案輸出
//既然我們的流物件是檔案,那麼我們可以使用FileWriter將檔案寫入資料,但我們為了增強I/O操作的效能,會使用BufferedWriter將其包裝起來用緩衝輸出,同時使用PrintWriter提供格式化
public class BasicFileOutput{
public static void main(String [] args){
//建立一個輸入流物件
StringReader reader = new StringReader(BufferedReaderFile.read(filepath));
BufferedReader in = new BuffferedReader(reader);
//建立一個輸出流物件(方式一)
FileWriter writer = new FileWriter(filePath);
BufferedWriter buffer = new BufferedWriter(writer);
PrintWriter out = new PrintWriter(buffer);
//建立一個輸出流物件(方式二) 這種方式可謂是PrintWriter輸出資料的快捷方式
//PrintWriter out = new PrintWriter(file)
String s;
while ((s = in.readLine()) != null){
//寫入一行後換行
out.println(s)
}
}
}
五.儲存和恢復資料
//PrintWriter 可以對資料進行格式化,以便人們閱讀。但是為了輸出可供另一個"流"恢復資料,我們需要用DataOutputStream寫入資料,並用DataInputStream恢復資料。我們已檔案為例。並且對讀寫操作進行緩衝
public class StoringAndRecoveringData{
public static void main(String [] args){
DataOutputStream out = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(file)));
out.writeDouble(3.1415);
out.writeUTF("That was pi");
out.writeDouble(1.434);
out.close();
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
System.out.println(in.readDouble());
System.out.println(in.readUTF());
System.out.println(in.readDouble());
//可以看到我們使用DataOutputStream寫入資料,Java保證我們用DataInputStream準確地讀取資料(無論讀和寫資料的平臺多麼不同)
}
}
六.讀寫隨機訪問檔案
//RandomAccessFile是一個比較獨立的類,之所以說他獨立是因為他既沒有實現InputStream 也沒有實現OutputStream,他提供了seek()方法可以移動到檔案的任意位置進行寫入操作,因為他實現了DataOutInput和DataInput介面可以說是DataInputStream和DataOutputStream的集合體
public class UsingRandomAccessFile{
public static void main(String [] args){
//建構函式接收兩個引數,一個表示檔案物件,第二個表示對檔案物件的操作權利(讀r和寫w)
RandomAccessFile rf = new RandomAccessFile(file,"rw");
for (int i = 0; i < 7; i++){
rf.writeDouble(i * 3.1314);
}
//因為double是8個位元組長度,查詢第5個double值(相當於將游標移動到此處)
rf.seek(5*8);
rf.writeUTF("This end");
rf.close();
}
}
七.管道流
PipedInputStream, PipedOutputStream,PipedReader,PipedWriter 他們的價值是用於任務之間的通訊。