Home  >  Article  >  Java  >  Detailed explanation of examples of IO streams

Detailed explanation of examples of IO streams

PHP中文网
PHP中文网Original
2017-06-21 09:55:091660browse

IO stream:
I:input, input, read to memory
O:Output, output, write to file
Stream: data stream (characters, bytes)
Category:
Flow direction:
Input: byte input stream (FileInputStream), character input stream (FileReader)
Output: byte output stream (FileOutputStream), character input stream (FileWriter)
Type:
Character, byte

java.io.OutputStream: Byte output stream, all bytes output Parent class of stream

Public member methods:
abstract void write(int b) writes a byte
void write(byte[] b) writes a word Section array
void write(byte[] b, int off, int len) writes the byte array, off is the starting index, write several len
void close() closes this output stream and releases this All system resources related to streams.

java.io.FileOutputStream: File byte output stream
Function: Write the data in the memory to the file in the form of bytes

Construction method :
FileOutputStream(File file) Creates a file output stream that writes data to the file represented by the specified File object.
FileOutputStream(String name) Creates an output file stream that writes data to the file with the specified name.
Parameters of the construction method:
file: file
name: path of the file
are the destination for writing data


##Features:

If the file specified in the construction method or the file pointed to by the file path does not exist, the construction method will create a file

If the append writing switch is not turned on and the file already exists, it will be overwritten.


Usage steps: 1. Create a byte output stream object FileOutputStream and bind the destination of the data
2. Use the write method in FileOutputStream, Write data to the file
3. Release resources

When the stream writes data, it will find the JVM. The JVM will call the system's local method to complete the writing. After using the stream, you must use the system-related Resources (release memory)

void write(byte[] b) Write byte array

