A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© liweihao5253 初级黑马   /  2019-8-1 17:29  /  808 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

字节流

节点流FileInputStream的使用
构造方法:
FileInputStream fis = new FileInputStream("文件目录");
FileInputStream fis = new FileInputStream(文件对象);

常用方法
int fis.read() 读取一个字节的数据,并返回int类型,如果返回-1表示文件读完了
int fis.read(byte[] bys) 将读取的内容装进字节数组内,字节数组要提前初始化,一般设置为1024的整数倍,返回的是当次读取到的字节数

节点流FileOutputStream的使用
构造方法:
同上

常用方法
void fos.write(int b)  将指定字节b写入文件
void fos.write(byte[] bys) 将字节数组写入文件
void fos.write(byte[] bys, int off, int len)  将字节数组部分写入文件,并设置偏移量和长度,本方法常用!
文件拷贝

public static void copy(File src,File dest) throws IOException {
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);
        byte[] bys = new byte[1024]; //自定义缓冲区
        int len = 0;
        while((len = fis.read(bys))!=-1) {
            fos.write(bys,0,len);
        }
        fos.close();
        fis.close();
    }


套接流BufferedInputStream的使用
带缓冲区的字节流
构造法方法:
BufferedInputStream bis = new BufferedInputStream(InputStream的实现对象);
BufferedInputStream bis = new BufferedInputStream(InputStream的实现对象, int size); //自定义缓冲区大小,默认是8192字节8KB

常用方法:
使用方法同FileInputStream,但是由于自带缓冲区,读写效率极大提高,建议使用带缓冲区的流
套接方式:
该类可套接FileInputStream

套接流BufferedOutputStream的使用
构造方法
BufferedOutputStream bos = new BufferedOutputStream(InputStream的实现对象);
BufferedOutputStream bos = new BufferedOutputStream(InputStream的实现对象, int size); //自定义缓冲区大小,默认是8192字节8KB
常用方法:
使用方法同FileOutStream,但是由于自带缓冲区,读写效率极大提高,建议使用带缓冲区的流
该类可套接FileOutputStream

BufferedOutputStream和自定义缓冲区的FileOutputStream区别是
BufferedOutputStream在缓冲区不满的情况下不会写入操作,需要flush()方法

文件拷贝四种方法

public class Test {
    public static void main(String[] args) throws IOException{
        File srcFile = new File("C:\\Users\\liweihao\\Documents\\就业班课程15\\day9\\视频\\15_IO流小结.mp4");
        File destFile = new File("myLesson\\qq.avi");
        long beginTime = System.currentTimeMillis();
        copy_02(srcFile,destFile);
        long endTime = System.currentTimeMillis();
        System.out.println("一共耗时:"+(endTime-beginTime));
    }
    public static void copy_01(File srcFile, File destFile) throws IOException {
        //method 1
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        int byt;
        while((byt=fis.read())!=-1) {
            fos.write(byt);
        }
        fis.close();
        fos.close();
    }
    public static void copy_02(File srcFile, File destFile) throws IOException {
        //method 1
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        int len;
        byte[] byts = new byte[8192];
        while((len=fis.read(byts))!=-1) {
            fos.write(byts,0,len);
        }
        fis.close();
        fos.close();
    }
    public static void copy_03(File srcFile, File destFile) throws IOException {
        //method 1
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int bys;
        while((bys=bis.read())!=-1) {
            bos.write(bys);
        }
        bis.close();
        bos.close();
    }
    public static void copy_04(File srcFile, File destFile) throws IOException {
        //method 1
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int len;
        byte[] byts = new byte[1024];
        while((len=bis.read(byts))!=-1) {
            bos.write(byts,0,len);
        }
        bis.close();
        bos.close();
    }
}

字符流

套接流InputStreamReader
构造方法
InputStreamReader isr = new InputStreamReader(InputStream的实现对象);
InputStreamReader isr = new InputStreamReader(InputStream的实现对象,String charset); //指定编码方式,重要
套接方式:
该类可套接FileInputStream

套接流OutputStreamWriter
构造方法
OutStream不写了,。同上
该类可套接FileOutputStream

套接流FileReader和FileWriter
是InputStreamReader和OutputStreamWriter的继承类,本身不提供新方法,但是其构造函数可以直接传文件或文件目录,方便初始化流,一般作为中间件使用
构造方法:

套接流BufferedReader
字符缓冲流,不仅自带缓冲区,还提供了一些特有的方法
构造方法,传Reader类的对象,一般使用FileReader
BufferedReader br = new BufferedReader(FileReader("文件路径"));
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputstream("文件路径")));
特有方法:readLine( ) //读一行,但不包括换行符

套接流BufferedWriter
字符缓冲流,不仅自带缓冲区,还提供了一些特有的方法
构造方法,同上
特有方法:
write(String s) //可以直接写一行字符串,如果要换行则需要追加\r\n,但是不同操作系统换行符不一样,因此使用下面的方法更好
newLine() //根据不同的操作系统产生对应的换行符


public class BufferedCopyDemo {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("myLesson\\f.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("myLesson\\f1.txt"));
        String str;
        while((str = br.readLine())!=null) { //注意,读到文件末尾会返回null而不是-1
            bw.write(str);
            bw.newLine();
            bw.flush();
        }
        br.close();
        bw.close();
    }
}

编码解码问题
字符串->字节数组  编码过程
字节数组->字符串  解码过程

public static void main(String[] args) throws UnsupportedEncodingException {
        String s = "中国";
        byte[] bys = s.getBytes("GBK");//指定编码字符集
        System.out.println(Arrays.toString(bys));
        String s2 = new String(bys,"GBK");//指定解码字符集
        System.out.println(s2);
    }

流的编码和解码问题
只能用字符流来处理编码问题
只有InputStreamReader/OutputStreamWriter 类可以处理,在调用构造函数时传入字符集名称作为第二参数
   
注意:所有自带缓冲区的流,在缓冲区不满/不空的时候是不会进行硬盘的访问操作的
标准输入流
常用的System.in本质上是InputStream,是一个字节流,可以外套字符流处理
可以手动使用套节后的字符流处理一行输入信息,不过Scanner类为我们封装好了这样的方法,使用的时候直接new Scanner(System.in)即可

标准输出流
PrintStream 打印流
构造方法:传入OutputStream的实现类
方法:
除了基本的write方法外,还有各种print方法,可以按数据实际类型打印

PrintWriter 字符打印流
构造方法:传入Writer的实现类
特点:可以在构造的时候指定自动刷新,再结合println方法,可以替代传统的write三连
bw.write("");
bw.newLine();
bw.flush();

特有方法:println() //直接输出一行,还会自动刷新(需要构造函数指定)



复制文件


public static void main(String[] args) throws IOException {
    File srcFile = new File("C:\\Users\\liweihao\\Pictures\\wallhaven-673719.jpg");
    File destFile = new File("myWork\\pic.jpg");
    FileInputStream fis = new FileInputStream(srcFile);
    FileOutputStream fos = new FileOutputStream(destFile);
    byte[] buffer = new byte[1024];
    int len;
    while((len=fis.read(buffer))!=-1) {
        fos.write(buffer,0,len);
    }
    fos.close();
    fis.close();
}

递归遍历文件夹中所有文件


public class ShowDirectory {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\liweihao\\Documents\\就业班课程15");
        show(file);
    }
    public static void show(File file) {
        File[] files = file.listFiles();
        for(File f:files) {
            if(f.isDirectory()) {
                show(f);
            }else if(f.isFile()) {
                System.out.println(f.getAbsolutePath());
            }
        }
    }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马