Day20作业

Day20作业,第1张

文章目录
    • A:简答题
    • B:看程序写结果(写出自己的分析理由),程序填空,改错,看程序写结果。
    • C:编程题

A:简答题

1、请把今天讲解的方法,在API中查找到,并使用自己的话进行描述

FileInputStream
FileInputStream(File file) 
通过一个文件创建一个字节输入流
FileInputStream(String name) 
通过一个路径创建一个字节输入流
int read() 
从这个输入流读取一个字节的数据  
int read(byte[] b) 
读到b.length从输入流到字节数组数据字节  
int read(byte[] b, int off, int len) 
读到len从输入流到字节数组数据字节  
void close() 
关闭流

FileOutputStream
FileOutputStream(File file) 
通过一个文件创建一个字节输出流  
FileOutputStream(File file, boolean append) 
通过一个文件创建一个字节输出流 
并判断是追加(true)还是覆盖(false)文件夹
void write(int b) 
将指定的字节写入该文件输出流中
void write(byte[] b) 
写b.length字节从指定的字节数组来此文件输出流  
void write(byte[] b, int off, int len) 
写len字节指定字节数组中的起始偏移off此文件输出流
void close() 
关闭此文件输出流并释放与此流关联的任何系统资源 

BufferedInputStream
BufferedInputStream(InputStream in) 
通过与其他字节输入流连用 创建BufferedInputStream  
BufferedInputStream(InputStream in, int size) 
通过与其他字节输入流连用
创建一个具有指定的缓冲区大小的BufferedInputStream
int read() 
与InputStream的read方法一样。  
int read(byte[] b, int off, int len) 
从这个字节的输入流读取到指定的字节数组中的字节从给定的偏移量开始 

BufferedOutputStream
BufferedOutputStream(OutputStream out) 
创建一个新的缓冲输出流 将数据写入到指定的基本输出流中  
BufferedOutputStream(OutputStream out, int size) 
创建一个新的缓冲输出流 用指定的缓冲区大小写数据到指定的基本输出流中  
void flush() 
刷新缓冲输出流。  
void write(byte[] b, int off, int len) 
写 len字节指定字节数组中的起始偏移 off这个缓冲输出流。  
void write(int b) 
将指定的字节写入该缓冲输出流中。  

2、请简述IO流的分类

按照数据流向
	输入流 读入数据
	输出流 写出数据
按照数据类型
	字节流 可以读写任意类型的文件(音频 视频 文本文件)
	字符流 只能读写文本文件
	
	  字节流	      字符流
输入流 InPutStream   Reader
输出流	OutPutStream  Writer
B:看程序写结果(写出自己的分析理由),程序填空,改错,看程序写结果。

1、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因

class Test {
    public static void main(String[] args) {
        try {
            write();
            read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write() throws IOException {
        FileOutputStream fos = new FileOutputStream("fos.txt");
        fos.write("Hello");
        fos.write("Java");
        //write方法不能写String类型的 只能是byte[] int
		//fos.write("Hello".getBytes());
		//fos.write("Java".getBytes());
        fos.close();
    }

    public static void read() throws IOException {
        BufferedInputStream bis = new BufferedInputStream("fos.txt");
        //BufferedInputStream是一个缓冲流 需要和FileInputStream搭配使用
		//BufferedInputStream bis = new BufferedInputStream(new FileInputStream("fos.txt"));
        byte[] buffer = new byte[5];
        int len = 0;
        while ((len = bis.read(buffer)) != -1) {
            System.out.print(new String(buffer, 0, len));
        }
        bis.close();
    }
}
C:编程题

1、编写程序,获取指定目录下 所有的.java结尾的文件, 打印出文件的绝对路径(包含 子文件夹中的.java文件名)

public class Homework01 {
    public static void main(String[] args){
        //文件指定的目录
        File file = new File("E:\IO");
        printFileName(file);
    }

    private static void printFileName(File file) {
        //获取目录中的文件
        File[] files = file.listFiles();
        for (File f : files) {
            //如果是文件并且是.java结尾 就输出其绝对路径
            if (f.isFile() && f.getName().endsWith(".java")) {
                System.out.println(f.getAbsolutePath());
            }
            //如果是文件夹 就递归查找
            if (f.isDirectory()){
                printFileName(f);
            }
        }
    }
}

2、编写程序,删除 指定目录下的 所有文件与文件夹 (包含子文件夹)

public class Homework02 {
    public static void main(String[] args) {
        //删除指定的多级目录
        File file = new File("E:\IO");
        deleteDir(file);
    }

    private static void deleteDir(File file) {
        //获取目录中的文件
        File[] files = file.listFiles();
        for (File f : files) {
            //如果是文件 直接删除
            if (f.isFile()) {
                f.delete();
            } else {
                //如果是子目录 就递归
                deleteDir(f);
                //删除子目录
                f.delete();
            }
        }
        //删除最外层目录
        file.delete();
    }
}

3、编写程序,采用多种方式,把d:\video01.avi的内容复制到d:\video02.avi中

方式1:基本字节流一次读写一个字节

方式2:基本字节流一次读写一个字节数组

方式3:高效字节流一次读写一个字节

方式4:高效字节流一次读写一个字节数组

public class Homework03 {
    public static void main(String[] args) throws IOException {
        //copy01();
        //copy02();
        //copy03();
        //copy04();
    }

    //基本字节流一次读写一个字节
    private static void copy01() throws IOException {
        FileInputStream fis = new FileInputStream("E:\video01.avi");
        FileOutputStream fos = new FileOutputStream("E:\video02.avi");
        int read = 0;
        while ((read = fis.read()) != -1) {
            fos.write(read);
        }
        fis.close();
        fos.close();
    }

    //基本字节流一次读写一个字节数组
    private static void copy02() throws IOException {
        FileInputStream fis = new FileInputStream("E:\video01.avi");
        FileOutputStream fos = new FileOutputStream("E:\video02.avi");
        byte[] bytes = new byte[1024 * 8];
        int readLen = 0;
        while ((readLen = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, readLen);
        }
        fis.close();
        fos.close();
    }

    //高效字节流一次读写一个字节
    private static void copy03() throws IOException {

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\video01.avi"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\video02.avi"));
        int read = 0;
        while ((read = bis.read()) != -1) {
            bos.write(read);
        }
        bis.close();
        bos.close();
    }

    //高效字节流一次读写一个字节数组
    private static void copy04() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\video01.avi"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\video02.avi"));
        byte[] bytes = new byte[1024 * 8];
        int readLen = 0;
        while ((readLen = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, readLen);
        }
        bis.close();
        bos.close();
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1499252.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-25
下一篇 2022-06-25

发表评论

登录后才能评论

评论列表(0条)

保存