Java之------IO加強
來源:程序員人生 發(fā)布時間:2016-06-13 11:36:27 閱讀次數(shù):2722次
RandomAccessFile
1、隨機訪問文件,本身具有讀寫的方法
new RandomAccessFile()以后,若文件不存在會自動創(chuàng)建,存在則不創(chuàng)建?!擃惼鋵崈?nèi)部既封裝了字節(jié)輸入流,又封裝了字節(jié)輸出流。
該類若用write()方法寫整數(shù),每次只寫它的最后1個字節(jié)。而采取writeInt()方法,則可把1個整數(shù)完全地寫入。
2、通過skipBytes(int x),seek(int x)來到達隨機訪問
通過seek方法設置數(shù)據(jù)的指針就能夠?qū)崿F(xiàn)對文件數(shù)據(jù)的隨機讀寫。InputStream中的skip()方法只能從頭往后跳,不能反向;而seek()方法可雙向隨意定位。
3、數(shù)據(jù)修改方面的特點
用RandomAccessFile類可以實現(xiàn)數(shù)據(jù)的修改,固然文件中的數(shù)據(jù)1般要有規(guī)律,以方便在編程時能夠進行定位,讓數(shù)據(jù)寫對地方。 而用“流”實現(xiàn)數(shù)據(jù)修改時,則通常需要把數(shù)據(jù)從流讀到數(shù)組當中,在數(shù)組中進行數(shù)據(jù)修改,然后再把修改后的數(shù)組
再重新寫到流中。
序列化
★ 序列化
將1個對象寄存到某種類型的永久存儲器上稱為保持。如果1個對象可以被寄存到磁盤或磁帶上,或可以發(fā)送到另外1臺機器并寄存到存儲器或磁盤上,那末這個對象就被稱為可保持的。(在Java中,序列化、持久化、串行化是1個概念。)
java.io.Serializable接口沒有任何方法,它只作為1個“標記者”,用來表明實現(xiàn)了這個接口的類可以斟酌串行化。類中沒有實現(xiàn)Serializable的對象不能保存或恢復它們的狀態(tài)。
★ 對象圖
當1個對象被串行化時,只有對象的數(shù)據(jù)被保存;方法和構(gòu)造函數(shù)不屬于串行化流。如果1個數(shù)據(jù)變量是1個對象,那末這個對象的數(shù)據(jù)成員也會被串行化。樹或?qū)ο髷?shù)據(jù)的結(jié)構(gòu),包括這些子對象,構(gòu)成了對象圖。
★ 瞬時 transient
避免對象的屬性被序列化。
Address.java
package io.serializable;
import java.io.Serializable;
public class Address implements Serializable {
//靜態(tài)變量是不會被序列化的。對非靜態(tài)變量,1般情況下都會被序列化,但如果聲明成transient型則不會。
transient int num;//瞬時變量---該變量是不會被序列化的---不會出現(xiàn)在對象圖中的
private String name;
private int age;
private String tel;
public Address(String name, int age, String tel) {
super();
this.name = name;
this.age = age;
this.tel = tel;
}
public Address(int num, String name, int age, String tel) {
super();
this.num = num;
this.name = name;
this.age = age;
this.tel = tel;
}
@Override
public String toString() {
return "Address [num=" + num + ", name=" + name + ", age=" + age
+ ", tel=" + tel + "]";
}
}
SerializableDemo.java
package io.serializable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
public class SerializableDemo {
public static void main(String[] args) throws Exception {
//demo1();
demo2();
}
private static void demo2() throws FileNotFoundException, IOException,
ClassNotFoundException {
//對象序列化---輸出,寫
OutputStream fout = new FileOutputStream("a.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject( new Address(1,"aa",23,"13588888888"));
out.writeObject( new Address(2,"bb",24,"13566666666"));
out.writeObject( new Address(3,"cc",23,"13577777777"));
out.writeObject( new Address(4,"dd",25,"13599999999"));
out.close();
//反序列化----讀
InputStream fin = new FileInputStream("a.txt");
ObjectInputStream in = new ObjectInputStream(fin);
System.out.println( in.readObject() );
System.out.println( in.readObject() );
System.out.println( in.readObject() );
System.out.println( in.readObject() );
}
private static void demo1() throws FileNotFoundException, IOException,
ClassNotFoundException {
//對象序列化---輸出,寫
OutputStream fout = new FileOutputStream("a.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject( new Address("aa",23,"13588888888"));
out.writeObject( new Address("bb",24,"13566666666"));
out.writeObject( new Address("cc",23,"13577777777"));
out.writeObject( new Address("dd",25,"13599999999"));
out.close();
//反序列化----讀
InputStream fin = new FileInputStream("a.txt");
ObjectInputStream in = new ObjectInputStream(fin);
System.out.println( in.readObject() );
System.out.println( in.readObject() );
System.out.println( in.readObject() );
System.out.println( in.readObject() );
}
}
緩沖輸入輸出流
(BufferedInputStream和BufferedOutputStream)
方式1:
DataInputStream in = new DataInputStream(
new BufferedInputStream(
new FileInputStream("Test.txt") );
方式2:
DataInputStream in = new DataInputStream(
new FileInputStream("Test.txt") );
方式3:
BufferedInputStream in = new BufferedInputStream(
new DataInputStream(
new FileInputStream("Test.java") );
☆示例測試技術總結(jié):方案1是最優(yōu)的
1)有buffer比沒有更快;
2)buffer放在中間層包裝比放在外層更快;
3)按行或按塊操作 比 按字節(jié)或字符操作更快(用Object流操作的速度 比 字節(jié)字符方式 更快)
4)緩沖區(qū)要結(jié)合流才可使用,在流的基礎上對流的功能進行了增強。
package io.buffer;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
public class BufferedStreamDemo {
public static void main(String[] args) {
try {
//test1();
//test2();
test3();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void test1() throws Exception {
long t1 = System.currentTimeMillis();
DataInputStream din = new DataInputStream(new BufferedInputStream(
new FileInputStream("b.txt")));
String str = null;
while ((str = din.readLine()) != null) {
System.out.println(str);
}
long t2 = System.currentTimeMillis();
System.out.println("方式1運行時間(毫秒):" + (t2 - t1));
}
private static void test2() throws Exception {
long t1 = System.currentTimeMillis();
DataInputStream din = new DataInputStream(
new FileInputStream("b.txt"));
String str = null;
while ((str = din.readLine()) != null) {
System.out.println(str);
}
long t2 = System.currentTimeMillis();
System.out.println("方式2運行時間(毫秒):" + (t2 - t1));
}
private static void test3() throws Exception {
long t1 = System.currentTimeMillis();
BufferedInputStream bin = new BufferedInputStream(
new DataInputStream(
new FileInputStream("b.txt")) );
byte buf[] = new byte[20];
int n=0;
while ((n = bin.read(buf)) != ⑴) {
System.out.println(new String(buf,0,n));
}
long t2 = System.currentTimeMillis();
System.out.println("方式3運行時間(毫秒):" + (t2 - t1));
}
}
轉(zhuǎn)換流
(InputStreamReader和OutputStreamWriter)
★轉(zhuǎn)換流功能1:充當字節(jié)流與字符流之間的橋梁
需求:摹擬英文聊天程序,要求:
(1) 從鍵盤錄入英文字符,每錄1行就把它轉(zhuǎn)成大寫輸出到控制臺;
(2) 保存聊天記錄到字節(jié)流文件。
要求1的設計分析:
1)需要從鍵盤接收錄入,得用System.in,它是字節(jié)輸入流InputStream;
2)需要處理字符,可以自己把字節(jié)強轉(zhuǎn)成字符,也能夠用字符流;
3)需要類似readLine的功能,而這個方法在字符流BufferedReader中有(而且該類有緩沖增速)。
綜上,采取轉(zhuǎn)換流把字節(jié)流轉(zhuǎn)成字符流處理比較公道,即便用InputStreamReader
要求2的設計分析:
1)需要把字符數(shù)據(jù)按行保存到字節(jié)流文件 ;
2)字符流采取BufferedWriter比較適合,由于它有newLine方法且能實現(xiàn)高效;
3)字節(jié)流文件,得采取FileOutputStream。
綜上,采取轉(zhuǎn)換流把字符流轉(zhuǎn)成字節(jié)流處理比較公道,即便用OutputStreamWriter
package io.transfer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class TranStreamDemo {
public static void main(String[] args) throws IOException {
//輸入
InputStream in = System.in;
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
//輸出
OutputStream out = new FileOutputStream("chat.txt");
OutputStreamWriter osw = new OutputStreamWriter(out);
BufferedWriter bw = new BufferedWriter(osw);
String line = null;
while( (line=br.readLine())!=null){
if("over".equals(line)){//養(yǎng)成好的代碼習慣:調(diào)用String中的方法時,把常量字符串放在前面,避免變量為null而致使異常
break;
}
System.out.println( line.toUpperCase() );
bw.write(line);
bw.newLine();
bw.flush();//字符流是帶緩沖的,必須刷緩沖
}
}
}
★轉(zhuǎn)換流功能2:字符編碼轉(zhuǎn)換
采取FileWriter以默許方式編碼
FileOutputStream+默許編碼表
采取轉(zhuǎn)換流以默許方式編碼
OutputStreamWriter + FileOutputStream + 默許編碼表
采取轉(zhuǎn)換流以指定編碼方式編碼
OutputStreamWriter + FileOutputStream +指定編碼表
采取轉(zhuǎn)換流以指定編碼方式解碼
InputStreamReader + FileInputStream +指定編碼表
package io.transfer;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class TranStreamDemo2 {
public static void main(String[] args) {
try {
//readTextDecoding1();
//readTextDecoding2();
writeTextEncoding();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writeTextEncoding() throws IOException {
//第1種: FileWriter+默許編碼表
FileWriter fw = new FileWriter("files\\w_utf⑻.txt");//該文件的編碼由平臺(如MyEclipse或dos窗口)定,不1定是utf⑻
fw.write("每天進步1點點...");
fw.close();
//第2種: OutputStreamWriter+默許編碼表
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("files\\w_utf⑻_2.txt"));//該文件的編碼由平臺(如MyEclipse或dos窗口)定,不1定是utf⑻
osw.write("第天進步1點點...");//牛耳
osw.close();
//第3種: OutputStreamWriter+指定編碼表
OutputStreamWriter osw2 = new OutputStreamWriter(new FileOutputStream("files\\w_utf⑻_3.txt"),"utf⑻");//該文件的編碼1定是utf⑻,由于是我們自己指定的
osw2.write("第天進步1點點...");
osw2.close();
}
private static void readTextDecoding1() throws IOException {
FileReader fr = new FileReader("files\\utf⑻.txt");//采取默許編碼表解碼
char[] cbuf = new char[10];
int len=0;
while( (len=fr.read(cbuf))!=⑴){
String str = new String(cbuf,0,len);
System.out.print(str);
}
fr.close();
}
private static void readTextDecoding2() throws IOException {
//InputStreamReader isr = new InputStreamReader(new FileInputStream("files\\gbk.txt"));//如果不指定編碼表,則是采取默許的
//用轉(zhuǎn)換流自己指定解碼表----只要文件的編碼表和這里指定的解碼表相同,就不會出現(xiàn)亂碼
//InputStreamReader isr = new InputStreamReader( new FileInputStream("files\\gbk.txt"), "gbk"); //ok
//InputStreamReader isr = new InputStreamReader( new FileInputStream("files\\utf⑻.txt"), "gbk");//亂碼
InputStreamReader isr = new InputStreamReader( new FileInputStream("files\\utf⑻.txt"), "utf⑻");//ok
char[] cbuf = new char[20];
int len = isr.read(cbuf);
String str = new String(cbuf,0,len);
System.out.println(str);
isr.close();
}
}
打印流
(PrintStream和PrintWriter)
★打印流的特點:
1)只有輸出沒有輸入。PrintStream是字節(jié)打印流,PrintWriter是字符打印流。
2)能夠方便地打印各種數(shù)據(jù)“值表示情勢”,提供了1系列的打印功能(只有它有,其它流都沒有。)
3)和其他輸出流不同,它永久不會拋出IOException異常(構(gòu)造方法除外),異常內(nèi)部解決且設置了內(nèi)部標志。
4)可創(chuàng)建具有自動刷新的功能,可以使用帶換行符的println()方法。
5)(在構(gòu)造方法中)可以指定字符集編碼的。
package io.print;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
//System.out就是PrintStream類型
public class PrintStreamDemo {
public static void main(String[] args) {
try {
demo1();
demo2();
for(int i=0;i<10;i++){
System.out.println(i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//把System.out的輸出目的地從屏幕更改到日志文件
private static void demo2() throws IOException {
FileOutputStream fout = new FileOutputStream("log.txt");
PrintStream out = new PrintStream(fout, true);
System.setOut(out);
}
private static void demo1() throws IOException {
//
PrintStream out = new PrintStream("print.txt");
//out.write()只寫入1個字節(jié)的信息,如果參數(shù)大于1個字節(jié)的范圍,那末實際上只會寫入最后1個字節(jié)的數(shù)據(jù)
//out.write(97);
out.write(353);//最后1個字節(jié)是97,因此寫入的是1個字符'a'----寫入的是值的表現(xiàn)情勢
//System.out.write(353);//輸出'a'
//System.out.flush();
out.println(345);//把參數(shù)轉(zhuǎn)換成字符串輸出
//上1句等價于out.write( String.valueOf(i) )
//※總之,PrintStream中用write()輸出的是字節(jié)數(shù)據(jù)且每次只輸出1個字節(jié),而print()輸出的是數(shù)據(jù)的值的表現(xiàn)情勢即轉(zhuǎn)換成字符串輸出。
//JSP中的out對象就是這類類型。要輸出字節(jié)數(shù)據(jù)如圖片聲明等2進制格式則必須用write(),而輸出頁面數(shù)據(jù)(字符)則要用print()或println()
}
}
★關于打印流的自動刷新
只有遇到結(jié)束字符(換行符)時才會自動刷新,如在調(diào)用其中1個println方法或?qū)懭霌Q行符或字節(jié)('\n)時會自動刷新輸出緩沖區(qū)。
package io.print;
import java.io.IOException;
import java.io.PrintWriter;
//演示PrintStream類的自動刷新功能
public class PrintStreamDemo2 {
public static void main(String[] args) {
try {
//demo1();
demo2();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void demo1() throws IOException {
//默許不自動刷新的
PrintWriter out = new PrintWriter(System.out);
out.print("Hello World");//不會自動刷新
out.println("Hello World");//不會自動刷新
out.flush();//手動刷新
}
private static void demo2() throws IOException {
//設置自動刷新的
PrintWriter out = new PrintWriter(System.out,true);
out.print("Hello World");//不會自動刷新
out.println("Hello World");//會----由于println()內(nèi)部調(diào)用了out.flush()
out.print("Hello3 \n");//不會
out.print("Hello3 \r\n");//不會
out.printf("%s", "Hello4");//會
/*總之:
* autoFlush - boolean 變量;如果為 true,則 println、printf 或 format 方法將刷新輸出緩沖區(qū)。
* ---實際上是由于這幾個方法中幫我們調(diào)用了out.flush()。
*/
}
}
IO包中的其他流
★字節(jié)數(shù)組流
ByteArrayInputStream與ByteArrayOutputStream
package io.array;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public class ByteArrayStreamDemo {
public static void main(String[] args) {
String str ="adhjsdhhsd";
ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch=0;
while( (ch=bis.read())!=⑴ ){
bos.write(ch);
}
System.out.println(bos.toString());
}
}
★字符數(shù)組流
CharArrayReader與CharArrayWriter
★字符串流
StringReader 與 StringWriter
★序列流
SequenceInputStream ——對多個流進行合并
將多個流進行邏輯串連(合并變成1個流,操作起來很方便,由于多個源變成了1個源)
package io.sequence;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
public class SequenceInputStreamDemo {
public static void main(String[] args) throws IOException {
FileInputStream fin1 = new FileInputStream("files\\seq1.txt");
FileInputStream fin2 = new FileInputStream("files\\seq2.txt");
FileInputStream fin3 = new FileInputStream("files\\seq3.txt");
ArrayList<FileInputStream> v = new ArrayList<FileInputStream>();
v.add(fin1);
v.add(fin2);
v.add(fin3);
Enumeration<FileInputStream> en = Collections.enumeration(v);
SequenceInputStream sis = new SequenceInputStream(en);
//創(chuàng)建輸出流---由于要把前3個文件中的內(nèi)容讀取出來合并到 seq4.txt文件
FileOutputStream fos = new FileOutputStream("files\\seq4.txt");
int len = 0;
byte buf[] = new byte[10];
while((len=sis.read(buf))!=⑴){
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
}
IO流知識點小結(jié)
1、知識點
a、流是用來處理數(shù)據(jù)的。
b、處理數(shù)據(jù)時,1定要先明確數(shù)據(jù)源與數(shù)據(jù)目的地(數(shù)據(jù)匯)。
c、數(shù)據(jù)源可以是文件、鍵盤或其他流。
d、數(shù)據(jù)目的地可以是文件、顯示器或其他流。
e、流只是在幫助數(shù)據(jù)進行傳輸,并對傳輸?shù)臄?shù)據(jù)進行處理,比如過濾處理、轉(zhuǎn)換處理等。
2、IO流體系
使用要點:看頂層(父類共性功能),用底層(子類具體對象)。
命名規(guī)律:每一個子類的后綴名都是所屬體系的父類的名稱,很容易辨別所屬的體系。
而且每個子類前綴名都是該子類對象的功能體現(xiàn)。
(掌握IO流體系的要點和規(guī)律,開發(fā)時設計與查找相應的類就容易多了)
IO流的操作規(guī)律
1、明確源和目的。
源:InputStream Reader 1定是被讀取的。
目的:OutputStream Writer 1定是被寫入的。
2、處理的數(shù)據(jù)是不是是純文本的數(shù)據(jù)?
是:使用字符流。Reader Writer
否:使用字節(jié)流。 InputStream OutputStream
(到這里,兩個明確肯定完,就能夠肯定出要使用哪一個體系。接下來,就應當明確具體這個體系要使用哪一個具體的對象?!舅^的看頂層】)
3、明確數(shù)據(jù)所在的裝備。
源裝備:
鍵盤(System.in)
硬盤(FileXXX)FileReader FileInputStream
內(nèi)存(數(shù)組)ByteArrayInputStream CharArrayReader StringReader
網(wǎng)絡(Socket)
目的裝備:
顯示器(控制臺System.out)
硬盤(FileXXX)FileWriter FileOutputStream
內(nèi)存(數(shù)組)ByteArrayOutputStream CharArrayWriter StringWriter
網(wǎng)絡(Socket)
(到這里,具體使用哪一個對象就能夠明確了?!舅^的用底層】)
4、明確是不是需要額外功能?
1) 是不是需要高效?緩沖區(qū)Buffered (字符與字節(jié)各兩個)
2) 是不是需要轉(zhuǎn)換?轉(zhuǎn)換流 InputStreamReader OutputStreamWriter
3) 是不是操作基本數(shù)據(jù)類型? DataInputStream DataOutputStream
4) 是不是操作對象(對象序列化)? ObjectInputStream ObjectOutputStream
5) 需要對多個源合并嗎? SequenceInputStream
6) 需要保證數(shù)據(jù)的表現(xiàn)情勢到目的地嗎? PrintStream 或 PrintWriter
(到這里,具體的設計方案就能夠明確了?!咎捉优c功能加強】)
IO流的操作規(guī)律之設計方案練習
需求1:復制1個文本文件。
1、明確源和目的。
源:InputStream Reader
目的:OutputStream Writer
2、處理的數(shù)據(jù)是不是是純文本的數(shù)據(jù)?
源:Reader
目的:Writer
3、明確數(shù)據(jù)所在的裝備。
源:file(硬盤) FileReader fr = new FileReader("a.txt");
目的:file(硬盤) FileWriter fw = new FileWriter("b.txt");
4、明確是不是需要額外功能?
BufferedReader bufr = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bufw = new BufferedWriter(new FileWriter("b.txt"));
需求2:復制1個圖片文件。
1、明確源和目的。
源:InputStream Reader
目的:OutputStream Writer
2、處理的數(shù)據(jù)是不是是純文本的數(shù)據(jù)?
源:Reader
目的:Writer
3、明確數(shù)據(jù)所在的裝備。
源:file(硬盤) FileReader fr = new FileReader("a.txt");
目的:file(硬盤) FileWriter fw = new FileWriter("b.txt");
4、明確是不是需要額外功能?
BufferedReader bufr = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bufw = new BufferedWriter(new FileWriter("b.txt"));
需求3:讀取鍵盤錄入,存儲到1個文件中。
1、明確源和目的。
源:InputStream Reader
目的:OutputStream Writer
2、處理的數(shù)據(jù)是不是是純文本的數(shù)據(jù)?
源:Reader
目的:Writer
3、明確數(shù)據(jù)所在的裝備。
源:file(硬盤) InputStream in = System.in; 緣由:必須要將鍵盤錄入的字節(jié)轉(zhuǎn)成字符。需要將字節(jié)-->字符的轉(zhuǎn)換流。InputStreamReader
目的:file(硬盤) FileWriter fw = new FileWriter("b.txt");
4、明確是不是需要額外功能?
InputStreamReader isr = new InputStreamReader(System.in);
FileWriter fw = new FileWriter("a.txt");
高效:BufferedReader bufr = new BufferedReader( isr);
BufferedWriter bufw = new BufferedWriter( fw );
需求4:讀取1個文本文件,顯示到顯示器上。
1、明確源和目的。
源:InputStream Reader
目的:OutputStream Writer
2、處理的數(shù)據(jù)是不是是純文本的數(shù)據(jù)?
源:Reader
目的:Writer
3、明確數(shù)據(jù)所在的裝備。
源:file(硬盤) FileReader fr = new FileReader("a.txt");
目的:顯示器 OutputStream out = System.out; 緣由:要將字符數(shù)據(jù)轉(zhuǎn)換成字節(jié)輸出。輸出轉(zhuǎn)換流:OutputStreamWriter
4、明確是不是需要額外功能?
FileReader fr = new FileReader("a.txt");
OutputStreamWriter osw = new OutputStreamWriter(System.out);
高效:BufferedReader bufr = new BufferedReader( fr);
BufferedWriter bufw = new BufferedWriter( osw );
需求5:讀取1個文本文件,將文本依照指定的編碼表UTF⑻寫入到另外一個文件中
1、明確源和目的。
源:InputStream Reader
目的:OutputStream Writer
2、處理的數(shù)據(jù)是不是是純文本的數(shù)據(jù)?
源:Reader
目的:Writer
3、明確數(shù)據(jù)所在的裝備。
源:file(硬盤) FileReader fr = new FileReader("a.txt");
目的:file(硬盤) FileOutputStream fout = new FileOutputStream("b.txt")緣由:假定輸出時要為字符數(shù)據(jù)指定編碼表。轉(zhuǎn)換流中的參數(shù)需要字節(jié)流,因此用轉(zhuǎn)換流:FileOutputStream。
4、明確是不是需要額外功能?
FileReader fr = new FileReader("a.txt");
OutputStreamWriter osw = new OutputStreamWriter(fout,”utf⑻”);
高效:BufferedReader bufr = new BufferedReader( fr);
BufferedWriter bufw = new BufferedWriter( osw );
兩大例題
文件切割
點擊打開鏈接
字符串截取
點擊打開鏈接
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