IOストリーム - 他のストリーム

メモリの動作フロー

このストリームは、任意のファイル、彼はメモリ内のバッファを維持することをメモリ内のデータのみに関連付けられていない、我々は常に、バッファの書き込みデータを保持し、彼に行くことができ、我々は、バッファから出てデータを書き込むことができます

ByteArrayOutputStream

れるByteArrayInputStream:データをバイト配列に書き込まれたこのクラスの実装出力ストリーム。バッファを使用して自動的にデータの増加を書き込みます。toByteArray()とのtoString()データ取得を使用して。閉じずにメモリ動作の流れ

ケースプレゼンテーション:

public class MyTest2 {
    public static void main(String[] args) throws IOException {
      //ByteArrayOutputStream()
      //创建一个新的 byte 数组输出流。
      //创建出内存操作流 他维护着一个字节数组来充当缓冲区
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write("好好学习".getBytes());
        bos.write("天天向上".getBytes());
        bos.write("爱生活".getBytes());
        bos.write("爱Java".getBytes());
        byte[] bytes = bos.toByteArray();


      read(bytes);
}

    private static void read(byte[] bytes) throws IOException {
          /*  ByteArrayInputStream( byte[] buf)
        创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组。*/
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        byte[] bytes1 = new byte[1024];
        int len = bis.read(bytes1);
        String s = new String(bytes1, 0, len);
        System.out.println(s);

    }
}

文字のオペレーティング配列

CharArrayWrite:このクラスが実装文字入力バッファは、文字ストリームとして使用されています。
CharArrayReader:このクラスが実装Writerとして使用する文字バッファ。データバッファを使用して自動的に増やすストリームに書き込まれます。使用できるtoCharArray()toString()をデータを取得します。

ケースプレゼンテーション:

public class MyTest3 {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();
        charArrayWriter.write("abc");
        charArrayWriter.write("abc");
        String s = charArrayWriter.toString();//调用toString()来获取字符数组
        System.out.println(s);
        char[] chars = charArrayWriter.toCharArray();//调用toCharArray()来获取字符数组
        System.out.println(chars);

    }
}

文字列操作

StringReader:文字ストリームへのソース文字列
にStringWriter:文字ストリームは、その出力を用いて構成することができるには、文字列バッファに文字列を回復しました。閉じるにStringWriterは無効。このクラスのメソッドは、まだいかなる例外なく、流れの閉鎖後に呼び出すことができIO

public class MyTest4 {
    public static void main(String[] args) {
        StringWriter stringWriter = new StringWriter();
        stringWriter.write("abc");
        stringWriter.write("abc");
        stringWriter.write("abc");
        stringWriter.write("abc");
        stringWriter.write("abc");
        String s = stringWriter.toString();
    }
}

ByteArrayOutputStream /するByteArrayInputStream

するByteArrayInputStream:ストリームから読み込まれたバイトが含まれている内部バッファが含まれています。追跡内部カウンタreadのバイトを提供するための方法を。閉じるれるByteArrayInputStreamは無効。ストリームが閉じられた後に、このクラスのメソッドは、まだ例外IOなしに、呼び出すことができます。

ByteArrayOutputStream:データをバイト配列に書き込まれたこのクラスの実装出力ストリーム。バッファを使用して自動的にデータの増加を書き込みます。使用toByteArray()toString()をデータを取得します。閉じるByteArrayOutputStreamは無効。ストリームが閉じられた後に、このクラスのメソッドは、まだどのIOExceptionをせずに、呼び出すことができます。

ケースプレゼンテーション:複数のmp3ファイルを一緒にmp3ファイルに

