大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java文件處理詳解

Java文件處理詳解

更新時間:2022-12-29 11:06:23 來源:動力節點 瀏覽1246次

Java文件處理是什么?動力節點小編來為大家進行詳細介紹。

1.文件操作

import java.io.File;
import java.io.IOException;
public class FileDemo {
    public static void main(String[] args) {
        File file1 = new File("./src/main/resources/test");
        File file2 = new File("./src/main/resources/test/score.txt");
        System.out.println(file1.isDirectory());
        System.out.println(file2.isFile());
        System.out.println(file1.isAbsolute());
        if (! file1.exists()) {
            file1.mkdirs();
        }
        if (! file2.exists()) {
            try {
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2.字節流

字節輸入流InputStream

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
 * 從文件系統中的某個文件中獲得輸入字節
 * 用于讀取諸如圖像數據之類的原始字節流
 */
public class FileInputStreamDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
             fis = new FileInputStream("./src/main/resources/test/score.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (fis != null) {
            // readDemo(fis);
            readByteDemo(fis);
        }
    }
    /**
     * read(byte[] b)
     * read(byte[] b, int off, int len)
     * @param fis
     */
    public static void readByteDemo(FileInputStream fis) {
        byte[] b = new byte[100];
        try {
            fis.read(b, 0, 5);
            System.out.println(new String(b));
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * read()
     * @param fis
     */
    public static void readDemo(FileInputStream fis) {
        try {
            int n = 0;
            while ((n = fis.read()) != -1) {
                System.out.print((char) n );
            }
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

字節輸出流OutputStream

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
    public static void main(String[] args) {
        String name = "./src/main/resources/test/score1.txt";
        FileOutputStream fos;
        FileInputStream fis;
        try {
            fos = new FileOutputStream(name, true);
            fis = new FileInputStream(name);
            fos.write(50);
            fos.write('a');
            System.out.println(fis.read());
            System.out.println((char) fis.read());
            fos.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.文件復制

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFileDemo {
    public static void main(String[] args) {
        String path = "./src/main/resources/test/";
        String name = "111.jpeg";
        try {
            FileInputStream fis = new FileInputStream(path + name);
            FileOutputStream fos = new FileOutputStream(path + "copy" + name);
            int n = 0;
            byte[] b = new byte[1024];
            while ((n = fis.read(b)) != -1) {
                fos.write(b, 0, n);
            }
            fis.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4.文件操作緩沖區Buffered

import java.io.*;
/**
 *  緩沖輸入流 BufferedInputStream
 *  緩沖輸出流 BufferedOutputStream
 */
public class BufferedDemo {
    public static void main(String[] args) {
        String name = "./src/main/resources/test/score1.txt";
        try {
            FileOutputStream fos = new FileOutputStream(name);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            FileInputStream fis = new FileInputStream(name);
            BufferedInputStream bis = new BufferedInputStream(fis);
            long startTime = System.currentTimeMillis();
            bos.write(50);
            bos.write('a');
            bos.flush(); // 緩沖區未寫滿,調用該方法強制清空緩沖區并寫入文件
            System.out.println(bis.read());
            System.out.println((char) bis.read());
            long endTime = System.currentTimeMillis();
            System.out.println(endTime - startTime);
            fos.close();
            bos.close();  // 也會強制清空緩沖區并寫入文件
            fis.close();
            bis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5.字符流

字符輸入流 Reader

字符輸出流 Writer

6.字節字符轉換流

InputStreamReader

OutputStreamWriter

import java.io.*;
public class ReaderDemo {
    public static void main(String[] args) {
        String name = "./src/main/resources/test/score.txt";
        String name1 = "./src/main/resources/test/score1.txt";
        try {
            FileInputStream fis = new FileInputStream(name);
            InputStreamReader isr = new InputStreamReader(fis, "GBK");
            BufferedReader br = new BufferedReader(isr);
            FileOutputStream fos = new FileOutputStream(name1);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
            BufferedWriter bw = new BufferedWriter(osw);
            int n = 0;
            char[] cbuf = new char[10];
           /* while ((n = isr.read()) != -1) {
                System.out.print((char) n);
            }*/
            /*while ((n = isr.read(cbuf)) != -1) {
                String s = new String(cbuf, 0, n);
                System.out.print(s);
            }*/
            while ((n = br.read(cbuf)) != -1) {
                // String s = new String(cbuf, 0, n);
                // osw.write(s);
                bw.write(cbuf, 0, n);
                bw.flush();
            }
            fis.close();
            fos.close();
            isr.close();
            osw.close();
            br.close();
            bw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

7.對象序列化

步驟:

(1)創建一個類,繼承Serializable接口;

(2)創建對象;

(3)將對象寫入文件;

(4)從文件讀取對象信息

對象輸入流ObjectInputStream

對象輸出流ObjectOutputStream

import java.io.*;
public class GoodsTest {
    public static void main(String[] args) {
        String name = "./src/main/resources/test/score.txt";
        Goods goods1 = new Goods("gd001", "電腦", 3000);
        try {
            FileOutputStream fos = new FileOutputStream(name);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            FileInputStream fis = new FileInputStream(name);
            ObjectInputStream ois = new ObjectInputStream(fis);
            // 將Goods對象寫入文件
            oos.writeObject(goods1);
            oos.writeBoolean(true);
            oos.flush();
            // 讀取對象
            Goods goods = (Goods) ois.readObject();
            System.out.println(goods);
            System.out.println(ois.readBoolean());
            fos.close();
            oos.close();
            fis.close();
            ois.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
import java.io.Serializable;
public class Goods implements Serializable {
    private String goodsId;
    private String goodsName;
    private double 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;
    }
    @Override
    public String toString() {
        return "Goods{" +
                "goodsId='" + goodsId + '\'' +
                ", goodsName='" + goodsName + '\'' +
                ", price=" + price +
                '}';
    }
}

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 99久久综合狠狠综合久久 | 国产精品99久久久久久www | 四虎国产精品免费久久影院 | 99热最新网址 | 国产成人h片视频在线观看 国产成人h综合亚洲欧美在线 | 日日草天天干 | 国产欧美国产精品第二区 | 91精品国产爱久久久久 | aⅴ一区二区三区 | 青青草一区二区免费精品 | 手机看片国产精品 | 黄页成人免费网站 | 精品国产欧美一区二区最新 | 狠狠色噜噜狠狠狠97影音先锋 | a级做人爱免费播放 | 图片亚洲va欧美va国产综合 | 国产成人亚洲毛片 | 亚洲一区二区三区免费观看 | 99爱视频精品免视看 | 韩日一区二区三区 | 四虎影院国产 | 中文字幕日韩精品在线 | 欧美人成一本免费观看视频 | 精品久久久久久亚洲 | 超乳w真性中出し冲田杏梨101 | 亚洲一区二区三区精品国产 | 色偷偷亚洲女人天堂观看欧 | 国产精品中文字幕在线观看 | 精品四虎免费观看国产高清 | 国内免费在线视频 | 国产欧美国产精品第二区 | 一区二区中文字幕亚洲精品 | 欧美成人性色生活18黑人 | 国产午夜爽爽窝窝在线观看 | 国产91av视频 | 国产一区欧美二区 | 久久福利青草精品资源站免费 | 久久.com| 婷婷热 | 天天操天天干天天拍 | 久久香蕉国产精品一区二区三 |