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

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) 學(xué)習(xí)攻略 Java學(xué)習(xí) Java文件處理詳解

Java文件處理詳解

更新時(shí)間:2022-12-29 11:06:23 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1394次

Java文件處理是什么?動(dòng)力節(jié)點(diǎn)小編來(lái)為大家進(jìn)行詳細(xì)介紹。

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.字節(jié)流

字節(jié)輸入流InputStream

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
 * 從文件系統(tǒng)中的某個(gè)文件中獲得輸入字節(jié)
 * 用于讀取諸如圖像數(shù)據(jù)之類的原始字節(jié)流
 */
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();
        }
    }
}

字節(jié)輸出流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.文件復(fù)制

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.文件操作緩沖區(qū)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(); // 緩沖區(qū)未寫滿,調(diào)用該方法強(qiáng)制清空緩沖區(qū)并寫入文件
            System.out.println(bis.read());
            System.out.println((char) bis.read());
            long endTime = System.currentTimeMillis();
            System.out.println(endTime - startTime);
            fos.close();
            bos.close();  // 也會(huì)強(qiáng)制清空緩沖區(qū)并寫入文件
            fis.close();
            bis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5.字符流

字符輸入流 Reader

字符輸出流 Writer

6.字節(jié)字符轉(zhuǎn)換流

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.對(duì)象序列化

步驟:

(1)創(chuàng)建一個(gè)類,繼承Serializable接口;

(2)創(chuàng)建對(duì)象;

(3)將對(duì)象寫入文件;

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

對(duì)象輸入流ObjectInputStream

對(duì)象輸出流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對(duì)象寫入文件
            oos.writeObject(goods1);
            oos.writeBoolean(true);
            oos.flush();
            // 讀取對(duì)象
            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 +
                '}';
    }
}

 

提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 亚洲国产成人最新精品资源 | 一级做a爱片特黄在线观看 一级做a爱片特黄在线观看免费看 | 日本操操操 | 亚洲人成影院在线高清 | 五月婷婷激情综合网 | 一级毛片免费播放 | 四虎精品免费永久在线 | 天天射天天操天天 | 国产 欧美 日产中文 | 久久精品无码一区二区日韩av | 亚洲成在人线久久综合 | 深夜福利视频在线一区 | 国产成a人片在线观看视频99 | 一区二区三区视频网站 | 免费在线激情视频 | 亚洲丶国产丶欧美一区二区三区 | 99国产精品视频免费观看 | 男人影院免费 | 中文字幕日韩欧美 | caoporm超免费公开视频 | m3u8久久国产精品影院 | 亚洲精品一区二 | 免费亚洲网站 | 国产成人综合亚洲动漫在线 | 国产91精品高清一区二区三区 | 亚洲天堂一区二区 | 我要看免费一级毛片 | 大ji吧快给我别停受不了视频 | 99精品影视| 男人天堂欧美 | 久久精品视频热 | 国产综合亚洲精品一区 | 日本精品久久久中文字幕 | 国产高清在线视频一区二区三区 | 91精品视频免费观看 | 97在线视频免费 | 国产精品91av | 99精品视频在线观看免费播放 | 玖热在线| 亚洲免费在线观看视频 | 国产小视频在线观看www |