大战熟女丰满人妻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国产精品久久久久久久成人热 | 亚洲视频免 | 外国一级黄色毛片 | 免费视频国产 | 久久精品国产只有精品6 | 中文字幕一区二区三区四区五区人 | 一区二区国产在线观看 | 99热这里只有精品国产99 | 日韩欧美精品一区二区三区 | 亚洲婷婷综合色高清在线 | 丝袜亚洲精品中文字幕一区 | 国产亚洲综合成人91精品 | 亚洲欧洲一区二区 | 一级网站在线观看 | 欧美 日产 国产精品 | 麻豆伦理 | 欧美成人观看视频在线 | 国产乳摇福利视频在线观看 | 久久久久久九九 | 91尤物在线 | 日本精品一区二区三区在线 | 国产成人无精品久久久久国语 | 国产护士一级毛片高清 | 麻豆国内精品欧美在线 | 久久99精品久久久久久久不卡 | 国产精品久久久久久吹潮 | 国产99视频精品免视看9 | 久久一日本道色综合久久m 久久一色本道亚洲 | 久久精品国产亚洲麻豆小说 | 91精品啪在线观看国产老湿机 | 久久黄色影片 | 97在线资源| 麻豆成人在线 | 欧美精品久久久亚洲 | 欧美成人毛片在线视频 | 色综合欧美综合天天综合 | 四虎影院视频 | 人人鲁免费播放视频人人香蕉 |