国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

JAVA--IO

2019-11-14 21:00:36
字體:
來源:轉載
供稿:網友
java--IO

  經過一天的學習,基本已經對JAVA--IO有了一定的認識,現在就和大家分享一下。

  JAVA.IO包中定義了多種流的類型(類或抽象類)來實現輸入和輸出功能,可以從不同的角度對其進行分類:

  按數據流的方向不同可以分為輸入流和輸出流

  按處理數據單位的不同可以分為字節流和字符流

  按照功能的不同可以分為節點流和處理流

上圖就是JAVA--io中流的分類;看到上圖你一定想問輸入流和輸出流是以什么來區分的,輸入流和輸出流均是以程序的角度來度量的。

閑話少聊,現在直奔主題:

一、讀取指定文件中的內容(以字節流的方式讀取)

public class testFileInputstream {    /**文件輸入流(字節流輸入)     * @param args     */    public static void main(String[] args) {        FileInputStream input = null;        int b = 0;        try {            input = new FileInputStream("E://jsp//jsp_demo001//index.jsp");        } catch (FileNotFoundException e) {            System.out.

代碼不難,大家如有疑問請留言。

二、讀取指定文件中的內容(以字符流的方式讀取)

public class Filereader {    /**文件輸入流(字符流輸入)     * @param args     */    public static void main(String[] args) {        FileReader input = null;        int b = 0;        try {            input = new FileReader("E://JSP//jsp_demo001//index.jsp");        } catch (FileNotFoundException e) {            System.out.println("文件不存在");            System.exit(0);        }        long num = 0;        try {            while((b=input.read())!=-1){                System.out.print((char)b);                num++;            }            input.close();            System.out.println();            System.out.println("共讀取:"+num+"個字節");        } catch (IOException e) {            e.printStackTrace();        }    }}

在這里給大家說明一點,字節流和字符流的區別,字節流就是一個字符一個字符的來讀;字符流就不同的,它是一個字符一個字符的來讀;

三、文件輸出流(以字節的方式)

public class testFileOutputStream {    /**文件輸出流(字節流)     * @param args     */    public static void main(String[] args) {        int b = 0;        FileInputStream input = null;        FileOutputStream output = null;        try {            input = new FileInputStream("E://JSP//jsp_demo001//index.jsp");            //如果指定路徑下不存在該文件,系統會自動為我們創建一個文件            output = new FileOutputStream("E://JSP//jsp_demo001//index1.jsp");            //設置為true的話,會在指定文件下進行復制,不刪除原文件            //output = new FileOutputStream("E://JSP//jsp_demo001//index1.jsp", true);        } catch (FileNotFoundException e) {            System.out.println("指定文件不存在");            System.exit(0);        }        try {            while((b=input.read())!=-1){                output.write((char)b);            }            input.close();            output.close();        } catch (IOException e) {            System.out.println("文件復制錯誤");            System.exit(0);        }        System.out.println("文件復制完成");    }}

四、文件輸出流(以字符的方式)

public class Filewriter {    /**文件輸入流(字符流輸出)     * @param args     */    public static void main(String[] args) {        FileWriter output = null;            try {                output = new FileWriter("E://JSP//jsp_demo001//cn.txt");            } catch (IOException e) {                System.out.println("文件路徑有問題");                System.exit(0);            }        try {                for(int i=0 ; i<50000; i++){                    output.write(i);                }            output.close();            System.out.println("保存完成");        } catch (IOException e) {            e.printStackTrace();        }    }}

最后給大家介紹一個關于視頻文件的讀取和保存:

public class FileOutputStream_media {    /**文件輸出流(字節流)     * @param args     */    public static void main(String[] args) {        int b = 0;        FileInputStream input = null;        FileOutputStream output = null;        try {            input = new FileInputStream("E://JSP//jsp_demo001//move.avi");            //如果指定路徑下不存在該文件,系統會自動為我們創建一個文件            output = new FileOutputStream("E://JSP//jsp_demo001//move1.avi");        } catch (FileNotFoundException e) {            System.out.println("指定文件不存在");            System.exit(0);        }        try {            while((b=input.read())!=-1){                output.write((char)b);            }            input.close();            output.close();        } catch (IOException e) {            System.out.println("文件復制錯誤");            System.exit(0);        }        System.out.println("文件復制完成");    }}

我不喜歡長篇大論的文字,就喜歡這種直接完整的代碼,當然上面的每個地方我都做了注釋,如果在你的學習過程中有什么疑問,請留言,我一定會盡快給你答復。

在此聲明:以上內容如有不當,請及時留言,謝謝。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永嘉县| 宁晋县| 临湘市| 东港市| 翁源县| 建阳市| 河北省| 九江县| 山阳县| 如皋市| 来宾市| 勐海县| 泽库县| 民乐县| 禄丰县| 威海市| 安达市| 梅河口市| 屏东市| 灵武市| 交城县| 长武县| 都昌县| 辰溪县| 鄂尔多斯市| 西丰县| 和顺县| 海林市| 宁阳县| 信阳市| 灵川县| 滨州市| 海兴县| 乌恰县| 名山县| 马鞍山市| 东兰县| 湟中县| 杭锦旗| 图木舒克市| 柳江县|