JavaのIOストリーム

  その後の1の「JavaのFileクラス、」エッセイは、Fileクラスに基づいて、我々はJavaのIO、それをストリームに入りました。

 

概念と役割ストリーム

  流れがシーケンシャルのセットで、起動とバイトの集まりで終わる、それはデータ伝送または抽象の総称です。2つのデバイス間でデータ伝送が流れ、ストリームデータ伝送の性質上、フロー抽象的な、より直感的に便利なデータ操作の様々なタイプのデータ伝送特性と呼ばれています。

 

ストリームの分類:

  (1)は、異なるデータフローによると、に分けられる:(のみ書き込まれていない、読み取り可能なメモリになど、ディスク、ネットワーク、から読み出さ)入力ストリームメモリからディスクに書き込まれた出力ストリームを(ネットワークができません、書き込むことができます読書)

  (2)に分割処理単位データに応じて:(バイト基本演算単位)バイトストリーム、文字列(文字の動作の基本単位)

  (3)、異なる役割に応じて分割:ノードストリームパッケージングするための(、プロセスフロー(IO直接データ、また、より低いストリームと呼ばれるデバイス/ノード(ファイル・ディスク、ネットワーク等)への書き込み)は、既存のストリームは、を介して既存のフローに読み出し動作および書き込み、直接的に)、高度なストリーム、フローパッキングとして知られている、IOノードを動作しません

 

Javaのストリームクラスの構造:

分類

バイトの入力ストリーム

バイト出力ストリーム

文字入力ストリーム

文字出力ストリーム

抽象ベースクラス

InputStream

OutputStream

リーダー

ライター

アクションファイル

FileInputStreamを

FileOutputStream

FileReaderの

FileWriter

操作配列

するByteArrayInputStream

ByteArrayOutputStream

CharArrayReader

CharArrayWriter

文字列操作

   

StringReader

StringWriter

バッファの流れ

BufferedInputStreamを

なBufferedOutputStream

BufferedReaderの

BufferedWriterの

変換の流れ

   

InputStreamReader

OutputStreamWriter

オブジェクトストリーム(シリアル化)

ObjectInputStreamの

ObjectOutputStreamの

   

抽象基底クラス(フィルタリング用)

FilterInputStream

FilterOutputStream

FilterReader

FilterWriter

プリントストリーム(出力機能は非常に大きいです)

 

PrintStream

 

PrintWriter

 

ジャワのIOストリームを使用します

バイトの入力ストリーム入力ストリームは、この方法を用いるには、最上位の親クラスです。

  • intは読んで()//読み込まれたバイトがデータバイトのUnicodeコード値を返します。
  • 読み取りINT(バイト[]バフ)//配列バフでbuff.lengthバイト、リードデータを読み出す、バイト数を返す実際に読み取ら
  • 読み取りINT(バイト[]バフ、オフINT、INT長)//リード配列からオフ位置データから始まる、長さにアレイ上のバフをバイト、実際に読み取られたバイト数を返します。一般的に0にオフに設定し、長さは、一般的にバフの長さに設定されています。
        //1、创建一个File类的对象
        File file = new File("hello.txt");
        //2、创建一个FileInputStream类的对象
        FileInputStream fis = new FileInputStream(file);
        //3、调用FileInputStream的方法,实现file文件的读取
        int b = fis.read();
        while(b != -1){
            System.out.print((char)b+" ");
            b = fis.read();
        }
        fis.close();        

 

 

 

OutputStream是字节输出流的顶级父类,常用方法:

  • void  write(int  i)    \\输出一个字节,i是码值,即read()得到的码值,输出i对应的字节
  • void  write(byte[]  buff)   //输出整个字节数组的内容
  • void  write(byte[]  buff, int  off, int  length)    //把字节数组从off位置开始,输出长度为length字节的内容
        File file = new File("hello1.txt");
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream(file);
            fos.write(new String("\nI Love CYT!").getBytes());
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

 

 

