ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

文件输入/输出流

2022-04-07 19:01:35  阅读:125  来源: 互联网

标签:输出 文件 try printStackTrace FileOutputStream catch new 输入


《零基础学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();
                    }
                }
            }
        }
    }
    

标签:输出,文件,try,printStackTrace,FileOutputStream,catch,new,输入
来源: https://www.cnblogs.com/Auci/p/16113749.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有