天天看點

Java入門—輸入輸出流

File類的使用

檔案是:檔案可認為是相關記錄或放在一起的資料的集合。

Java中,使用java.io.File類對檔案進行操作

public class FileDemo {
    public static void main(String[] args) {
        String path = "E:\\pdd";
        File f = new File(path);
        //判斷是檔案還是目錄
        System.out.println(f.isFile());
        System.out.println(f.isDirectory());
    }
}
           
Java入門—輸入輸出流

image.png

對于File類還有其他很多方法的使用,建議在使用時進行查詢文檔。

位元組流

  • 位元組輸入流 :InputStream (讀)
  • 位元組輸出流 :OutputStream (寫)
    Java入門—輸入輸出流
Java入門—輸入輸出流

FileInputStream

  • 讀取諸如圖像資料的原始位元組流,如圖檔,檔案中的位元組。
    Java入門—輸入輸出流
Java入門—輸入輸出流
public class FileDemo {
    public static void main(String[] args) {
        String path = "";

        // FileInputStream,
        try {
            FileInputStream fs = new FileInputStream(path);
            try {
                
                int n = fs.read();
                System.out.print((char)n);
                fs.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
           

代碼中注意異常的繼承關系,外層異常不能為内層異常的子類。

FileOutputStream

将資料寫入檔案中。

Java入門—輸入輸出流
Java入門—輸入輸出流

緩沖流

  • BufferedInputStream
  • BufferedOutputStream

    緩沖流,當緩沖區滿了,送出,減少頻繁寫入操作。

public class FileDemo {
    public static void main(String[] args) {
        String path = "";
        try {
            FileOutputStream fos = new FileOutputStream(path);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            FileInputStream fis = new FileInputStream(path);
            BufferedInputStream bis = new BufferedInputStream(fis);

            bos.write(50);
            bos.write('a');
            //送出資料到檔案
            bos.flush();
            bos.close();

            System.out.println(bis.read());
            bis.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
           

字元流

  • 字元輸入流 : Reader
  • 字元輸出流 : Writer

位元組字元轉換流

  • InputStreamReader 輸入
  • OutputStreamWriter 輸出

讀寫資料時保持編碼一緻。

對象的序列化和反序列化

對象序列化步驟:

  1. 建立一個類繼承Serializable接口
  2. 建立對象
  3. 處理對象(寫檔案,發http,入庫等)
  4. 讀取對線
  • ObjectInputStream 對象輸入流
  • ObjectOutputStream 對象輸出流

示例:

建立一個Goods.java檔案,定義一個Goods類并繼承Serializable接口

public class Goods implements Serializable{
    private String goodsId;
    private String goodsName;
    private double price;

    @Override
    public String toString() {
        return "Goods{" +
                "goodsId='" + goodsId + '\'' +
                ", goodsName='" + goodsName + '\'' +
                ", price=" + price +
                '}';
    }

    public Goods(String goodsId, String goodsName, double price){
        this.goodsId = goodsId;
        this.goodsName = goodsName;
        this.price = price;
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
           

再建一個測試入口檔案名為GoodsTest.java

public class GoodsTest {
    public static void main(String[] args) {
        String path = "1.txt";
        Goods gd = new Goods("001", "allen", 6.0);

        try {
            FileOutputStream fos = new FileOutputStream(path);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(gd);
            oos.writeBoolean(true);
            oos.flush();
            oos.close();
            System.out.println("-----------------------------------");
            FileInputStream fis = new FileInputStream(path);
            ObjectInputStream ois = new ObjectInputStream(fis);
            try {
                Goods gt = (Goods)ois.readObject();
                boolean b = ois.readBoolean();
                System.out.println(gt);
                System.out.println(b);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            ois.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
           

對名為1.txt的檔案進行類的存儲和讀取。

注:養成看官方文檔的習慣,因為畢竟有些方法用的頻率很少,當你使用時去查閱即可,文章中關于類的方法部分沒有詳情,請查閱官方手冊,手冊中寫的很清楚。

如果文章對你有幫助記得點贊~,關注作者第一時間獲得最新更新~~。

祝好~~~