Heim  >  Artikel  >  Java  >  Ausführliche Erläuterung von Beispielen für IO-Streams

Ausführliche Erläuterung von Beispielen für IO-Streams

PHP中文网
PHP中文网Original
2017-06-21 09:55:091661Durchsuche

IO-Stream:
I: Eingabe, Eingabe, in den Speicher einlesen
O: Ausgabe, Ausgabe, in Datei schreiben
Stream: Datenstrom (Zeichen, Bytes)
Kategorie:
Flussrichtung:
Eingabe: Byte-Eingabestream (FileInputStream), Zeichen-Eingabestream (FileReader)
Ausgabe: Byte-Ausgabestream (FileOutputStream) , Zeicheneingabestream (FileWriter)
Typ:
Zeichen, Byte

java.io.OutputStream: Byte-Ausgabestream, der die Ausgabe aller Bytes der übergeordneten Klasse darstellt of stream

Öffentliche Mitgliedsmethoden:
abstract void write(int b) schreibt ein Byte
void write(byte[] b) schreibt ein Wort Abschnittsarray
void write(byte[] b, int off, int len) Schreiben Sie in das Byte-Array, off ist der Startindex, schreiben Sie mehrere len
void close() Schließen Sie diesen Ausgabestream und geben Sie alle mit Streams verbundenen Systemressourcen frei.

java.io.FileOutputStream: Datei-Byte-Ausgabestream
Funktion: Schreiben Sie die Daten im Speicher in Bytes in die Datei

Konstruktionsmethode:
FileOutputStream(File file) Erstellt einen Dateiausgabestream, der Daten in die Datei schreibt, die durch das angegebene File-Objekt dargestellt wird.
FileOutputStream(String name) Erstellt einen Ausgabedateistream, der Daten in eine Datei mit dem angegebenen Namen schreibt.
Parameter der Konstruktionsmethode:
Datei: Datei
Name: Pfad zur Datei
sind alle Ziele zum Schreiben von Daten


Eigenschaften:

Wenn die in der Konstruktionsmethode angegebene Datei oder die Datei, auf die der Dateipfad verweist, nicht existiert, erstellt die Konstruktionsmethode eine Datei.
Wenn der Schalter zum Anhängen des Schreibens nicht aktiviert ist und die Datei bereits vorhanden ist existiert, wird es überschrieben.


Verwendungsschritte :
1. Erstellen Sie ein Byte-Ausgabestream-Objekt FileOutputStream und binden Sie das Ziel der Daten
2. Verwenden Sie die Schreibmethode in FileOutputStream, Daten in die Datei schreiben
3. Ressourcen freigeben

Wenn der Stream Daten schreibt, findet er die JVM und die JVM ruft die lokale Methode des Systems auf, um den Schreibvorgang abzuschließen. Nach der Verwendung des Streams müssen Sie die systembezogenen Ressourcen verwenden (Speicher freigeben)

void write(byte[] b) Byte-Array schreiben
void write(byte[] b, int off, int len ) Byte-Array schreiben, Off ist der Anfang. Schreiben Sie für den Index mehrere

 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     }

 

Das obige ist der detaillierte Inhalt vonAusführliche Erläuterung von Beispielen für IO-Streams. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn