Java的IO操作(二) - 带缓冲区的流对象、写入基本数据类型、实现命令行中的copy命令...
在上一節(jié)中,我們使用FileInputStream類和FileOutputStream類來實現(xiàn)了一個可以自由拷貝文件的功能。為了提高效率,我們?nèi)藶榈囟x一個緩沖區(qū)byte[] 數(shù)組。其實,我們可以使用BufferedInputStream類和BufferedOutputStream類來重寫這個功能。
5、BufferedInputStream、BufferedOutputStream
看到Buffererd這個詞,我們或許可以猜到,這兩個類應(yīng)該是帶有緩沖區(qū)的流類。正如我們所想的那樣,它們確實有一個buf數(shù)據(jù)成員,是一個字符數(shù)組,默認(rèn)大小為2048字節(jié)。當(dāng)我們在讀取數(shù)據(jù)時,BufferedInputStream會盡量將buf填滿;使用read()方法讀取數(shù)據(jù)時,實際上是先從buf中讀取數(shù)據(jù),而不是直接從數(shù)據(jù)來源(如硬盤)上讀取。只有當(dāng)buf中的數(shù)據(jù)不足時,BufferedInputStream才會調(diào)用InputStream的read()方法從指定數(shù)據(jù)源中讀取。
BufferedOutputStream的數(shù)據(jù)成員buf是一個512字節(jié)的字節(jié)數(shù)組,當(dāng)我們調(diào)用write()方法寫入數(shù)據(jù)時,實際上是先向buf中寫入,當(dāng)buf滿后才會將數(shù)據(jù)寫入至指定設(shè)備(如硬盤)。我們也可以手動地調(diào)用flush()函數(shù)來刷新緩沖區(qū),強制將數(shù)據(jù)從內(nèi)存中寫出。
下面用這兩個類實現(xiàn)文件復(fù)制功能:
package cls;import java.io.*;public class BufferedStreamDemo {public static void main(String[] args) throws Exception{// 從命令行參數(shù)中指定文件File fSource = new File(args[0]);File fDest = new File(args[1]);// 創(chuàng)建帶緩沖的流對象BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fSource));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fDest));// 提示信息System.out.println("copy " + fSource.length() + "bytes");byte[] buf = new byte[1];while(bis.read(buf) != -1) // read()返回int類型,返回-1表示已到文件結(jié)尾bos.write(buf); // 寫入數(shù)據(jù)// 刷新緩沖區(qū)bos.flush();// 關(guān)閉流bos.close();bis.close();// 提示信息System.out.println("copy " + fDest.length() + "bytes finished");} }6、DataInputStream和DataOutputStream
DataInputStream和DataOutputStream類提供了對Java基本數(shù)據(jù)類型寫入的方法,如int,double,boolean。因為Java中基本數(shù)據(jù)類型的大小是固定的,不會因為不同的機器而改變,因此在寫入的時候就不必?fù)?dān)心不同平臺數(shù)據(jù)大小不同的問題。
有一個writeUTF()方法值得我們注意。這個方法會將指定的String對象中的字符寫入,但在寫入的數(shù)據(jù)之前會首先寫入2個字節(jié)的長度數(shù)據(jù),這個數(shù)據(jù)指示了帶寫入的字符的大小。這樣的好處是當(dāng)我們在使用readUTF()讀取數(shù)據(jù)的時候就不必考慮數(shù)據(jù)大小的問題了,直接讀取就行,因為在readUTF()內(nèi)部會控制好讀取數(shù)據(jù)的長度。
package cls;import java.io.*;class Student {String name;int score;// 構(gòu)造方法public Student(String name,int score){this.name = name;this.score = score;}// 返回名字public String getName(){return name;}// 返回分?jǐn)?shù)public int getScore(){return score;} }public class DataStreamDemo {public static void main(String[] args) throws Exception{// 創(chuàng)建3個Student對象Student[] sd = new Student[]{new Student("dog",100),new Student("pig",200),new Student("cat",300)};// 創(chuàng)建輸出流對象DataOutputStream dos = new DataOutputStream(new FileOutputStream(args[0])); //向文件中寫入// 使用增強for循環(huán)寫入數(shù)據(jù)for(Student st : sd){dos.writeUTF(st.getName()); // 寫入Stringdos.writeInt(st.getScore());}dos.flush(); // 刷新緩沖區(qū)dos.close(); // 關(guān)閉流// 從文件中讀入數(shù)據(jù)DataInputStream dis = new DataInputStream(new FileInputStream(args[0]));for(int i = 0 ; i < 3 ; ++i){System.out.println(dis.readUTF()); // 取入String字符串,不必?fù)?dān)心長度的問題System.out.println(dis.readInt());}dis.close();} }轉(zhuǎn)載于:https://www.cnblogs.com/whongfei/archive/2013/03/30/5246989.html
總結(jié)
以上是生活随笔為你收集整理的Java的IO操作(二) - 带缓冲区的流对象、写入基本数据类型、实现命令行中的copy命令...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用到的oracle sql语句-001
- 下一篇: 站着办公有助减轻体重