字節打印流PrintStream,字符打印流PrintWriter,序列流SequenceInputStream、

PrintStream:字節打印流
  1 提供了打印方法可以對多種數據類型值進行打印。並保持數據的表示形式
  2 打印方法不拋IOException
  
  構造函數,接收三種類型的值:
  1 字符串路徑
  2 File對象
  3 字節輸出流
 PrintStream out = new PrintStream("print.txt");
out.write(610);//只寫最低8位
out.print(97);//將97先變成字符保持原樣將數據打印到目的地。
out.close();

PrintWriter:字符打印流。
 * 構造函數參數:
 * 1 字符串路徑
 * 2 File對象
 * 3 字節輸出流
 * 4 字符輸出流

例:將鍵盤輸入的值打印到控制檯
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
String line = null;
while((line = buf.readLine())!=null){
if("over".equals(line))
break;
pw.println(line.toUpperCase());
pw.flush();
}
buf.close();
pw.close();

序列流:合併多個流

Vector<FileInputStream> v = new Vector<FileInputStream>(); 

 v.add(newFileInputStream("1.txt"));
  v.add(new FileInputStream("2.txt"));
  v.add(new FileInputStream("3.txt"));
  Enumeration<FileInputStream> en = v.elements();

SequenceInputStream sis = new SequenceInputStream(en);// 構造方法接收的是枚舉
FileOutputStream fos = new FileOutputStream("4.txt");
byte[] bus = new byte[1024];
int len = 0;
while ((len = sis.read(bus)) != -1) {
fos.write(bus, 0, len);
}
sis.close();
fos.close();

迭代與枚舉:

ArrayList<FileInputStream> a = new ArrayList<FileInputStream>();
for (int x = 0; x < 3; x++) {
a.add(new FileInputStream(x + ".txt"));
}
Enumeration<FileInputStream> en = Collections.enumeration(a);// 集合框架工具類中的方法


原理:
/*final Iterator<FileInputStream> it = a.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {
public boolean hasMoreElements() {
return it.hasNext();
}
public FileInputStream nextElement() {
return it.next();
}
};*/



SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("4.txt");
byte[] bus = new byte[1024];
int len = 0;
while ((len = sis.read(bus)) != -1) {
fos.write(bus, 0, len);
}
sis.close();
fos.close();


文件切割器:
* 思路:用一個讀取流,連接文件。
* 用多個輸出流,按大小寫入到別的文件

public static void splitFile(File file) throws IOException {
//用讀取流關聯源文件
FileInputStream fis = new FileInputStream(file);
//創建1m緩衝區
byte[] bus = new byte[1024*1024];
//創建目的
FileOutputStream fos = null;
int len = 0;
int count = 1;
while((len=fis.read(bus))!=-1){
fos = new FileOutputStream((count++)+".part");
fos.write(bus, 0, len);

}
fos.close();
fis.close();
}



序列化接口:

Serializable:用於給被序列化的類加入ID號。

用於判斷類和對象是否是同一版本。

強烈建議顯示聲明一個ID號


靜態數據與瞬態不能被序列化:

transient:非靜態數據不想被序列化可以使用這個關鍵字修飾。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章