這篇文章將為大家詳細講解有關Java中io流解析的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
IO流
Java中IO流分為兩種,字節流和字符流,顧名思義字節流就是按照字節來讀取和寫入的,字符劉是按照字符來存取的;常用的文件讀取用的就是字符流,在網絡通信里面用的就是字節流
下面這張圖是Java中IO流的總體框架:

字節流
Java中字節流一般都是以stream結尾的,輸入的字節流叫InputStream,輸出字節流叫OutputStream;InputStream和OutputStream是表示自己輸入/輸出的所有類的超類,是抽象類(abstract)
常用的字節流有:
1.FileInputStream/FileOutputStream 2.BufferedInputStream/BufferedOutputStream 3.SequenceInputStream(序列流) 4.ObjectInputStream/ObjectOutputStream(對象的輸入輸出流) 5.PrintStream(打印流)
字符流
Java中輸入字符流是以Reader結尾的,輸出字符流是以Writer結尾的,比如我們常見的FileReader和FileWriter就是個字符流,Reader和Witer是輸入/輸出字符流的超類,也是抽象類
常用的字符流有:
1.FileReader/FileWriter 2.BufferedReader/BufferedWriter 3.InputStremReader/OutputStreamWriter(轉換流)
轉換流
轉換流就是將字節流轉換為字符流的類,有兩種:
·InputStreamReader ·OutputStreamWriter
InputStreamReader是個字符流(Reader),需要包裝一個字節流(InputStream);
OutputStreamWriter是個字符流(Writer),需要包裝一個字節流(OutputStream)
包裝(decorate)
包裝的作用的就是在原始的對象的基礎上增加新的功能,比如BufferedReader包裝一個Reader,實際就是對Reader功能的增強;原始的Reader只能按照一個字符一個字符的讀取,經過包裝之后形成的BufferedReader就具有了新的功能:直接讀取一行(readLine)的功能,直觀上說這就是所謂的Decorate.
在設計模式上這就是典型的裝飾模式,其特點是:
1.裝飾對象和真實對象有相同的接口。這樣客戶端對象就能以和真實對象相同的方式和裝飾對象交互 2.裝飾對象可以在轉發這些請求以前或以后增加一些附加功能。這樣就確保了在運行時,不用修改給定對象的結構就可以在外部增加附加的功能
對應到我們上來就是,BufferedReader和Reader都是個Reader,通過包裝之后BufferedReader功能增強,但是依然可以當做Reader來用(OO的父類引用可以指向子類)
例子
字節流的例子
將mp3文件切割成多份數,然后重新組合起來
package cn.xdian.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
public class Demo2 {
public static void main(String[] args) throws IOException {
cutFile();
//切割MP3文件
mergeFlile();
//合并MP3文件
}
//合并MP3
public static void mergeFlile() throws IOException{
File dir = new File("/home/gavinzhou/music_test");
//找到文件夾下所有的MP3文件
Vector<FileInputStream> vector = new Vector<FileInputStream>();
File[] files = dir.listFiles();
for (File file : files){
if(file.getName().endsWith(".mp3")){
vector.add(new FileInputStream(file));
}
}
//通過Vector獲取迭代器
Enumeration<FileInputStream> e = vector.elements();
//創建序列流
SequenceInputStream inputStream = new SequenceInputStream(e);
//輸出流
FileOutputStream fileOutputStream = new FileOutputStream("/home/gavinzhou/conbine.mp3");
//讀取分割的MP3文件
byte[] buf = new byte[1024];
int length = 0 ;
while((length = inputStream.read(buf))!=-1){
fileOutputStream.write(buf,0,length);
}
//關閉流
fileOutputStream.close();
inputStream.close();
}
//切割MP3
public static void cutFile() throws IOException{
File file = new File("/home/gavinzhou/test.mp3");
File dir = new File("/home/gavinzhou/music_test");
//輸入字節流
FileInputStream fileInputStream = new FileInputStream(file);
//讀取文件
byte[] buf = new byte[1024*1024];
int length = 0;
for (int i = 0 ; (length = fileInputStream.read(buf))!=-1 ; i++){
FileOutputStream fileOutputStream = new FileOutputStream(new File(dir,"part"+i+".mp3"));
fileOutputStream.write(buf,0,length);
fileOutputStream.close();
}
//關閉流
fileInputStream.close();
}
}字符流的例子
拷貝文件A變為文件B
package cn.xidian.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) throws IOException {
File sourceFile = new File("/home/gavinzhou/a.txt");
File desFile = new File("/home/gavinzhou/b.txt");
//創建輸入流
BufferedReader input = new BufferedReader(new FileReader(sourceFile));
//創建輸出流
BufferedWriter output = new BufferedWriter(new FileWriter(desFile));
//讀取源文件,寫入到新的文件
String line = null;
while((line = input.readLine()) != null){
output.write(line);
output.newLine();
}
//關閉輸入輸出流
input.close();
output.close();
}
}打印流的例子
package cn.xidian.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
/*
打印流可以打印任意類型的數據,打印數據之前都會先把數據轉換成字符串再進行打印
*/
class Animal{
String name;
String color;
public Animal(String name,String color){
this.name = name;
this.color = color;
}
@Override
public String toString() {
return "名字:"+this.name+ " 顏色:"+ this.color;
}
}
public class Demo6 {
public static void main(String[] args) throws IOException {
/*
File file = new File("/home/gavinzhou/a.txt");
//創建打印流
PrintStream printStream = new PrintStream(file);
//打印任何信息到文件中
printStream.println(97);
printStream.println(3.14);
printStream.println('a');
printStream.println(true);
Animal a = new Animal("老鼠", "黑色");
printStream.println(a);
//更改標準的輸入輸出
System.setOut(printStream); //標準輸出是到屏幕上
System.out.println("test.......");
*/
//收集異常的日志信息。
File logFile = new File("/home/gavinzhou/test.log");
PrintStream logPrintStream = new PrintStream( new FileOutputStream(logFile,true) );
try{
int c = 4/0;
//引起異常
System.out.println("c="+c);
int[] arr = null;
System.out.println(arr.length);
}
catch(Exception e){
e.printStackTrace(logPrintStream);
//輸出到文件而不是屏幕上
}
}
}關于“Java中io流解析的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。