javaIO流详解 | 您所在的位置:网站首页 › java从文件读入数据的方法是什么 › javaIO流详解 |
目录 IO流简介: 1 :InputStream 2:OutputStream 3:FileInputStream 4:FileOutputStream 5:利用FileInputStream和FileOutputStream读写文件 6: BufferedInputStream 6.1:常用方法: 7: BufferedOutputStream 8:BufferedInputStream和BufferedOutputStream读取文件的案例 9: DataInputStream 10: DataOutputStream 11: ObjectInputStream :对象输入流 12: ObjectOutputStream :对象输出流 字符流: 13: InputStreamReader 14: OutputStreamWriter 15:使用InputStreamReader 和 OUtputStreamWriter的案例 16: FileReader 17: FileWriter 18: BufferedReader(缓冲流) 19: BufferedWriter (缓冲流) 20:ByteArrayInputStream 21:ByteArrayOutputStream IO流图解: 流的定义:流是指一连串流动的字符,是以先进先出方式发送信息的通道。 按流向分:输出流:OutputStream和Writer为基类 输入流:InputStream和Reader为基类 按处理数据单元划分:字节流:字节输入流:InputStream基类 字节输出流:OutputStream基类 字符流:字符输入流:Reader基类 字节输出流:Writer基类 (字节流是 8 位通用字节流,字符流是16位Unicode字符流) 1 :InputStream InputStream ins ins =new FileInputStream(new File(path));操控字节输入流的,所用的方法也是针对字节的。 1.1:常用方法: 返回值类型 方法名 方法简介 abstract intread( )从此输入流中读取下一个字节(此方法是抽象方法,子类必须实现该方法。intread(byte [ ] b )从输入流中读取一定数量的字节,存储在参数指定的字节数组中。intread(byte [ ] b ,int off ,int len )读到 len字节从输入流读入字节数组数据。longskip( long n )跳过并丢弃 n字节从输入流中的数据。intavailable( )返回此输入流下一个方法可以读取的字节数。voidclose( )关闭流。tips: 关于InputStream.read(byte[] b)和InputStream.read(byte[] b,int off,int len)这两个方法都是用来从流里读取多个字节的,有经验的程序员就会发现,这两个方法经常 读取不到自己想要读取的个数的字节。比如第一个方法,程序员往往希望程序能读取到b.length个字节,而实际情况是,系统往往读取不了这么多。仔细阅读Java的API说明就发现了,这个方法 并不保证能读取这么多个字节,它只能保证最多读取这么多个字节(最少1个)。因此,如果要让程序读取count个字节,最好用以下代码: byte[] b = new byte[count]; int readCount = 0; // 已经成功读取的字节的个数 while (readCount < count) { readCount += in.read(bytes, readCount, count - readCount); }用这段代码可以保证读取count个字节,除非中途遇到IO异常或者到了数据流的结尾(EOFException) 2:OutputStream常用方法: 返回值类型 方法名 方法简介 voidflush( )刷新输出流,是缓存数据被写出来voidwrite(byte [ ] b)写b.length 字节从指定的字节数组的输出流voidwrite(byte [ ] b, int off, int len)写len字节从字节数组数据中到输入流abstract intwrite( int b )将指定的字节写入该输出流中voidclose( )关闭流 3:FileInputStreamFileInputStream是读取原始字节的图像数据流。读取字符流,考虑使用FileReader。常用方法见InputStream中的方法 构建输入流:(准备工作) FIle file =new File(path); InputStream ins =null; ins =new FileInputStream(file); //这里需要try catch3.1:读取文件字符: int i =0; while( ( i=ins.read( ) ) != -1){ System.out.println( i ); } //输出的是自动转化的ASCII值,此处会抛出 IOException,需要try catch //或者提高效率,使用缓冲数组byte[ ] b,可读取英文文件,需要转化成char字符: int len =0; byte [ ]b=new byte [ 1024 ];//建立缓冲流数组 while((len=ins.read( b ))!= -1){ System.out.println(""+ new String(b,0,len)); } } //中间嵌套for循环,读多少输出多少,中文不可用,会输出乱码-->此处会抛出 IOException,需要try //catch 4:FileOutputStream是OutputSTream的子类,主要功能是从源文件写入资料至指定文件中 4.1:构造方法: 1、FileOutputStream( File file )// 创建“File对象”对应的“文件输入流”;默认“追加模式”是false, 即“写到输出的流内容”不是以追加的方式添加到文件中。若要追加则为(File file, true); 2、FileOutputStream( String path ) // 创建“文件(路径为path)”对应的“文件输入流”; (若没有,自动系统调用方法创建) 默认“追加模式”是false,即“写到输出的流内容”不是以追加的方式添加到文件中。 若要追加则为(String path, true); public class Test1 { public static void main(String[] args) { try { // 构造方法1 File file = new File("E:/test.txt"); FileOutputStream fos1 = new FileOutputStream(file); // 没有该文件会直接创建文件,但不能有多级目录,否则会报错,或者这只是个目录 // -->java.io.FileNotFoundException:E:\java\java48.txt (系统找不到指定的路径。) // 构造方法2 FileOutputStream fos2 = new FileOutputStream("E:/java/test.txt");// 规则如上 } catch (Exception e) { e.printStackTrace(); } } }4.2:常用方法:(同OutputStream) 返回值类型 方法名 方法简介 voidflush( )刷新输出流,是缓存数据被写出来voidwrite(byte [ ] b)写b.length 字节从指定的字节数组的输出流voidwrite(byte [ ] b, int off, int len)写len字节从字节数组数据中到输入流abstract intwrite( int b )将指定的字节写入该输出流中voidclose( )关闭流应用主要步骤:1、构建字节输出流对象 2、写数据 3、关闭流 5:利用FileInputStream和FileOutputStream读写文件 public class Test2 { public static void main(String[] args){ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { File file = new File("C:\\Users\\86156\\Desktop\\test.txt"); fileInputStream = new FileInputStream(file); fileOutputStream = new FileOutputStream(new File("C:\\Users\\86156\\Desktop\\testhello.txt")); byte[] b =new byte[20]; int i=0; while ((i=fileInputStream.read(b))!=(-1)){ System.out.println(""+i); System.out.println(""+ new String(b)); fileOutputStream.write(b,0,i); fileOutputStream.flush(); } } catch (IOException e) { e.printStackTrace(); }finally { try { fileInputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } 6: BufferedInputStream带缓冲区的输入流,默认缓冲区大小是8M,能够减少访问磁盘的次数,提高文件读取性能;是 FileInputStream 的子类。 实现了装饰设计模式!BufferedInputStream没有无参构造方法,它必须传入一个InputStream(一般是FileInputStream),来一起使用,以提高读写效率。 构造方法:1、BufferInputStream(InputStream in)// 创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。创建一个内部缓冲区数组并将其存储在 buf 中,该buf的大小默认为8192。 2、BufferedInputStream(InputStream in, int size) //创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。创建一个长度为 size 的内部缓冲区数组并将其存储在 buf 中。 6.1:常用方法: 返回值类型方法名 方法简介 abstract intread( )从此输入流中读取下一个字节(此方法是抽象方法,子类必须实现该方法。intread(byte [ ] b )从输入流中读取一定数量的字节,存储在参数指定的字节数组中。intread(byte [ ] b ,int off ,int len )读到 len字节从输入流读入字节数组数据。 7: BufferedOutputStream带缓冲区的输出流,能够提高文件的写入效率。实现了装饰设计模式! BufferedOutputStream没有无参构造方法,它必须传入一个OutputStream(一般是FileOutputStream),来一起使用,以提高读写效率。 构造方法: 1、BufferOutputStream(OutputStream outs)// 创建一个 BufferedInputStream 并保存其参数,即输出流outs,将数据写入指定的基本输入流中。 2、BufferedOutputStream(OutputStream outs, int size) //创建具有指定缓冲区大小的 BufferedOutputStream ,即输出流outs,将数据写入指定的基本输入流中 。
7.1:常用方法: 返回值类型 方法名 方法简介 voidflush( )刷新输出流,是缓存数据被写出来voidwrite(byte [ ] b)写b.length 字节从指定的字节数组的输出流voidwrite(byte [ ] b, int off, int len)写len字节从字节数组数据中到输入流tips:为什么要用BufferedInputStream 和 BufferedOutputStream? 优点:效率高 缺点:内存占用多 不带缓冲的操作,每读一个字节就要写入一个字节,由于涉及磁盘的IO操作相比内存的操作要慢很多,所以不带缓冲的流效率很低。 带缓冲的流,可以一次读很多字节,但不向磁盘中写入,只是先放到内存里。等凑够了缓冲区大小的时候一次性写入磁盘,这种方式可以减少磁盘操作次数,速度就会提高很多。 8:BufferedInputStream和BufferedOutputStream读取文件的案例 public class Test2 { public static void main(String[] args){ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; try { File file = new File("C:\\Users\\86156\\Desktop\\test.txt"); fileInputStream = new FileInputStream(file); fileOutputStream = new FileOutputStream(new File("C:\\Users\\86156\\Desktop\\testhello.txt")); bufferedInputStream = new BufferedInputStream(fileInputStream); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); byte[] b =new byte[1024]; int i=0; while ((i=bufferedInputStream.read(b))!=(-1)){ System.out.println(""+i); System.out.println(""+ new String(b,0,i)); bufferedOutputStream.write(b,0,i); //最后刷新一下 bufferedOutputStream.flush(); } } catch (IOException e) { e.printStackTrace(); }finally { try { fileInputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } 9: DataInputStream数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。 DataInputStream 对于 多线程访问不一定是安全的。 线程安全是可选的,它由此类方法的使用者负责。 构造方法:DataInputStream(InputStream in); 9.1:常用方法: 返回值类型 方法名方法简介 intread(byte [ ] b)读取一定数量的字节从包含输入流并存入缓冲区阵列bintread (byte [ ] b, int off, int len)读到 len 从包含输入流读入字节数组数据字节booleanreadBoolean( )读 true / falsebytereadByte( )读取并返回一个输入字节charreadChar( )读取两个字节返回一个 char 价值floatreadFloat( )四字节读取输入并返回一个 float 价值doublereadDouble( )八字节读取输入并返回一个 double 价值intreadInt( )四字节读取输入并返回一个 int 价值StringreadLine( )从输入流读取下一行文本shortreadShort( )两字节读取输入并返回一个 short 价值StringreadUTF( )读取一个字符串,一直采用 modified UTF-8 编码格式 10: DataOutputStream数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。 DataInputStream 对于多线程访问不一定是安全的。 线程安全是可选的,它由此类方法的使用者负责。 构造方法: DataOutputStream( OutputStream outs); 10.1:常用方法: 返回值类型 方法名 方法简介 voidflush( )刷新数据输出流intsize( )返回柜台 written 电流值,这个数据写入输出流的字节数为止voidwrite( int b )将指定的字节(论证b的低八位)的底层输出流voidwrite( byte [ ] b, int off, int len )写 len 字节指定字节数组中的起始偏移量 off 当前输出流voidwriteBoolean( boolean v )写一个 boolean 当前输出流作为一个字节的值voidwriteByte( int v )写了 byte 当前输出流作为一个字节的值voidwriteBytes( String s )将字符串写入到基础的输出流作为字节序列voidwriteChar( int v )写一个char当前输出流作为一个双字节的值,高字节优先voidwriteChars( String s )将字符串写入底层输出流,作为一个字符序列voidwriteFloat( float v )将浮点型参数的 int使用 floatToIntBits方法在类 Float,然后写道, int值基本为4字节输出流量,高字节优先。voidwriteDouble( double v )将双参数到一个 long使用 doubleToLongBits方法在类 Double,然后写道, long值基本的输出流的字节的数量,高字节优先。voidwriteInt( int v )写一个 int当前输出流为四个字节,高字节优先。voidwriteShort( int v )写一个 short当前输出流的两个字节,高字节优先。voidwriteUTF( String str )一个字符串写入到输出流的潜在使用在一个机器无关的方式 modified UTF-8编码。案例: public class Test4 { public static void main(String[] args) { try { FileOutputStream fileOutputStream = new FileOutputStream(new File("")); DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream); dataOutputStream.writeDouble(2.00); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 11: ObjectInputStream :对象输入流特点 : 可以从输入流中读取java对象,而不需要每次读取一个字节,需要把InputStream包装到ObjectInputStream中,就可以从中读取对象,对象必须实现序列化,对象类实现Serializable接口 例:(实现序列化) public class Pet implements Serializable { private static final long serialVersionUID = 1L; } 构造方法:1、ObjectInputStream( ) ; 2、ObjectInputStream( InputStream ins) ; 10.1:常用方法: 返回值类型 方法名 方法简介 voidclose( )关闭输入流intread( )读取一个字节的数据intread( byte [ ] b, int off, int len ) 读入一个字节数组booleanreadBoolean( )在布尔值中读取bytereadByte( )读取一个8位字节charreadChar( )读取一个16位字符doublereadDouble( )floatreadFloat( )intreadInt( )读取一个32位的intStringreadLine( ) 过时的ObjectreadObject( )读取对象输入流中的对象StringreadUTF( )读modified UTF-8的格式字符串 12: ObjectOutputStream :对象输出流特点: 能够让你把对象写入到输出流中,而不需要每次写入一个字节。你可以把OutputStream包装到ObjectOutputStream中,然后就可以把对象写入到该输出流中了 对象必须实现序列化,对象类实现Serializable接口 例:(实现序列化) public class Pet implements Serializable { private static final long serialVersionUID = 1L; } 构造方法: 1、ObjectOutputStream( ) ; 2、ObjectOutputStream( OutputStream ins) ; 10.1:常用方法: 返回值类型 方法名 方法简介 voidclose( )关闭流voidflush( )刷新流protect ObjectreplaceObject( )这种方法将允许受信任的子类对象来代替一个物体对另一个序列化期间voidwrite( byte [ ] b )写入一个字节数组voidwrite( byte [ ] b, int off, int len )写入字节数组的字节数voidwrite( int val )写一个字节voidwriteBoolean( boolean val )写一个布尔值voidwriteByte( byte val )写入一个8位字节voidwriteChar( int val )写入一个16位字符voidwriteInt( int val )写入一个32位长的intvoidwriteObject( Object obj )写入指定对象的对象voidwriteUTF( String str )原始数据写在 modified UTF-8格式字符串ObjiectInputStream 和 ObjectOutputStream 案例:(创建一个宠物类,放入一个集合中,使用者两个类进行读写操作) public class Test3 { public static void main(String[] args) { File file = new File("C:\\Users\\86156\\Desktop\\date.txt"); FileOutputStream fileOutputStream = null; ObjectOutputStream objectOutputStream = null; try { fileOutputStream = new FileOutputStream(file); objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(new Date()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { objectOutputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } 字符流:1、字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串。字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的。字符流操作的是缓冲区(当我们对文件进行读写操作时如果不调用close() 或 flush()方法时不能看到数据的变化)。 2、字符输入流 Reader 基类 字符输出流 Writer 基类 3、Reader 和 Writer都是抽象类 13: InputStreamReader特点:将字节输入流转换为字符输入流。是字节流通向字符流的桥梁,可以指定字节流转换为字符流的字符集。 构造方法:1、InputStreamReader( InputStream ins) ; 创建一个默认字符集的 InputStreamReader 2、InputStreamReader( InputStream ins ,“ encoding”) ; 创建使用指定字符集的 InputStreamReader 13.1:常用方法: 返回值类型 方法名 方法简介 voidclose( )关闭流StringgetEncoding( )返回此流使用的字符编码名称intread( )读取单个字符intread( char [ ] ch, int off, int len )将字符读入一个数组的一部分booleanready( )告诉是否该流已准备好阅读读文件内容的常用三种方法: 1、int read() //public int read() throws IOException //读取单个字符。在字符可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。
2、int read( char [ ]ch, int off, in len ) //public int read(char[] cbuf) throws IOException //将字符读入数组。在某个输入可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。 3、int read( char [ ] ch) //public int read(char[] cbuf) throws IOException //将字符读入数组的某一部分。在某个输入可用、发生 I/O 错误或者到达流的末尾前,此方法一直阻塞。 14: OutputStreamWriter 特点:将字节输出流转为字符输出流,是字符流通向字节流的桥梁,可使用指定的 encoding 要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定, 否则将接受平台默认的字符集。 构造方法:1、OutputStreamWriter(OutputStream outs) ; 创建使用默认字符编码的 OutputStreamWriter 2、 OutputStreamWriter(OutputStream outs , “ecoding”) ; 可以设置编码格式 常用方法: 返回值类型 方法名 方法简介 voidclose( )关闭流voidflush( )刷新流StringgetEcoding( )返回此流使用的字符编码格式名称voidwriter( int val )写入一个字符voidwriter( char [ ]ch, int off, int len )写入一个字符数组的一部分voidwriter( String str, int off, int len )写入字符串的一部分用OutputStreamWriter 将内存中的数据写入文件中有5种方式: 1、 void writer( int val ) //public void write(int c)throws IOException //写入单个字符。要写入的字符包含在给定整数值的 16 个低位中,16 高位被忽略。 2、 void writer( char [ ]ch ) //public void write(char[] cbuf)throws IOException 写入字符数组。
3、 void writer( char [ ]ch, int off, int len) //public abstract void write(char[] cbuf,int off,int len)throws IOException 写入字符数组的某一部分。
4、 viod writer( String str ) //public void write(String str) throws IOException 写入字符串。
5、 void writer( String str, int off ,int len ) //public void write(String str,int off, int len)throws IOException; 写入字符串的某一部分。 15:使用InputStreamReader 和 OUtputStreamWriter的案例 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.Scanner; /** * 包装流设置字符集 * * @author kuang * */ public class homework_2 { public static void main(String[] args) { // 确定源和目标文件 Scanner sc = new Scanner(System.in); File o = new File("text1.txt"); File t = new File("text2.txt"); //检测没有文件就创建文件 check(o); check(t); // 构建流对象 InputStream ins = null; OutputStream outs = null; InputStreamReader insr = null; OutputStreamWriter outsw = null; try { outs = new FileOutputStream(o);// 先做为目标文件写入内容 outsw = new OutputStreamWriter(outs, "UTF-8");//编码格式为UTF-8 System.out.println("输入你要写入的内容:"); String msg = sc.next(); // 写入内容 outsw.write(msg); // 刷新 outsw.flush(); System.out.println("已经完全写入!"); ins = new FileInputStream(o);// 作为源文件 insr = new InputStreamReader(ins, "UTF-8");//编码格式为UTF-8 outs = new FileOutputStream(t);// 作为写入的目标文件 outsw = new OutputStreamWriter(outs, "UTF-8");//编码格式为UTF-8 char[] ch = new char[1024]; int i = 0; // 执行边读边写 while ((i = insr.read(ch)) != -1) { outsw.write(ch, 0, i); } // 刷新 outsw.flush(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭流 try { if (outs != null) { outs.close(); } if (ins != null) { ins.close(); } } catch (Exception e) { e.printStackTrace(); } } } public static void check(File file) { try { if (!file.exists()) { file.getParentFile().mkdirs();// 创建文件夹 file.createNewFile();// 创建文件 System.out.println("没有该文件!已创建成功!"); } } catch (Exception e) { e.printStackTrace(); } } } 16: FileReader 17: FileWriter 与上面InputStreamReader 和 OutputStreamWriter没啥区别 ,不多说 18: BufferedReader(缓冲流)特点:字节输入缓冲流,从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,而且提供了很实用的readLine。 构造方法:1、BufferedReader( Reader in ) ; 创建一个使用默认大小输入缓冲区的缓冲字符输入流 2、BufferedReader( Reader in, int sz ) ; 创建一个使用指定大小的输入缓冲区的缓冲字符输入流 常用方法: 该类读取文本的方法就是父类Reader提供的read()系列方法(见上边的 InputStreamReader)。在这里仅介绍他自己特有的方法readLine() 返回值类型 方法名 方法简介 StringreadLine( )读一行文本 --> 特有例: //public String readLine()throws IOException读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。 public static void readLineTest() throws IOException{ BufferedReader br=new BufferedReader(new FileReader("E:\\test\\javaIo\\1.txt")); String str=null; while((str=br.readLine())!=null){ System.out.println(str); } br.close(); } 19: BufferedWriter (缓冲流)特点:字节输出缓冲流,将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。 可以指定缓冲区的大小,或者接受默认的大小。在大多数情况下,默认值就足够大了。 该类提供了 newLine() 方法,它使用平台自己的行分隔符概念,此概念由系统属性 line.separator 定义。 构造方法:1、BufferedWriter( Writer out ) ;创建一个使用默认大小输出缓冲区的缓冲字符输出流 2、BufferedWriter( Writer out, int sz ) ;创建一个新的缓冲字符输出流,该流使用给定大小的输出缓冲区。 常用方法: 该类写入文本的方法就是父类Writer提供的write()系列方法(见上边的 OutputStreamWriter),在这里仅介绍他特有的newLine( )方法。 返回值类型 方法名 方法简介 voidnewLine( )写行分隔符例: public static void newLineTest() throws IOException{ BufferedWriter bw=new BufferedWriter(new FileWriter("E:\\test\\javaIo\\2.txt")); bw.write("爱我"); bw.newLine(); bw.write("中华"); bw.flush(); bw.close(); } //结果:(加入了换行符) 爱我 中华 字符缓冲流 案例:(使用 BufferedReader 和 BufferedWriter) import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.Reader; import java.io.Writer; /** * 字符缓冲流 * * @author kuang * */ public class Demo2 { public static void main(String[] args) { // 1、指明源和目标 File o = new File("F:/text1.txt"); File t = new File("F:/text2.txt"); // 2、构建流的对象 Reader re = null; Writer wt = null; BufferedReader bfr = null; BufferedWriter bfw = null; try { re = new FileReader(o); bfr = new BufferedReader(re); wt = new FileWriter(t); bfw = new BufferedWriter(wt); String msg = null; // 读取操作 while ((msg = bfr.readLine()) != null) {// 读一行 // System.out.println(msg); bfw.write(msg); bfw.write("\r\n");// 执行换行,读一行 写一行 换行 // 或者 :bfw.newLine();//换行 } bfw.flush(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭流 try { if (wt != null) {// 为了避免空指针异常,进行判空 wt.close(); } if (re != null) { re.close(); } } catch (Exception e) { e.printStackTrace(); } } } } 20:ByteArrayInputStream 和缓存输入流一样,只不过他的缓冲区可以一直增加; ByteArrayInputStream包含一个内部缓冲区,其中包含可以从流中读取的字节。 内部计数器跟踪read方法要提供的下一个字节。 案例: public static void main(String[] args) { //当有数据需要读取的时候,获得一个byte数组,或者从别的地方获得了一个byte数组 //就可以用这个方法然后进行读取 ByteArrayInputStream bArray = new ByteArrayInputStream(byte []a,int off,int len) for(int y = 0 ; y < 1; y++ ) { while(( c= bInput.read())!= -1) { System.out.println(Character.toUpperCase((char)c)); } bInput.reset(); } } 21:ByteArrayOutputStream该类实现了将数据写入字节数组的输出流。 当数据写入缓冲区时,缓冲区会自动增长。 数据可以使用toByteArray()和toString() 。当读取文件中的数值然后不写出去,只需要请求接口传送的时候,需要将全部的数据都读在一起,就用他; 案例: public static void main(String[] args) { try { //这个content用来盛放数据 byte[] content = new byte[102400]; // 上传附件 try { int byteread; byte data[] = new byte[1024]; InputStream input = new FileInputStream(new File("C:\\Users\\liulei\\Desktop\\测.xlsx")); System.out.println(input); ByteArrayOutputStream out = new ByteArrayOutputStream(); while ((byteread = input.read(data)) != -1) { out.write(data, 0, byteread); out.flush(); } content = out.toByteArray(); input.close(); System.out.println(input); out.close(); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { // TODO: handle exception } } |
CopyRight 2018-2019 实验室设备网 版权所有 |