最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java IO流文件的讀寫(xiě)具體實(shí)例

 更新時(shí)間:2013年12月20日 17:24:44   作者:  
這篇文章主要介紹了java IO流文件的讀寫(xiě)具體實(shí)例,有需要的朋友可以參考一下

引言:

關(guān)于java IO流的操作是非常常見(jiàn)的,基本上每個(gè)項(xiàng)目都會(huì)用到,每次遇到都是去網(wǎng)上找一找就行了,屢試不爽。上次突然一個(gè)同事問(wèn)了我java文件的讀取,我一下子就懵了第一反應(yīng)就是去網(wǎng)上找,雖然也能找到,但自己總感覺(jué)不是很踏實(shí),所以今天就抽空看了看java IO流的一些操作,感覺(jué)還是很有收獲的,順便總結(jié)些資料,方便以后進(jìn)一步的學(xué)習(xí)...

IO流的分類(lèi)
1、根據(jù)流的數(shù)據(jù)對(duì)象來(lái)分:
高端流:所有的內(nèi)存中的流都是高端流,比如:InputStreamReader 
低端流:所有的外界設(shè)備中的流都是低端流,比如InputStream,OutputStream
如何區(qū)分:所有的流對(duì)象的后綴中包含Reader或者Writer的都是高端流,反之,則基本上為低端流,不過(guò)也有例外,比如PrintStream就是高端流

2、根據(jù)數(shù)據(jù)的流向來(lái)分:
輸出流:是用來(lái)寫(xiě)數(shù)據(jù)的,是由程序(內(nèi)存)--->外界設(shè)備
輸入流:是用來(lái)讀數(shù)據(jù)的,是由外界設(shè)備--->程序(內(nèi)存)
如何區(qū)分:一般來(lái)說(shuō)輸入流帶有Input,輸出流帶有Output 

3、根據(jù)流數(shù)據(jù)的格式來(lái)分:
字節(jié)流:處理聲音或者圖片等二進(jìn)制的數(shù)據(jù)的流,比如InputStream
字符流:處理文本數(shù)據(jù)(如txt文件)的流,比如InputStreamReader 
如何區(qū)分:可用高低端流來(lái)區(qū)分,所有的低端流都是字節(jié)流,所有的高端流都是字符流

4、根據(jù)流數(shù)據(jù)的包裝過(guò)程來(lái)分:
原始流:在實(shí)例化流的對(duì)象的過(guò)程中,不需要傳入另外一個(gè)流作為自己構(gòu)造方法的參數(shù)的流,稱(chēng)之為原始流。
包裝流:在實(shí)例化流的對(duì)象的過(guò)程中,需要傳入另外一個(gè)流作為自己構(gòu)造方法發(fā)參數(shù)的流,稱(chēng)之為包裝流。
如何區(qū)分:所以的低端流都是原始流,所以的高端流都是包裝流

IO流對(duì)象的繼承關(guān)系(如下圖):

下面來(lái)看一些具體的代碼例子:

按字節(jié)來(lái)讀取文件

復(fù)制代碼 代碼如下:

