文件输入/输出流

博客 分享
0 257
张三
张三 2022-04-07 18:57:41
悬赏:0 积分 收藏

文件输入/输出流

《零基础学Java》


  • 文件输入/输出流

    程序运行期间,大部分数据都被存储在内存中,当程序结束或被关闭时,存储在内存中的数据将会消失。如果要永久保存数据,那么最好的办法就是把数据保存到磁盘的文件中。为此,Java提供了文件输入/输出流,即 FilelnputStream类 与 FilcOutputSream类 和 FilcRcader类 与 FileWriter类 。

  • FilelnputStream类 与 FilcOutputSream类

    Java提供了操作磁盘文件的 FilelnpuSueam类 与 FileOutputStream类 (读取文件内容使用的是FilenputStram类;向文作中写入内容使用的是FileOutputStream类)。FilelnputStream类 与 FilcOutputSream类 操作的数据单元是一个字节,如果文件中有中文字符则占两个字节。

    FilelnpuSueam类常用的构造方法:

    构造方法介绍
    FilelnpuSueam(String name);使用指定的文件名****(name)创建一个FilelnpuSueam对象;
    FilelnpuSueam(File file);使用File对象创建FilelnpuSueam对象。(PS:该方法允许把文件链接输入流)

    FileOutputStream类常用的构造方法:

    构造方法介绍
    FileOutputStream(File file);使用File对象创建FileOutputStream对象,为文件写入数据的文件输出流。
    FileOutputStream(File file , boolean whether);使用File对象创建FileOutputStream对象,为文件写入数据的文件输出流,当 whether 为 true 时,字节写入文件末尾处,而不是覆盖。
    FileOutputStream(String name);使用指定的文件名(name)创建一个FilelnpuSueam对象,为文件写入数据的文件输出流。
    FileOutputStream(String name , boolean whether);使用指定的文件名(name)创建一个FilelnpuSueam对象,为文件写入数据的文件输出流,当 whether 为 true 时,字节写入文件末尾处,而不是覆盖。

    FilelnputStream类与FilcOutputSream类 实例:

    mport java.io.*;public class Demo2 {    public static void main(String[] args) {        File file = new File("C:\\12.4.1\\Word.txt");        /**         * 输出流 (FileOutputStream)         */        FileOutputStream outputStream =null;        try {            outputStream=new FileOutputStream(file,true);//输出流 读文件。true:在文件末尾追加内容; fales:替换文件内容;            String str = "hello word";            byte by[] = str.getBytes();//字符串转换为字节数组            try {                outputStream.write(by);//将字节数组中的数据写入到文件当中            } catch (IOException e) {                e.printStackTrace();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        }finally {            if (outputStream!=null){                try {                    outputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        /**         * 输入流 (FileInputStream)         */        FileInputStream fileInputStream = null;        try {            fileInputStream = new FileInputStream(file);//输入流 读文件            byte by1[] = new byte[128];//创建缓冲区            try {                int len = fileInputStream.read(by1);//读入缓冲区的总字节数 = 输入流添加到缓冲区                System.out.println("文件中的数据为:"+new String(by1,0,len));//去空格            } catch (IOException e) {                e.printStackTrace();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        }finally {            if (fileInputStream!=null){                try {                    fileInputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}
  • FileReader类 与 FileWriter类

    FileReader类 与 FileWriter类 对应了 FilelnputStream类 与 FilcOutputSream类;其中读取文件内容使用的是FileReader类,向文件写入内容使用的是FileWriter类;FileReader类 与 FileWriter类操作的数据单元是一个字节,如果文件中有中文字符,就可以使用FileReader类 与 FileWriter类读取文件、写入文件就可以避免乱码的产生。

    FileReader类 与 FileWriter类 实例:

    import java.io.*;public class Demo3 {    public static void main(String[] args) {        File file = new File("C:\\Word.txt");        /**         *  字符输出流(FileWriter)         */        FileWriter fileWriter = null;        try {            fileWriter=new FileWriter(file,true);// true:在源文件后追加新内容; fales:覆盖源文件;            String str = "神里绫华,参上!";            fileWriter.write(str);//将字符串写入到文本文档        } catch (IOException e) {            e.printStackTrace();        }finally {            if (fileWriter != null){                try {                    fileWriter.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        /**         * 字符输入流(FileReader)         */        FileReader fileReader = null;        try {            fileReader=new FileReader(file);            char ch[] = new char[1024];//缓冲区            int i;//已读出的字符数            while ((i=fileReader.read(ch))!=-1){//循环读取文件中的数据,直到所有字符都读完。                System.out.println("文件中的内容为"+new String(ch,0,i));            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            if (fileReader != null){                try {                    fileReader.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}
posted @ 2022-04-07 18:52 Auci 阅读(0) 评论(0) 编辑 收藏 举报
回帖
    张三

    张三 (王者 段位)

    821 积分 (2)粉丝 (41)源码

     

    温馨提示

    亦奇源码

    最新会员