read方法+read和write一起用

版权声明:正在学习中,有错误欢迎指出。感谢比❤❤~ https://blog.csdn.net/gx17864373822/article/details/79958343

read()方法1:一个字节一个字节的读

import java.io.*;
public class Main {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("c:\\gx.txt");
        long l1 = System.currentTimeMillis();
        //1:一个字节一个字节的读
        /*int result = is.read();//返回读取的字符的编码,当返回值为-1,结束,关闭输入流
        while(result != -1){
            System.out.println(result);
            result = is.read();
        }*/

        int result;//返回读取的字符的编码,当返回值为-1,结束,关闭输入流
        while((result = is.read()) != -1){
            System.out.println(result);
        }
        is.close();

        //2
        long l2 = System.currentTimeMillis();
        System.out.println(l2-l1);
    }

}

read(byte [])方法2:一个字节数组一个字节数组的读

import java.io.*;
import java.util.Arrays;
public class Main {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("c:\\gx.txt");
        long l1 = System.currentTimeMillis();

        //2一个字节数组一个字节数组的读
        /*byte[] bytes = new byte[10];
        int len = is.read(bytes);//一次实际读取字节的数量
        while(len != -1){
            System.out.println(Arrays.toString(bytes));
            len = is.read(bytes);
        }*/

        byte[] bytes = new byte[10];

        int len = 0;
        while((len = is.read(bytes)) != -1){
            System.out.println(Arrays.toString(bytes));
        }

        long l2 = System.currentTimeMillis();
        System.out.println(l2-l1);
    }

}

输出结果:
[97, 102, 115, 106, 104, 118, 108, 107, 106, 97]
[98, 115, 105, 107, 97, 100, 102, 106, 108, 32]
[120, 122, 105, 107, 97, 59, 112, 59, 97, 115]
[107, 108, 100, 43, 56, 53, 54, 13, 10, 50]
[51, 56, 52, 53, 54, 52, 100, 115, 106, 108]
[83, -60, -38, -76, -26, 118, 107, 122, 67, 104]
[106, 118, 98, 32, 101, 46, 59, 119, 105, 103]
[98, 59, 113, 119, 50, 58, 116, 113, 104, 105]
[111, 59, 113, 119, 50, 58, 116, 113, 104, 105]
0

最后两行:
[98, 59, 113, 119, 50, 58, 116, 113, 104, 105]
[111, 59, 113, 119, 50, 58, 116, 113, 104, 105]
可以知道,最后一次读进去了8个字符(覆盖前8个),最后两个是上一次遗留的,并未覆盖

从一个文件中读取数据写到另一个文件中:

import java.io.*;
import java.util.Arrays;
public class Main {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("c:\\gx.txt");//从gx.txt文件中读数据。输入流
        OutputStream os = new FileOutputStream("c:\\aa.txt");//向aa.txt文件中写数据。输出流
        long l1 = System.currentTimeMillis();

        byte[] bytes = new byte[10];//一次读这些,一次也写这些

        int len = 0;
        while((len = is.read(bytes)) != -1){
            System.out.println(Arrays.toString(bytes));
//          os.write(bytes);//将一次读的10个写入待写文件.这种方法不可以,可能会多写
            os.write(bytes, 0, len);
        }

        long l2 = System.currentTimeMillis();
        System.out.println(l2-l1);
    }

}

把一个网站的网址当作输入流:

import java.io.*;
import java.net.URL;
import java.util.Arrays;
public class Main {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {

        URL url = new URL("http://www.sdut.edu.cn");//输入流的来源:从一个网址
        InputStream is = url.openStream();

        OutputStream os = new FileOutputStream("c:\\index.html");//输出流的去向
        long l1 = System.currentTimeMillis();

        byte[] bytes = new byte[10];//一次读这些,一次也写这些

        int len = 0;
        while((len = is.read(bytes)) != -1){
            System.out.println(Arrays.toString(bytes));
//          os.write(bytes);//将一次读的10个写入待写文件.这种方法不可以,可能会多写
            os.write(bytes, 0, len);
        }

        long l2 = System.currentTimeMillis();
        System.out.println(l2-l1);
    }

}

不过只有结构,没有样式

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页