public class ReadFromFile {
    /**
     * 以字節(jié)為單位讀取文件,常用于讀二進(jìn)制文件,如圖片、聲音、影像等文件。
     */
    public static void readFileByBytes(String fileName) {
        File file = new File(fileName);
        InputStream in = null;
        try {
            System.out.println("以字節(jié)為單位讀取文件內(nèi)容,一次讀一個(gè)字節(jié):");
            // 一次讀一個(gè)字節(jié)
            in = new FileInputStream(file);
            int tempbyte;
            while ((tempbyte = in.read()) != -1) {
                System.out.print(tempbyte);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        try {
            System.out.println("以字節(jié)為單位讀取文件內(nèi)容,一次讀多個(gè)字節(jié):");
            // 一次讀多個(gè)字節(jié)
            byte[] tempbytes = new byte[100];
            int byteread = 0;
            in = new FileInputStream(fileName);
            ReadFromFile.showAvailableBytes(in);
            // 讀入多個(gè)字節(jié)到字節(jié)數(shù)組中,byteread為一次讀入的字節(jié)數(shù)
            while ((byteread = in.read(tempbytes)) != -1) {
                System.out.print(tempbytes, 0, byteread);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
    }

按字符來(lái)讀取文件

復(fù)制代碼 代碼如下:

  /**
     * 以字符為單位讀取文件,常用于讀文本,數(shù)字等類(lèi)型的文件
     */
    public static void readFileByChars(String fileName) {
        File file = new File(fileName);
        Reader reader = null;
        try {
            System.out.println("以字符為單位讀取文件內(nèi)容,一次讀一個(gè)字符:");
            // 一次讀一個(gè)字符
            reader = new InputStreamReader(new FileInputStream(file));
            int tempchar;
            while ((tempchar = reader.read()) != -1) {
                // 對(duì)于windows下,\r\n這兩個(gè)字符在一起時(shí),表示一個(gè)換行。
                // 但如果這兩個(gè)字符分開(kāi)顯示時(shí),會(huì)換兩次行。
                // 因此,屏蔽掉\r,或者屏蔽\n。否則,將會(huì)多出很多空行。
                if (((char) tempchar) != '\r') {
                    System.out.print((char) tempchar);
                }
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            System.out.println("以字符為單位讀取文件內(nèi)容,一次讀多個(gè)字符:");
            // 一次讀多個(gè)字符
            char[] tempchars = new char[30];
            int charread = 0;
            //由于要以字符來(lái)讀取,所以需要套上字符流
            reader = new InputStreamReader(new FileInputStream(fileName));
            // 讀入多個(gè)字符到字符數(shù)組中,charread為一次讀取字符數(shù)
            while ((charread = reader.read(tempchars)) != -1) {
                // 同樣屏蔽掉\r不顯示
                if ((charread == tempchars.length)
                        && (tempchars[tempchars.length - 1] != '\r')) {
                    System.out.print(tempchars);
                } else {
                    for (int i = 0; i < charread; i++) {
                        if (tempchars[i] == '\r') {
                            continue;
                        } else {
                            System.out.print(tempchars[i]);
                        }
                    }
                }
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

按行來(lái)讀取文件

復(fù)制代碼 代碼如下:

  /**
     * 以行為單位讀取文件,常用于讀面向行的格式化文件
     */
    public static void readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行:");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次讀入一行,直到讀入null為文件結(jié)束
            while ((tempString = reader.readLine()) != null) {
                // 顯示行號(hào)
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

將一個(gè)文件的內(nèi)容寫(xiě)入另一個(gè)文件(按行來(lái)寫(xiě))

復(fù)制代碼 代碼如下:

public class FileTest {
public static void main(String[] args)   {
 File  file=new File("c:\\test.txt");
 BufferedReader read=null;
 BufferedWriter writer=null;
 try {
   writer=new BufferedWriter(new  FileWriter("c:\\zwm.txt"));
 } catch (IOException e1) {
  e1.printStackTrace();
 }
 try {
   read=new BufferedReader(new  FileReader(file));
   String tempString = null;
   while((tempString=read.readLine())!=null){
    writer.append(tempString);
    writer.newLine();//換行
    writer.flush();//需要及時(shí)清掉流的緩沖區(qū),萬(wàn)一文件過(guò)大就有可能無(wú)法寫(xiě)入了
   }
   read.close();
   writer.close();
   System.out.println("文件寫(xiě)入完成...");
 } catch (IOException e) {
  e.printStackTrace();
 }

}
}

相關(guān)文章

最新評(píng)論

遂宁市| 开江县| 河南省| 泗阳县| 云龙县| 仙游县| 长白| 内黄县| 碌曲县| 汉沽区| 山丹县| 柯坪县| 嵊泗县| 醴陵市| 偃师市| 洞头县| 财经| 延长县| 陈巴尔虎旗| 灵石县| 鸡西市| 岚皋县| 德庆县| 恩平市| 府谷县| 镇康县| 清水县| 灵武市| 丰城市| 阿合奇县| 陆河县| 安远县| 岐山县| 龙陵县| 怀化市| 太和县| 汕头市| 中卫市| 中阳县| 防城港市| 峡江县|