黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注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) 瀏覽1380次

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)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 看一级毛片女人洗澡 | 成人在线综合 | 日韩αv | 99成人精品 | 黄色网点 | 91在线 在线播放 | 成人羞羞视频在线观看 | 91精选视频| 免费精品精品国产欧美在线 | 久久精品伦理 | 毛片免费在线播放 | 国产无套在线观看视频 | 中文字幕看片在线a免费 | 亚洲成a人片在线观看导航 亚洲成a人片在线观看精品 | 亚洲伊人久久综合一区二区 | 国产精品久久久久久久久免费观看 | 国产在线视频www色 国产在线视频国产永久视频 | 最近的中文字幕在线看 | 成人午夜爽爽爽免费视频 | 亚洲视频在线观看视频 | 我看一级黄色片 | 亚洲影视久久 | 婷婷色综合成人成人网小说 | www深夜视频在线观看高清 | 国产精品一区二区久久不卡 | 粉色污视频 | 免费污污视频在线观看 | 亚洲欧美日韩在线一区二区三区 | 欧美日韩国产超高清免费看片 | a一级免费视频 | 中国男女全黄大片一级 | 国产成人久久综合热 | 日韩精品高清自在线 | 亚欧成人乱码一区二区 | 亚洲精品成人a | 中文字幕日本一区久久 | 免费无遮h在线网站大全 | 色www免费视频 | 免费超爽大片黄 | 精品视频在线视频 | 日老逼视频 |