public class MyTest {
    public static void main(String[] args) throws IOException {
        FileInputStream in1 = new FileInputStream("许巍 - 曾经的你.mp3");
        FileInputStream in2 = new FileInputStream("许巍 - 蓝莲花.mp3");
        FileOutputStream out = new FileOutputStream("C:\\Users\\jjh\\Desktop\\歌曲大连唱.mp3");
        ArrayList<FileInputStream> list = new ArrayList<>();//创建集合
        list.add(in1);
        list.add(in2);//把两手哥存入集合
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        int len=0;
        byte[] bytes = new byte[1024 * 8];
        //读取两首歌的字节数据,一块先缓存到内存操作流所维护的字节数组中
        for (FileInputStream in : list) {
            while ((len = in.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            in.close();
        }
        //取出两首歌的字节数据
        byte[] allBytes = bos.toByteArray();

        //读取两首歌的字节数据,往硬盘上写
        ByteArrayInputStream bis = new ByteArrayInputStream(allBytes);
        int len2 = 0;
        byte[] bytes2 = new byte[1024 * 8];
        while ((len2=bis.read(bytes2))!=-1){
            out.write(bytes2,0,len2);
            out.flush();
        }
        out.close();
        System.out.println("合并完成");
    }
}

印刷ストリーム

彼は唯一の目的地ではなく、関連するソースファイルと関連します。これは、出力のみを読み取ることができないことができます。

バイトストリーム印刷

PrintStream(File file)指定:行の自動フラッシュは行わずに、指定された出力ストリームを持つ新しいファイルを作成します。

PrintStream型(文字列fileNameが):行の自動フラッシュは行わずに、指定したファイル名で新しい出力ストリームを作成します

ケースプレゼンテーション:

public class MyTest {
    public static void main(String[] args) throws IOException {
        PrintStream printStream = new PrintStream(new File("b.txt"));
        //通过创建得来的字节打印流,关联的是文件,那么你是往文件中打印数据
        printStream.write("曾梦想仗剑走天涯,看一看世界的繁华".getBytes());
        printStream.write("\r\n".getBytes());
        printStream.println("曾梦想仗剑走天涯,看一看世界的繁华");
        printStream.close();

        //System.out 获取出的这个字节打印流,关联的设备是屏幕
        PrintStream out = System.out;  //关联屏幕 out标准”输出流。此流已打开并准备接受输出数据。通常,此流对应于显示器
        out.write("abc".getBytes());
        out.print(20000);
        out.println(300000);
        System.out.println();
        out.close();

    }
}

文字印刷の流れ

PrintWriterの文字印刷の流れ

PrintWriter(OutputStreamのうち、ブール自動フラッシュ) : 既存のOutputStreamを通じて新しいPrintWriterを作成します。オートリフレッシュが有効になっている場合は呼び出したときにのみのprintlnprintf関数、またはフォーマットだけ一つのアプローチをこれを行う可能性があります。パラメータ2は、あなたが自動更新を有効にするかどうか

ケースプレゼンテーション1:

public class MyTest2 {
    public static void main(String[] args) throws FileNotFoundException {
        PrintWriter printWriter = new PrintWriter("c.txt");
        printWriter.write("abc");
        printWriter.print(100);
       
        printWriter.flush();
        printWriter.close();

    }
}

ケースプレゼンテーション2:

public class MyTest3 {
    public static void main(String[] args) throws FileNotFoundException {
     // PrintWriter(OutputStream out, boolean autoFlush)
      //  通过现有的 OutputStream 创建新的 PrintWriter。

    PrintWriter printWriter = new PrintWriter(new FileOutputStream("d.txt"), true);
        //printWriter.write("abc");//因为没有调上面说的三种方法,不手动刷新的话就不显示
        printWriter.println("abc");
        printWriter.flush();
        printWriter.close();
    }
}

ケース・プレゼンテーション3:テキストファイルの印刷ストリームをコピーします。

public class MyTest4 {
    public static void main(String[] args) throws IOException {
        BufferedReader bfr = new BufferedReader(new FileReader("MyTest.java"));
        PrintWriter printWriter = new PrintWriter(newFileOutputStream("MyTest222.java"),true);
        String line=null;
        while ((line=bfr.readLine())!=null){
            printWriter.println(line);//调用了println所以我们不需要手动刷新
        }
        bfr.close();
        printWriter.close();
    }
}

ランダム・アクセス・ストリーム

このクラスのサポートのインスタンスは、ランダムアクセスファイルへの読み書き。ファイルシステムに格納されている大規模なバイト配列に似たランダムアクセスファイルを行っています。暗黙的な配列の存在を指し示すカーソル又はインデックスと呼ばれる、ファイルポインタ、入力操作は、ファイルポインタからバイトを読み取り、予めとしてバイトのファイルポインタの読み取りを開始します。ランダムアクセスファイルが読み取り/書き込みモードで作成された場合、出力動作を使用してもよい;出力動作は、ファイルポインタからバイトの書き込みを開始し、バイトライトとして、ファイルポインタを進めます。拡張の配列結果の現在の出力終了後、アレイ書込み動作を暗示。ファイルによってポインタgetFilePointerメソッドを読み、そしてによって求める方法を設定します。

RandomAccessFile(ファイルのファイル、文字列モード) : 読み込み元と(オプション)ストリーム、ファイルパラメータで指定されたファイルへの書き込みへのランダムアクセスファイルを作成します。

モードパラメータは、オープンアクセスモードにファイルを指定します。許可された値とその意味は以下のとおりです。

「R」:読み取り専用として開きます。呼び出しオブジェクトの任意の書き込み結果はIOExceptionがになります。

「RW」:読み取りおよび書き込み用にオープン。ファイルが存在しない場合は、ファイルを作成してみてください。

「RWS」:読み込みと書き込みのためにオープン、「RW」については、各更新内容を必要とするか、またはメタデータファイルは、元になるストレージデバイスと同時に書き込まれています。

「RWD」:読み込みと書き込みのためにオープン、「RW」については、また元になるストレージデバイスと同時に書き込まれ、各アップデートファイルの内容を必要とします。

ケース1:ランダム・アクセス・ストリームで歌を置く3つのコピー

public class MyTest3 {
    public static void main(String[] args) throws IOException {
        RandomAccessFile in = new RandomAccessFile("许巍 - 曾经的你.mp3", "rw");
        RandomAccessFile out = null;
        byte[] bytes = new byte[1024];
        int len = 0;
        for (int i = 0; i < 3; i++) {
            out = new RandomAccessFile((i + 10) + "许巍 - 曾经的你.mp3", "rw");
            while ((len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
            }
            in.seek(0);//把文件指针置于文件的开头
            out.close();
        }
        in.close();
    }
}

ケース2:コピー曲のブレークポイント

public class MyTest4 {
    public static void main(String[] args) throws IOException {
        RandomAccessFile in = new RandomAccessFile("许巍 - 蓝莲花.mp3", "rw");
        File mbFile = new File("C:\\Users\jjh\\Desktop\\许巍 - 蓝莲花22222.mp3");
        RandomAccessFile out = new RandomAccessFile(mbFile, "rw");

        //第二次来复制了
        String s = new BufferedReader(new FileReader("ls.txt")).readLine();
       //读取临时文件
        long len2 = Long.parseLong(s);//获取临时文件的长度(向下转型)
        if(mbFile.exists()&&mbFile.length()==len2){
        //如果临时文件存在而且目标文件的长度和临时文件相等的话继续执行
            in.seek(len2);//把指针位置置于len2这样就可以断点下载
            out.seek(len2);
        }else{
            in.seek(0);//设置文件指针的位置
            out.seek(0);
        }


        int len=0;
        byte[] bytes = new byte[1024];
        int i=1;
        try {
            while ((len = in.read(bytes)) != -1) {
                //i++;
                out.write(bytes, 0, len);
              /*  if(i>=2500){
                    System.out.println(1/0);
                }*/   //这是自己制造的异常情况让复制终止

            }
        }catch (Exception e){
            long filePointer = in.getFilePointer(); //获取文件指针的位置
            System.out.println(filePointer);
            PrintWriter printWriter = new PrintWriter(new File("ls.txt"));
            //创建一个临时文件来记录文件复制的字节数
            printWriter.println(filePointer);
            printWriter.flush();
            printWriter.close();
        }

        in.close();
        out.close();
    }
}

他のシリアル入力ストリーム(ストリーム順序)

たSequenceInputStreamは:他の入力ストリームの論理的なシリーズを表します。また、入力ストリームから読み込まれたファイルの最後まで、最初の入力ストリームから開始順序付きコレクションで、最後の最後にファイル入力ストリームを含むようになるまで、2番目の入力ストリームを読み込み、など迄

たSequenceInputStream(入力ストリームS1、入力ストリームS2) :たSequenceInputStreamから読み出さ提供するために、(これらの2つのパラメータを順次、最初の読み出しS1を読み、次にS2を読み取るする)新しく作成されたSequenceInputStreamを初期化するために、これらの2つのパラメータを記憶することによってバイト

ケースプレゼンテーション:ストリーム3曲の順番は、曲を構成します

public class MyTest {
    public static void main(String[] args) throws IOException {
        FileInputStream in1 = new FileInputStream("许巍 - 曾经的你.mp3");
        FileInputStream in2 = new FileInputStream("许巍 - 蓝莲花.mp3");
        FileInputStream in3 = new FileInputStream("许巍 - 蓝莲花.mp3");
        FileOutputStream out = new FileOutputStream("gequ.mp3");//输出
        
        SequenceInputStream sequenceInputStream = new SequenceInputStream(in1, in2);
        SequenceInputStream sequenceInputStream1 = new SequenceInputStream(sequenceInputStream, in3);
        int len=0;
        byte[] bytes = new byte[1024 * 8];
        while ((len=sequenceInputStream1.read(bytes))!=-1){
            out.write(bytes,0,len);
            out.flush();
        }
        out.close();
        sequenceInputStream1.close();
    }
}

プロパティ

プロパティの継承ハッシュテーブルは、複列の設定された所定のキーは、文字列型

ケースプレゼンテーション1:

public class MyTest {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("username", "张三");
        properties.setProperty("password", "123456");
        String username = properties.getProperty("username");
        //当这个键对应的值得没找到,可以返回这个备用值,参数2 可以指定一个备用的值
        String pwd = properties.getProperty("password","654321");
        System.out.println(username);
        System.out.println(pwd);
    }
}

ケースプレゼンテーション2:設定ファイルの書き込みに格納されたデータのキー属性のコレクション(店舗())

public class MyTest2 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.setProperty("username", "张三");
        properties.setProperty("password", "123456");
        //参数2:是注释 一般给个null 写个默认注释
        properties.store(new FileOutputStream("user.properties"),"hehehe");
        
        

ケースプレゼンテーション3:キーデータのプロファイルは、プロパティが再設定されます。

public class MyTest3 {
    public static void main(String[] args) throws IOException {
        //Properties 读取配置文件,对文件有要求,1.键值对,是 = 拼接 2.属性集合读取到的这个文件的后缀名是以.properties
        Properties properties = new Properties();
        //读取配置文件
        properties.load(new FileInputStream("user.properties"));
        System.out.println(properties);

    }
}

ケースプレゼンテーション4:

public class MyTest04 {
/*我有一个文本文件file.txt,我知道数据是键值对形式的,但是不知道内容是什么。
  请写一个程序判断是否有"lisi"这样的键存在,如果有就改变其实为"100"
  file.txt文件内容如下:
zhangsan = 90
lisi = 80
wangwu = 85*/
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("file.txt"));
        //先读写这个文件
        Set<String> names = properties.stringPropertyNames();
         //stringPropertyNames()
        //返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。其键或值不是 String 类型的属性被忽略
        if (names.contains("lisi")){
          properties.setProperty("lisi","100");
          properties.store(new FileOutputStream("d.txt"),null);
            //最后写入保存
            System.out.println(properties);
        }

おすすめ

転載: www.cnblogs.com/godles/p/11892729.html