目录

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流图解:

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 int read( ) 从此输入流中读取下一个字节(此方法是抽象方法,子类必须实现该方法。
int read(byte [ ] b ) 从输入流中读取一定数量的字节,存储在参数指定的字节数组中。
int read(byte [ ] b ,int off ,int len ) 读到 len字节从输入流读入字节数组数据。
long skip( long n ) 跳过并丢弃 n字节从输入流中的数据。
int available( ) 返回此输入流下一个方法可以读取的字节数。
void close( ) 关闭流。

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

常用方法:

返回值类型

方法名

方法简介

void flush( ) 刷新输出流,是缓存数据被写出来
void write(byte [ ] b) 写b.length 字节从指定的字节数组的输出流
void write(byte [ ] b, int off, int len) 写len字节从字节数组数据中到输入流
abstract int write( int b ) 将指定的字节写入该输出流中
void close( ) 关闭流

3:FileInputStream

FileInputStream是读取原始字节的图像数据流。读取字符流,考虑使用FileReader。常用方法见InputStream中的方法

构建输入流:(准备工作)

FIle file =new File(path);InputStream ins =null;ins =new FileInputStream(file); //这里需要try catch

3.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 {// 构造方法1File 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)

返回值类型

方法名

方法简介

void flush( ) 刷新输出流,是缓存数据被写出来
void write(byte [ ] b) 写b.length 字节从指定的字节数组的输出流
void write(byte [ ] b, int off, int len) 写len字节从字节数组数据中到输入流
abstract int write( int b ) 将指定的字节写入该输出流中
void close( ) 关闭流

应用主要步骤: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 int read( ) 从此输入流中读取下一个字节(此方法是抽象方法,子类必须实现该方法。
int read(byte [ ] b ) 从输入流中读取一定数量的字节,存储在参数指定的字节数组中。
int read(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:常用方法

返回值类型

方法名

方法简介

void flush( ) 刷新输出流,是缓存数据被写出来
void write(byte [ ] b) 写b.length 字节从指定的字节数组的输出流
void write(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:常用方法:

返回值类型

方法名

方法简介

int read(byte [ ]  b) 读取一定数量的字节从包含输入流并存入缓冲区阵列b
int read (byte [ ]  b, int off, int len) 读到 len 从包含输入流读入字节数组数据字节
boolean readBoolean( ) 读 true / false
byte readByte( ) 读取并返回一个输入字节
char readChar( ) 读取两个字节返回一个 char 价值
float readFloat( ) 四字节读取输入并返回一个 float 价值
double readDouble( ) 八字节读取输入并返回一个 double 价值
int readInt( ) 四字节读取输入并返回一个 int 价值
String readLine( ) 从输入流读取下一行文本
short readShort( ) 两字节读取输入并返回一个 short 价值
String readUTF( ) 读取一个字符串,一直采用 modified UTF-8 编码格式

10:  DataOutputStream

数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。 DataInputStream 对于多线程访问不一定是安全的。 线程安全是可选的,它由此类方法的使用者负责。

构造方法: DataOutputStream( OutputStream outs);

10.1:常用方法:

返回值类型

方法名

方法简介

void flush( ) 刷新数据输出流
int size( ) 返回柜台 written 电流值,这个数据写入输出流的字节数为止
void write( int b ) 将指定的字节(论证b的低八位)的底层输出流
void write( byte [ ] b, int off, int len ) 写 len 字节指定字节数组中的起始偏移量 off 当前输出流
void writeBoolean( boolean v ) 写一个 boolean 当前输出流作为一个字节的值
void writeByte( int v ) 写了 byte 当前输出流作为一个字节的值
void writeBytes( String s ) 将字符串写入到基础的输出流作为字节序列
void writeChar( int v ) 写一个char当前输出流作为一个双字节的值,高字节优先
void writeChars( String s ) 将字符串写入底层输出流,作为一个字符序列
void writeFloat( float v ) 将浮点型参数的 int使用 floatToIntBits方法在类 Float,然后写道, int值基本为4字节输出流量,高字节优先。
void writeDouble( double v ) 将双参数到一个 long使用 doubleToLongBits方法在类 Double,然后写道, long值基本的输出流的字节的数量,高字节优先。
void writeInt( int v ) 写一个 int当前输出流为四个字节,高字节优先。
void writeShort( int v ) 写一个 short当前输出流的两个字节,高字节优先。
void writeUTF( 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:常用方法:

返回值类型

方法名

方法简介

void close( ) 关闭输入流
int read( ) 读取一个字节的数据
int read( byte [ ] b, int off, int len )  读入一个字节数组
boolean readBoolean( ) 在布尔值中读取
byte readByte( ) 读取一个8位字节
char readChar( ) 读取一个16位字符
double readDouble( )
float readFloat( )
int readInt( ) 读取一个32位的int
String readLine( )   过时的
Object readObject( ) 读取对象输入流中的对象
String readUTF( ) 读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:常用方法:

返回值类型

方法名

方法简介

void close( ) 关闭流
void flush( ) 刷新流
protect Object replaceObject( ) 这种方法将允许受信任的子类对象来代替一个物体对另一个序列化期间
void write( byte [ ] b ) 写入一个字节数组
void write( byte [ ] b, int off, int len ) 写入字节数组的字节数
void write( int val ) 写一个字节
void writeBoolean( boolean val ) 写一个布尔值
void writeByte( byte val ) 写入一个8位字节
void writeChar( int val ) 写入一个16位字符
void writeInt( int val ) 写入一个32位长的int
void writeObject( Object obj ) 写入指定对象的对象
void writeUTF( 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:常用方法:

返回值类型

方法名

方法简介

void close( ) 关闭流
String getEncoding( ) 返回此流使用的字符编码名称
int read( ) 读取单个字符
int read( char [ ] ch, int off, int len ) 将字符读入一个数组的一部分
boolean ready( ) 告诉是否该流已准备好阅读

读文件内容的常用三种方法:

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”) ;  可以设置编码格式

常用方法:

返回值类型

方法名

方法简介

void close( ) 关闭流
void flush( ) 刷新流
String getEcoding( ) 返回此流使用的字符编码格式名称
void writer( int val ) 写入一个字符
void writer( char [ ]ch, int off, int len ) 写入一个字符数组的一部分
void writer( 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-8System.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-8outs = new FileOutputStream(t);// 作为写入的目标文件outsw = new OutputStreamWriter(outs, "UTF-8");//编码格式为UTF-8char[] 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()

返回值类型

方法名

方法简介

String readLine( ) 读一行文本 --> 特有

例:

//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( )方法。

返回值类型

方法名

方法简介

void newLine( ) 写行分隔符

例:

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}}

javaIO流详解--读取,写入文件的所有类与方法相关推荐

  1. JavaIO流详解——Java教案(十)

    文章目录 IO流 相关资源 1. File 访问文件和目录 相对路径和绝对路径 创建文件 查看文件列表 获取所有磁盘的根路径 文件过滤器 小结 2. IO流 流的分类 流的概念模型 3. 字节流和字符 ...

  2. Java--IO流详解

    目录 一.IO流的介绍 二.IO流的具体详解 (1)字节流 1:FileOutputStream 2:FileInputStream (2)字符流 1:解码编码 2:FileReader 3:File ...

  3. python open写入_Python3 open() 函数详解 读取文件写入文件追加文件二进制文件

    Python3 open() 函数详解 读取文件写入文件追加文件二进制文件 open()函数的主要作用是打开文件并返回相应文件对象,使用文件对象可以对当前文件进行读取.写入.追加等操作,默认情况下&q ...

  4. 技术揭秘之详解回收站删除文件恢复

    |=------------------------------------------------------------------------=| |=--------------=[技术揭秘之 ...

  5. java io流详解_一文带你看懂JAVA IO流,史上最全面的IO教学啦

    一.IO流是什么 惯例引用百科的回答流是一种抽象概念,它代表了数据的无结构化传递.按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列.从流中取得数据的操作称为提取操作,而向流中添加数据的操作 ...

  6. Java 中IO流详解(附实例代码/面试题)

    Java I/O流详解 前言 一.I/O流是什么? 二.IO流分类: 1. 流程图: io流对象 2. io流的优缺点: 3. io 流Java中用途有哪些? 三.一些 io 实例 四.面试题: 前言 ...

  7. python怎么读写文件-手机上怎么写pythonPython文件读写详解及设置文件的字符编码...

    文件读写操作在各种编程语言中都是比较重要的部分,也是很常用的部分,今天就来详细说一下python对文件的读写操作,以及需要注意的点. 一. python打开文件 代码如下:f = open(" ...

  8. Java IO 字节流、字符流详解

    1. 字节流与字符流 流是一个很形象的概念,当程序需要读取数据的时候,就会开启一个通向数据源的流,这个数据源可以是文件,内存,或是网络连接.类似的,当程序需要写入数据的时候,就会开启一个通向目的地的流 ...

  9. 详解HTTP的文件上传全过程(RFC1867协议)

    详解HTTP的文件上传全过程:RFC1867协议 HTTP请求头 HTTP请求体 什么是 multipart/form-data 上传文件信息的内容组成 原生Node实现客户端上传文件 原生Golan ...

最新文章

  1. Dell Fluid FS 集群NAS系统在4K非编环境的卓越表现
  2. 计算机数据库技术应用了什么,2018年全国计算机三级数据库技术考试考什么内容...
  3. python怎么画两幅图_python matplotlib模块: Subplots(在同一个figure里绘制多个图)
  4. (2016北京集训十二)【xsy1542】疯狂求导
  5. 文件下载时,文件名乱码问题
  6. iOS开发造轮子 | 通用占位图
  7. Android 11首个预览版发布:打开飞行模式蓝牙耳机不断连
  8. 具备这些特征,轻松进入互联网大厂成为web前端工程师
  9. Nginx查看并发链接数
  10. 让 API 端点的响应速度提高 50 倍!
  11. webservice和restful的区别
  12. MATLAB作图颜色
  13. e5服务器系列天梯图,至强e5处理器天梯图_至强E5系列CPU排行榜高清图2020-win7之家...
  14. 【0514 更新中】CVPR 2019 论文汇总 按方向划分
  15. 面试之其他面试题归类
  16. java后台 学习顺序
  17. 看到一篇详细的关于Python之Numpy教程分享给和我一样在编程上的小白
  18. Web前端学习(千锋)
  19. 社交网络的致瘾性设计如何夺走我们的时间?
  20. bids格式数据中config文件的编写

热门文章

  1. 简单实用的Mac视频转换软件:UniConverter mac中文版
  2. 惠普笔记本win10系统蓝牙功能突然失效的解决办法
  3. gks的定义计算机图形学,计算机图形学(概念)
  4. 世界第五大外包公司(CGI group inc)能够做出来多烂的网站,通过系分的课程设计我对于人机交互和团队合作的几个总结
  5. 微信个性签名怎么弄成竖的?不仅可以竖着还可以加边框效果!
  6. Google Earth Engine-06(GEE操作方法)
  7. 我们好好聊聊华为Harmony OS,鸿蒙对于智能家居来说意味着什么?
  8. 网约车风云再起:“小窗口”和“大窗口”齐开
  9. ML之LoR:基于信用卡数据集利用LoR逻辑回归算法实现如何开发通用信用风险评分卡模型之以toad框架全流程讲解
  10. 周一见!距离阿里巴巴开源开放周还有3天