一.打印流(PrintWriter和PrintStream):
特點:可以將各種基本數據類型的數據都原樣打印。
PrintWriter(字符打印流):
常用構造函數:
PrintWriter(File file)
|
PrintWriter(OutputStream out)
|
PrintWriter(String fileName)
|
PrintWriter(Writer out)
可以將字符打印進入任何字符輸出流,包括System.out |
PrintWriter(OutputStream out, boolean autoFlush)
|
PrintStream(字節打印流):
常用構造函數:
PrintStream(File file)
|
PrintStream(OutputStream out)
|
PrintStream(String fileName)
|
二.文件的切割和合並(SequenceInputStream):
作用:大文件切割分批傳輸
示例代碼:
/**
* 文件的切割和合並
* @author 小蘇
*
*/
public class FileSplitDemo {
static String path = "G://圖片/95ff58976aa17a70e5200c7ae633cac0.jpg";
static String[] str = new String[]{"G://圖片/split/0.pnp","G://圖片/split/1.pnp","G://圖片/split/2.pnp"};
/**
* @param args
*/
public static void main(String[] args) {
//文件切割
try {
split(path);
} catch (IOException e) {
e.printStackTrace();
}
//文件合併
try {
merge(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void split(String path) throws IOException{
int num = 0;
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = null;
byte[] buff = new byte[1024*1024];
int len;
while((len = fis.read(buff)) != -1){
fos = new FileOutputStream("G://圖片/split/"+(num++)+".pnp");
fos.write(buff, 0, len);
}
fos.close();
fis.close();
}
static void merge(String[] str) throws IOException{
Vector<FileInputStream> ve = new Vector<FileInputStream>();
for(String s : str){
ve.add(new FileInputStream(s));
}
Enumeration<FileInputStream> elements = ve.elements();
SequenceInputStream sis = new SequenceInputStream(elements);
FileOutputStream fos = new FileOutputStream("G://圖片/split/合併文件.png");
byte[] buff = new byte[1024*1024];
int len;
while((len = sis.read(buff)) != -1){
fos.write(buff, 0, len);
}
fos.close();
sis.close();
}
}
三.對象的序列化(ObjectInputStream,ObjectOutputStream和Serializable(接口)):
1. 被序列化的對象必須實現Serializable接口
2. transient關鍵字:被transient修飾的關鍵字不會被序列化
3. 類的靜態成員變量不會被序列化
4. serialVersionUID:類的標識符,判斷某個對象是否是某個類的實例。是根據類的成員變量得出的一個值
ObjectInputStream常用方法摘要:
從 ObjectInputStream 讀取對象。 |
|
int |
readInt()
|
int |
read()
|
int |
read(byte[] buf, int off, int len)
|
ObjectOutputStream常用方法摘要:
|
writeObject(Object obj)
|
|
writeInt(int val)
|
|
write(byte[] buf)
|
|
write(byte[] buf, int off, int len)
|
|
write(int val)
|
四. 管道流(PipedInputStream和PipedOutputStream):
特點:輸入輸出可以直接進行連接,通過結合線程使用
示例代碼:/**
* 管道流示例代碼
* @author 小蘇
*
*/
public class Test12 {
public static void main(String[] args) {
try {
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);
new Thread(new Reader(pis)).start();
new Thread(new Writer(pos)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Reader implements Runnable{
private PipedInputStream pis;
public Reader(PipedInputStream pis) {
this.pis = pis;
}
@Override
public void run() {
try {
StringBuffer sb = new StringBuffer();
byte[] buff = new byte[1024];
int len;
while((len = pis.read(buff))!= -1){
sb.append(new String(buff,0,len));
}
pis.close();
System.out.println(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Writer implements Runnable{
private PipedOutputStream pos;
public Writer(PipedOutputStream pos) {
this.pos = pos;
}
@Override
public void run() {
try {
byte[] buff = "hello world".getBytes();
pos.write(buff);
pos.flush();
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
五. RandomAccessFile:
概述:不算是IO體系的子類,直接繼承Object;但它是IO的一員,因爲它具備讀寫操作。
特點:能指定指針位置對文件進行讀寫或者修改;如果操作模式爲rw,操作的文件不存在則會自動創建,如果存在不會覆蓋。可以實現多線程操作同一個文件,多線程下載
原理:內部封裝了一個數組,通過指針對數組元素進行操作,可以通過
getFilePointer()來獲取指針的位置,同時可以通過seek()來改變指針的位置(內部封裝了字節輸入和輸出流)
注意:該類只能操作文件,而且還是帶着模式操作
操作模式
含意 |
|
"r" |
以只讀方式打開。調用結果對象的任何 write 方法都將導致拋出 IOException。 |
"rw" |
打開以便讀取和寫入。如果該文件尚不存在,則嘗試創建該文件。 |
"rws" |
打開以便讀取和寫入,對於 "rw",還要求對文件的內容或元數據的每個更新都同步寫入到底層存儲設備。 |
"rwd" |
打開以便讀取和寫入,對於 "rw",還要求對文件內容的每個更新都同步寫入到底層存儲設備。 |
常用方法摘要:
|
length()
|
|
seek(long pos)
|
|
skipBytes(int n)
|
l六. 操作基本數據類型(DataInputStream,DataOutputStream):
特點:直接操作基本數據類型,凡是操作基本數據類型就用這個流
七,操作字節數組(內存流)(ByteArrayInputStream,ByteArrayOutputStream):
ByteArrayOutputStream:此類實現了一個輸出流,其中的數據被寫入一個 byte 數組。緩衝區會隨着數據的不斷寫入而自動增長。可使用
toByteArray()
和 toString()
獲取數據。 關閉 ByteArrayOutputStream 無效。此類中的方法在關閉此流後仍可被調用,而不會產生任何
IOException。
ByteArrayInputStream:ByteArrayInputStream
包含一個內部緩衝區,該緩衝區包含從流中讀取的字節。內部計數器跟蹤
read
方法要提供的下一個字節。 關閉 ByteArrayInputStream 無效。此類中的方法在關閉此流後仍可被調用,而不會產生任何
IOException。
可見兩個流都不需要close()操作
用流的讀寫來操作數組