Reader时字符输入流的顶级父类,常用方法:

  • int  read()     //读取一个字符,返回该字符的Unicode码值,注意并不是返回该字符。
  • int  read(char[]  buff)   //最多读取buff.length个字符,放在buff数组中,返回实际读取的字符数
  • int  read(char[]  buff, int  off, int  length) //最多读取length个字节,放在buff数组中,从数组的off位置开始放置数据,返回实际读取的字符数。off一般设置为0,length一般设置为buff的长度
            File file = new File("1.txt");
            FileReader fr = null;
            try{
                fr = new FileReader(file);
                char[] c = new char[24];
                int len;
                while((len = fr.read(c)) != -1){
                    for(int i=0;i<len;i++){
                        System.out.print(c[i]);
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                if(fr != null){
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }    

     

 

 

 

Writer是字符输出流的顶级父类,常用方法:

  • void  write(int  i)    //输出一个字符,i是码值,输出的是i对应的字符
  • void  write(char[]  buff)    //输出整个char[]的内容
  • void  write(char[]  buff,  int  off,  int  length)      //把char[]从off位置开始,输出长度为length字符的内容

可以用String代替char[],所以Writer还具有以下2个方法:

  • void  write(String str)
  • void  write(String str, int off, int length)
        FileWriter fw = null;
        try{
            fw = new FileWriter(new File("1.txt"));
            String str = "我喜欢Java,我要成为Java工程师。";
            fw.write(str);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

 

缓冲流是和4级顶级父类对应的:加前缀Buffered

  InputStream   BufferedInputStream   字节输入缓冲流,可作为所有字节输入流类的缓冲流

  OutputStream   BufferedOutputStream    字节输出缓冲流

  Reader     BufferedReader    字符输入缓冲流

  Writer  BufferedWriter    字符输出缓冲流

//用 BufferedReader 把文件读进来, 再用 BufferedWriter 把内容写出去
        File file = new File("hello.txt");
        File file1 = new File("hello3.txt");
        FileReader fr = null;
        BufferedReader br = null;
        FileWriter fw = null;
        BufferedWriter bw = null;
        try{
            fr = new FileReader(file);
            fw = new FileWriter(file1);
            br = new BufferedReader(fr);
            bw = new BufferedWriter(fw);
            String str;
            while((str = br.readLine()) != null){
//                System.out.println(str);
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

 

其他方法我就不一一说明了。有兴趣的同学可以自己再深入研究。

最后我就提供文件复制的通用方法吧。自己写的通用类。让自己更好的去了解Java的IO流的使用。

package io;
/*
 * 实现文件的复制
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyFile {

    // 字节文件的复制方法
    public static void copyFileByte(String src, String dest) {
        // 1、提供读入、写出的文件
        File file1 = new File(src);
        File file2 = new File(dest);
        // 2、提供相应的流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            // 3、实现文件的复制
            byte[] b = new byte[20];
            int len;
            while ((len = fis.read(b)) != -1) {
                fos.write(b, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /*
     * 使用FileReader 、 FileWriter 可以实现文本文件的复制
     * 对于非文本文件(视频文件、音频文件、图片),只能使用字节流复制。
     */
    public static void copyFileChar(String src, String dest) {
        // 1、提供读入、写出的文件
        File file1 = new File(src);
        File file2 = new File(dest);
        // 2、提供相应的流
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader(file1);
            fw = new FileWriter(file2);
            // 3、实现文件的复制
            char[] c = new char[24];
            int len;
            while ((len = fr.read(c)) != -1) {
                fw.write(c, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    //使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制
    public static void copyFileBuffered1(String src, String dest) {
        //1、提供读入、写出的文件
        File file1 = new File(src);
        File file2 = new File(dest);
        //2、先创建相应的节点流:FileInputStream、 FileOutputStream
        FileInputStream fis = null;
        FileOutputStream fos = null;
        //3、再创建缓冲流:BufferedInputStream 、 BufferedOutputStream
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            //4、具体实现文件复制的操作
            byte[] b = new byte[1024];
            int len;
            while((len = bis.read(b)) != -1){
                bos.write(b, 0, len);
                bos.flush();
            }
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

现在自己也准备大四了,所以自己要捉紧时间去学习,博客里面有什么问题的可以跟我说一下,大家一起交流一下学习的经验。

 

おすすめ

転載: www.cnblogs.com/HHHY/p/10964567.html