FileInputStream读取文件&FileOutputStream写入文件

FileInputStream读取文件&FileOutputStream写入文件

概念摘自:http://jingyan.baidu.com/article/5552ef473ab5f2518ffbc98e.html

 Java的流式输入输出建立在4个抽象类的基础上:InputStream,OutputStream,Reader和Writer。它们用来创建具体的流式子类。InputStream和OutputStream类被设计为字节类,而Reader和Writer被设计为字符流类。

 一般,处理字符和字符串用字符流类,字节和二进制文件用字节类。本篇只讲字节流类;

 字节流中的两个顶层类为:InputStream(输入字节流)和OutputStream(输出字节流). 其下有两个子类FileInputStream(文件输入流)和FileOutputStream(文件输出流)。

 FileInputStream读取的两种方法:逐字节读;以字节数组读取两种方式;

复制代码

public static void main(String[] args) {
        //创建文件对象,指定要读取的文件路径(要读的文件一定要存在)
        File file=new File("E:\\a.text");
        
        try {
            //创建文件输入流对象.指定要读取的文件对象
            FileInputStream fin=new FileInputStream(file);
            
            /***********方法一(将输入流的数据传递给字节数组)*********/
            //创建字节数组,准备将文件流中的数据传给字节数组
            /*byte[] b=new byte[fin.available()];
            
            //将字节流中的数据传递给字节数组
            fin.read(b);
            
            //将字节数组转为字符串
            String s=new String(b);
            
            System.out.println(s);*/
            /*********************************************/
            
            /********方法二(逐字节读取数据从字节输入流)***********/
            int l;
            while ((l=fin.read())!=-1) {
                System.out.println((char)l);
                //测试read()方法的含义,什么是逐字节读,及int类型的l代表什么意思,测试结果l代表存储的内容的int的表现形式,与进制相关,不做深究
                //System.out.println((char)l+"\t"+l);
            }
            
            fin.close();
            /************************************************/
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

复制代码

FileOutputStream写入文件的两种方式:以字节数组写入;逐字节写入;

复制代码

public static void main(String[] args) {
        // TODO Auto-generated method stub
        //创建文件对象,指定要写出的文件路径
        File file=new File("d:\\d.text");
        
        try {
            //创建文件字节输出流对象,准备向d.txt文件中写出数据,true表示在原有的基础上增加内容
            FileOutputStream fout=new FileOutputStream(file,true);
            Scanner sc=new Scanner(System.in);
            
            System.out.println("请写出一段字符串:");
            String msg=sc.next()+"\r\n";;
            
            /******************(方法一)按字节数组写入**********************/
            //byte[] bytes = msg.getBytes();//msg.getBytes()将字符串转为字节数组
            
            //fout.write(bytes);//使用字节数组输出到文件
            /******************(方法一)逐字节写入**********************/
            byte[] bytes = msg.getBytes();
            for (int i = 0; i < bytes.length; i++) {
                fout.write(bytes[i]);//逐字节写文件
            }
            fout.flush();//强制刷新输出流
            fout.close();//关闭输出流
            System.out.println("写入完成!");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

复制代码

字节流转化为字符流

复制代码

            //创建字节输出流对象
            FileOutputStream fout=new FileOutputStream(new File("student.xml"));
            //创建字节流缓冲区,加快写出速度
            BufferedOutputStream bout=new BufferedOutputStream(fout);
            
            //创建字符输出流对象
            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(bout, "utf-8"));
            
            doc.write(bw);
            
            bw.flush();
            bw.close();

复制代码

为什么将字节流转化为字符流?

因为字节流是二进制读写的,在外部打开是乱码,转化为字符串或字符可以让文件在外部也可以看到内容!根据具体的需求确定用哪个!

标签: 

猜你喜欢

转载自blog.csdn.net/qq_26171035/article/details/82188526