void write(byte[] b, int off, int len) Write byte array, off is the start For the index, write a few len

 1 public static void main(String[] args) throws IOException { 2         File file = new File("b.txt"); 3         FileOutputStream fos = new FileOutputStream(file); 4         //写入100到文件中,100是3个字节 5         fos.write(49); 6         fos.write(48); 7         fos.write(48); 8          9         /*10          * void write(byte[] b) 写入字节数组11          * 写入数据的时候,如果一次写多个字节12          * 写入的字节是正数:会查询ASC||表13          * 写入的字节是,第一个字节是负数,第二个字节可以是正数,也可以是负数,查询的时候就会把两个字节组成一个中文,查询GBK编码表14          */15         byte[] bytes = {65,66,67,68,69};//ABCDE16         //byte[] bytes = {-65,-66,-67,68,88};//烤紻17         18         fos.write(bytes);19         20         //void write(byte[] b, int off, int len) 写入字节数组,off是开始的索引,len写几个21         fos.write(bytes, 1, 2);22         23         /*24          * 快速写入字节数组的方法25          * String类中有一个方法26          * byte[] getBytes(String charsetName) :把字符串转换为字节数组 
27          */28         byte[] bytes2 = "你好".getBytes();29         System.out.println(Arrays.toString(bytes2));//[-60, -29, -70, -61]30         fos.write(bytes2);31         32         fos.close();33     }

void write(byte[] b) 写入字节数组
写入数据的时候,如果一次写多个字节
写入的字节是正数:会查询ASC||表
写入的字节是,第一个字节是负数,第二个字节可以是正数,也可以是负数,查询的时候就会把两个字节组成一个中文,查询GBK编码表

快速写入字节数组的方法
String类中有一个方法
byte[] getBytes(String charsetName) :把字符串转换为字节数组

文件的续写和换行:
换行:
windows:\r\n
linux:\n
mac:\r
追加写:使用两个参数的构造方法
FileOutputStream(File file, boolean append)
FileOutputStream(String name, boolean append)
参数:
File file,String name:写入数据的目的地
boolean append:追加写的开关,true:可以追加写(往之前的文件,继续写内容),fasle:不能追加写(覆盖之前的文件)

java.io.InputStream:字节输入流,是所有字节输入流的父类

公共的成员方法:
int read():读取一个字节并返回,没有字节返回-1.
int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。
void close() 关闭此文件输入流并释放与此流有关的所有系统资源。

java.io.FileInputStream:文件字节输入流
作用:把文件中的数据,以字节的方式读取到内存中

构造方法:
FileInputStream(String name)
FileInputStream(File file)
参数:读取的哪个文件(数据源)
String name:字符串的文件路径
File file:读取的文件

使用步骤:
1.创建字节输入流对象FileInputStream,并且绑定数据源
2.使用FileInputStream对象中的方法read,读取文件
3.释放资源


int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。
 明确:
  1.byte数组的作用:起到缓冲作用,一次可以往数组中缓冲多个字节,可以提高读取效率
  byte数组的长度:一般定义为1024(一kb字节)或者1024的整数倍
  2.返回值int是什么:每次读取的有效字节个数

文件的复制:读取文件采用一次读取一个字节的方式,写入文件采用一次写一个字节的方式

 数据源:c:\\1.jpg
 数据目的地:d:\\1.jpg

操作步骤:
  1.创建字节输入流对象FileInputStream,并且绑定数据源
  2.创建字节输出流对象FileOutputStream,并且绑定数据目的地
  3.使用FileInputStream中的方法read,一次读取一个字节
  4.使用FileOutputStream中的方法write,一次写一个字节
  5.释放资源(先关写,后关读)

 1 public static void main(String[] args) throws IOException { 2         long s = System.currentTimeMillis(); 3         //1.创建字节输入流对象FileInputStream,并且绑定数据源 4         FileInputStream fis = new FileInputStream("c:\\1.jpg"); 5         //2.创建字节输出流对象FileOutputStream,并且绑定数据目的地 6         FileOutputStream fos = new FileOutputStream("d:\\1.jpg"); 7         //3.使用FileInputStream中的方法read,一次读取一个字节 8         int len = 0;//接收读取到的字节 9         while((len = fis.read())!=-1){10             //4.使用FileOutputStream中的方法write,一次写一个字节11             fos.write(len);12         }13         //5.释放资源(先关写,后关读)14         fos.close();15         fis.close();16         long e = System.currentTimeMillis();17         System.out.println(e-s);18     }

文件的复制:读取文件采用字节数组缓冲读取,写入数据一次写入多个字节
*
* 数据源:c:\\1.jpg
* 数据目的地:d:\\1.jpg
*
* 操作步骤:
* 1.创建字节输入流对象FileInputStream,并且绑定数据源
* 2.创建字节输出流对象FileOutputStream,并且绑定数据目的地
* 3.使用FileInputStream中的方法read(byte[]),一次读取多个字节
* 4.使用FileOutputStream中的方法write(byte[],0,len),一次写多个字节
* 5.释放资源

 1 public static void main(String[] args) throws Exception { 2         long s = System.currentTimeMillis(); 3         //1.创建字节输入流对象FileInputStream,并且绑定数据源 4         FileInputStream fis =  new FileInputStream("c:\\z.zip"); 5         //2.创建字节输出流对象FileOutputStream,并且绑定数据目的地 6         FileOutputStream fos = new FileOutputStream("d:\\z.zip"); 7         //3.使用FileInputStream中的方法read(byte[]),一次读取多个字节 8         byte[] bytes = new byte[1024*100]; 9         int len = 0;//读取的字节有效个数10         while((len = fis.read(bytes))!=-1){11             //4.使用FileOutputStream中的方法write(byte[],0,len),一次写多个字节12             fos.write(bytes, 0, len);13         }14         //5.释放资源15         fos.close();16         fis.close();17         18         long e = new Date().getTime();19         System.out.println(e-s);20     }

文件的复制:读取文件使用缓冲流+数组,写入数据使用缓冲流一次写入多个
*
* 数据源:c:\\1.jpg
* 数据目的地:d:\\1.jpg
*
* 操作步骤:
* 1.创建FileInputStream对象,绑定数据源
* 2.创建BufferedInputStream对象,构造方法中传入FileInputStream,提高FileInputStream的读取效率
* 3.创建FileOutputStream对象,绑定数据目的地
* 4.创建BufferedOutputStream对象,构造方法中传递FileOutputStream,提高FileOutputStream效率
* 5.使用BufferedInputStream中的方法read(byte[]),读取文件
* 6.使用BufferedOutputStream中的方法write(byte[],0,len),写入数据到缓冲区
* 7.使用BufferedOutputStream中的方法flush把缓冲区的数据,刷新到文件中
* 8.释放资源

 1 public static void main(String[] args) throws Exception { 2         long s = System.currentTimeMillis(); 3         //1.创建FileInputStream对象,绑定数据源 4         FileInputStream fis = new FileInputStream("c:\\z.zip"); 5         //2.创建BufferedInputStream对象,构造方法中传入FileInputStream 6         BufferedInputStream bis = new BufferedInputStream(fis); 7         //3.创建FileOutputStream对象,绑定数据目的地 8         FileOutputStream fos = new FileOutputStream("d:\\z.zip"); 9         //4.创建BufferedOutputStream对象,构造方法中传递FileOutputStream,提高FileOutputStream效率10         BufferedOutputStream bos = new BufferedOutputStream(fos);11         //5.使用BufferedInputStream中的方法read(byte[]),读取文件12         /*int len = 0;13         while((len = bis.read())!=-1){14             //6.使用BufferedOutputStream中的方法write(byte[],0,len),写入数据到缓冲区15             bos.write(len);16         }*/17         byte[] bytes = new byte[1024*100];18         int len = 0;19         while((len = bis.read(bytes))!=-1){20             fos.write(bytes, 0, len);21             fos.flush();22         }23         24         //8.释放资源25         bos.close();26         bis.close();27         28         long e = new Date().getTime();29         System.out.println(e-s);30     }

java.io.BufferedOutputStream:字节缓冲输出流 extends OutputStream
* 字节缓冲输出流作用:给基本流增加一个缓冲区,提高基本流的效率
*
* 继承自父类的公共的成员方法
* abstract  void write(int b) 写入一个字节
* void write(byte[] b) 写入字节数组
* void write(byte[] b, int off, int len) 写入字节数组,off是开始的索引,len写几个
* void close() 关闭此输出流并释放与此流有关的所有系统资源。
*
* 构造方法:
* BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
* 参数:
* OutputStream out:字节输出流,可以使用FileOutputStream
* 参数传递时哪个字节输出流,就会给哪个字节输出流增加一个缓冲区,提高这个流的效率
* 使用步骤:
* 1.创建FileOutputStream对象,绑定数据目的地
* 2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream,提高FileOutputStream效率
* 3.使用BufferedOutputStream中的方法write,把数据写入到缓冲区中
* 4.使用BufferedOutputStream中的方法flush,把缓冲区中的数据,刷新到文件中
* 5.释放资源

 1  public static void main(String[] args) throws IOException { 2         //1.创建FileOutputStream对象,绑定数据目的地 3         FileOutputStream fos = new FileOutputStream("buffered.txt"); 4         //2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream 5         BufferedOutputStream bos = new BufferedOutputStream(fos); 6         //3.使用BufferedOutputStream中的方法write,把数据写入到缓冲区中 7         bos.write(97); 8          9         bos.write("我是缓冲流".getBytes());10         //4.使用BufferedOutputStream中的方法flush,把缓冲区中的数据,刷新到文件中11         bos.flush();12         //5.释放资源13         bos.close();14     }

java.io.BufferedInputStream:字节缓冲输入流 extends InputStream
* 作用:给基本字节输入流增加一个缓冲区,提高基本字节输入流的效率
*
* 继承自父类的公共发的成员方法:
* int read():读取一个字节并返回,没有字节返回-1.
* int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。
* void close() 关闭此文件输入流并释放与此流有关的所有系统资源。
*
* 构造方法:
* BufferedInputStream(InputStream in) 创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
* 参数:
* InputStream in:字节输入流,可以传递FileInputStream
* 参数传递的是哪个字节输入流对象,就会给哪个字节输入流对象增加一个缓冲区,提高该流的效率
*
* 使用步骤:
* 1.创建FileInputStream对象,绑定数据源
* 2.创建BufferedInputStream对象,构造方法中传入FileInputStream,提高FileInputStream的读取效率
* 3.使用BufferedInputStream中的方法read,读取文件
* 4.释放资源
*
* 总结:
* 字节流:操作的文件是非文本文件,文件的复制
* 字符流:操作的文件都是文本文件,一次读取一个字符,可以读取中文
* 文本文件:使用记事本打开,能看懂

 1 public static void main(String[] args) throws IOException { 2         //1.创建FileInputStream对象,绑定数据源 3         FileInputStream fis = new FileInputStream("buffered.txt"); 4         //2.创建BufferedInputStream对象,构造方法中传入FileInputStream 5         BufferedInputStream bis = new BufferedInputStream(fis); 6         //3.使用BufferedInputStream中的方法read,读取文件 7         //int read():读取一个字节并返回,没有字节返回-1. 8         /*int len = 0; 9         while((len = bis.read())!=-1){10             System.out.println((char)len);11         }*/12         13         //int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。14         byte[] bytes = new byte[1024];15         int len = 0;16         while((len = bis.read(bytes))!=-1){17             System.out.println(new String(bytes,0,len));            
18         }19         20         //4.释放资源21         bis.close();22         System.out.println("-------------------");23         BufferedReader br = new BufferedReader(new FileReader("buffered.txt"));24         while((len = br.read())!=-1){25             System.out.println((char)len);26         }27     }

复制单层文件夹
*
* 数据源:c:\\demo
* 数据目的地:d:\\
*
* 操作流程:
* 1.判断d盘有没有demo文件夹,没有则创建
* 2.创建一个复制文件的方法
* 返回值类型:void
* 方法名:copyFile
* 参数列表:File src,File dest
* 3.遍历要复制的文件夹,获取文件夹中的每一个文件的路径(要复制文件的数据源)
* 4.使用file类中的方法getName拼接要复制文件的数据目的
* 5.调用copyFile复制的方法进行复制

 1 public static void main(String[] args) throws IOException { 2         //1.判断d盘有没有demo文件夹,没有则创建 3         File file = new File("d:\\demo"); 4         if(!file.exists()){ 5             file.mkdirs(); 6         } 7         //3.遍历要复制的文件夹,获取文件夹中的每一个文件的路径(要复制文件的数据源) 8         File srcDirectory = new File("c:\\demo"); 9         File[] srcFiles = srcDirectory.listFiles();10         for (File srcFile : srcFiles) {11             //4.使用file类中的方法getName拼接要复制文件的数据目的12             String srcName = srcFile.getName();13             //使用File类的第三个构造方法创建目的地14             File destFile = new File(file, srcName);15             //5.调用copyFile复制的方法进行复制16             copyFile(srcFile, destFile);17         }18     }

 

The above is the detailed content of Detailed explanation of examples of IO streams. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn