当前位置: 移动技术网 > IT编程>开发语言>Java > J03-Java IO流总结三 《 FileInoutStream和FileOutputStream 》

J03-Java IO流总结三 《 FileInoutStream和FileOutputStream 》

2018年09月15日  | 移动技术网IT编程  | 我要评论

1. fileinputstream

   fileinputstream是一个文件输入节点流,它是一个字节流,它的作用是将磁盘文件的内容读取到内存中。

  fileinputstream的父类是inputstream。

  该类的源码不用细看,因为它是节点流,已经是相对底层的了,读源码根本读不出来它是怎么实现的。

  下面是该类的两种简单用法,分别是使用read()read(byte[] buf)方法来读取流数据。

import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

public class fileinputstreamtest {
    public static void main(string[] args) {
        system.out.println("一个一个字节地读取的效果:");
        test1();
        
        system.out.println("\n通过字节数组读取的效果:");
        test2();
    }
    
    
//////////////////////////////////////////////////////////////////////
    /**
     * 使用read()方法,一个字节一个字节地读取
     */
    private static void test1() {
        fileinputstream fis = null;
        
        try {
            fis = new fileinputstream("./src/res/1.txt");
            int value = 0;
            
            while(-1 != (value = fis.read())) {
                system.out.print((char)value);//转换为字符并打印出来
            }
            
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            if(null != fis) {
                try {
                    fis.close();
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
        }
    }
    
    
//////////////////////////////////////////////////////////////////////
    /**
     * 使用read(byte b[])方法,一次最多读取b.length个字节到b字节数组中
     */
    private static void test2() {
        fileinputstream fis= null;
        
        try {
            fis = new fileinputstream("./src/res/1.txt");
            
            int len = 0;
            byte[] buf = new byte[1024];
            
            while(-1 != (len = fis.read(buf))) {
                system.out.println(new string(buf, 0, len));
            }
            
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            if(null != fis) {
                try {
                    fis.close();
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
        }
    }
}

  代码运行效果:

一个一个字节地读取的效果:
hello java hello hello ???ú
通过字节数组读取的效果:
hello java hello hello 中国

    由上面的运行效果可以看到,在文件存在中文时,若使用read()方法一个字节一个字节地读取,并且每取到一个字节就打印出来,这个时候就会出现乱码,这是因为中文一般都不止占用一个字节(gbk编码时占用2个字节,utf-8编码时占用3个字节),当取到一个字节时,有可能该字节只是一个中文的一半,将中文截断了,这时打印出来肯定就是乱码的了。而第2中方法先将数据读取到字节数组,再用string的string(byte bytes[], int offset, int length)构造方法还原成字符串,则可以一定程度上避免了中文被截断的隐患,所以该方法可以正确的读取到文件内容,并且不会有乱码。

   在上面的示例中,文件1.txt使用的是gbk编码,而使用方法2中的string(byte bytes[], int offset, int length)方法使用的也是平台的默认字符集gbk来进行解码的,因此可以正确地读取出文件内容,不会有乱码。倘若将1.txt的编码修改为utf-8编码,此时还用方法2去读取,则会出现如下所示的乱码情况:

通过字节数组读取的效果:
hello java hello hello 涓浗

  这种情况也很好理解,源文件1.txt是使用gbk编码的,我们使用utf-8去解码,得到的结果自然是乱码了。这时如果还想将源文件中的内容正确打印到控制台,则需将方法2修改下面所示的方法3:

private static void test3() {
        fileinputstream fis= null;
        
        try {
            fis = new fileinputstream("./src/res/1.txt");
            
            int len = 0;
            byte[] buf = new byte[1024];
            
            while(-1 != (len = fis.read(buf))) {
                system.out.println(new string(buf, 0, len, "utf-8"));
            }
            
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            if(null != fis) {
                try {
                    fis.close();
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
        }
    }

   上面代码中,我们通过string的string(byte bytes[], int offset, int length, string charsetname)构造方法,以显式指定字符集的方式来解码字节数组。如此,同样可以正确读出文件中的内容。从这里可以知道,还原字符串的时候,必须保证编码的统一!!!

 

 

2. fileoutputstream

  fileoutputstream是文件输出节点流,同样它也是个字节流。根据api文档可知,fileoutputstream文件输出流是用于将数据写入 file 或 filedescriptor 的输出流。

  首先该类的几个构造方法需要注意一下:

fileoutputstream(string name) throws filenotfoundexception

   该方法创建一个向具有指定名称的文件中写入数据的输出文件流,事实上该方法的底层会调用重载的构造方法来创建指向文件的输出流。由源码可以看出它的实现思路:

public fileoutputstream(string name) throws filenotfoundexception {
        this(name != null ? new file(name) : null, false);
    }

   另外,需要注意的是,使用该方法指向的文件,若文件已存在,则通过该输出流写入时会覆盖文件中的原有内容;若文件不存在,则会先创建文件,再向文件中写入数据。即便如此,该方法还是有可能会抛出filenotfoundexception,这是因为,假如你传入的是一个完全存在的文件路径(如:suhaha/xxx…/2.txt),那么jvm根本无法在一个不存在的路径上创建文件,这个时候就会报filenotfoundexception异常。

fileoutputstream(string name, boolean append) throws filenotfoundexception

   该方法的功能跟上面的差不多一样,只不过它可以通过第二个参数append,来决定是追加还是覆盖目标文件中的内容,若传入true,则是追加,若传入false,则是覆盖。

示例代码:

import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;

public class fileoutputstreamtest {
    public static void main(string[] args) {
        fileoutputstream fos = null;
        
        try {
            fos = new fileoutputstream("./src/res/2.txt");  //路径正确时,若文件不存在,则自动创建
//          fos = new fileoutputstream("suhaha/xxx");   //如果是随便乱写一个压根不存在的路径,则会报filenotfoundexception异常
            string str1 = "hello java";
            string str2 = "中国";
            
            //将字符串转换为字节数组
            byte[] bytes1 = str1.getbytes();
            byte[] bytes2 = str2.getbytes();
            
            fos.write(bytes1[0]);   //将单个字节写入输出流中,写入:h
            fos.write(bytes2);      //将整个字节数组写入,写入:中国
            fos.write(bytes1, 6, 4);//将字节数组bytes1中从索引6开始的4个字节写入输出流中,写入:java
            
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            if(null != fos) {
                try {
                    fos.close();
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
        }
    }
}

 代码运行效果:

  同样,在通过fileoutputstream将数据写入目标文件时,也有可能存在由编码引起的乱码问题。

  在上面的示例中,在使用str1.getbytes()方法将字符串转换为字节数组时,由getbytes()方法的源码可以看出,它使用的是平台默认的字符集进行编码,我在windows平台上是gbk。然后,最后通过输出流写入的文件也是使用gbk进行编码的,因此最后写入的数据没有出现乱码,倘若目标文件2.txt是utf-8编码,则使用上面的代码进行写入就会出现如下所示的乱码:

 

    此时可以通过手动将2.txt文件的编码由utf-8改为gbk,来将数据正确显式出来。

  然而,如果我们的需求就是需要将数据写入一个用utf-8编码的目标文件中,则可用通过使用string类的byte[] getbytes(string charsetname)方法显式指定字符集来将字符串转换为字节数组,这样就可以将数据正确写入一个utf-8目标文件中。示例代码如下所示:

import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;

public class fileoutputstreamtest {
    public static void main(string[] args) {
        fileoutputstream fos = null;
        
        try {
            fos = new fileoutputstream("./src/res/2.txt");  //路径正确时,若文件不存在,则自动创建
//          fos = new fileoutputstream("suhaha/xxx");   //如果是随便乱写一个压根不存在的路径,则会报filenotfoundexception异常
            string str1 = "hello java";
            string str2 = "中国";
            
            //将字符串转换为字节数组
            byte[] bytes1 = str1.getbytes();
            byte[] bytes2 = str2.getbytes("utf-8");
            
            fos.write(bytes1[0]);   //将单个字节写入输出流中,写入:h
            fos.write(bytes2);      //将整个字节数组写入,写入:中国
            fos.write(bytes1, 6, 4);//将字节数组bytes1中从索引6开始的4个字节写入输出流中,写入:java
            
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            if(null != fos) {
                try {
                    fos.close();
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
        }
    }
}

 

 

 

3. fileinputstream和fileoutputstream综合使用示例

 下面的代码中定义了两个方法,test1()和test2(),test1()方法从一个gbk编码的源文件中读取数据,复制到一个以utf-8编码的目标文件中;test2()则正好反之。

import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;

public class fileinoutstreamtest {
    public static void main(string[] args) {
        
//      test1();
        test2();
        
    }

    /////////////////////////////////////////////////////
    /**
     * 从1.txt文件中读取数据,复制到2.txt文件中
     * 其中,1.txt是用gbk编码的,2.txt使用utf-8编码的
     */
    private static void test1() {
        fileinputstream fis = null;
        fileoutputstream fos = null;
        
        try {
            fis = new fileinputstream("./src/res/1.txt");
            fos = new fileoutputstream("./src/res/2.txt");
            
            int len = 0;
            byte[] buf = new byte[1024];
            
            while(-1 != (len = fis.read(buf))) {        //从源文件1.txt读取到字节数组中的数据是gbk编码的
                string str = new string(buf, 0, len);   //先转为字符串,这里默认使用gbk进行解码
                byte[] bytes = str.getbytes("utf-8");   //再显式指定以utf-8字符集进行编码
                fos.write(bytes);                       //将字节数组数据写入到以utf-8编码的目标文件2.txt中
                fos.flush();
            }
            
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            if(null != fos) {
                try {
                    fos.close();
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
            if(null != fis) {
                try {
                    fis.close();
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
        }
    }
    
    
    /////////////////////////////////////////////////////
    /**
     * 从2.txt文件中读取数据,复制到1.txt文件中
     * 其中,1.txt是用gbk编码的,2.txt使用utf-8编码的
     */
    private static void test2() {
        fileinputstream fis = null;
        fileoutputstream fos = null;
        
        try {
            fis = new fileinputstream("./src/res/2.txt");
            fos = new fileoutputstream("./src/res/1.txt");
            
            int len = 0;
            byte[] buf = new byte[1024];
            
            while(-1 != (len = fis.read(buf))) {        //从源文件2.txt读取到字节数组中的数据是utf-8编码的
                string str = new string(buf, 0, len, "utf-8");  //先转为字符串,这里显式指定使用utf-8进行解码
                byte[] bytes = str.getbytes();          //再以默认字符集gbk进行编码
                fos.write(bytes);                       //将字节数组数据写入到以gbk编码的目标文件1.txt中
                fos.flush();
            }
            
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            if(null != fos) {
                try {
                    fos.close();
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
            if(null != fis) {
                try {
                    fis.close();
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
        }
    }
}

  :上面演示的方法可能比较low,但是如果只使用目前的这两个文件输入输出流的话,这是我能想到的一个解决办法。

  后面介绍到转换流inputstreamreader和outputstreamwriter的时候,再针对该需求写一遍代码。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网