java基础之IO 节点流 缓冲过滤流 练习题

1:Java 中根据流的方向,流分为 输入流     输出流 ;对应的操作分别为    和    写

2:Java 中字节流的父类为   InputStream      OutputStream ;以上 2个类都位于   java.io         包中

3:利用FileOutputStream(String path)创建对象时,如果对应的文件在硬盘上不存在,则会 在硬盘上创建文件, 如果对应的文件存在,则 覆盖 (覆盖 | 不覆盖)原有内容,参数代表 文件路径; 利用 有参 构造方法创建FileOutputStream 对象,将第二个参数设置为 true不会覆盖原有内容。

4:FileInputStream 有三个重载的read 方法,完成以下填空

        (1)无参数的read()方法返回值为 int  类型,表示 返回获取值的哈希码

        (2)有参数的read(byte[] bs)方法的返回值表示 返回实际读取个数 ,参数表示 读取的值哈希码存入数组中

5:下面关于FileInputStream 说法正确的是()

  1. 创建FileInputStream 对象是为了读取文件
  2. 创建FileInputStream 对象时,如果硬盘上对应的文件不存在,则抛出一个异常
  3. 创建FileInputStream 对象可以创建对应的文件
  4. FileInputStream 对象读取文件时,只能读取文本文件

 

6:  以下几种文件格式,应当使用字节流还是字符流?

A. java 源文件  字符流 B. .class 字节码文件   字节流

C. .html 网页文件   字符流 D. .jpg 图像文件   字节流

E. .mp3 音乐文件   字节流 F. .txt 文件   字符流

7:仔细阅读以下代码,将程序中不恰当的地方进行改正。 

try(FileInputStream fin=new FileInputStream("test.txt")){
    System.out.println(fin.read());
}catch (Exception e){
    
}

8:编程:利用 FileInputStream 和FileOutputStream,完成下面的要求 ​​​​​​

        (1) 用FileOutputStream 往当前目录下“test.txt”文件中写入“Hello World”;

        (2)利用FileInputStream 读入test.txt 文件,并在控制台上打印出test.txt 中的内容。

        (3)要求用 try-catch-finally 处理异常,并且关闭流应放在 finally 块中。

package com.by.homework7;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import javax.imageio.IIOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLOutput;

public class Test7 {
    public static void main(String[] args) {
        FileOutputStream fo=null;
        FileInputStream fi=null;
       try{
           fo=new FileOutputStream("file/test.txt");
           fi=new FileInputStream("file/test.txt");
            String str="Hello World";
            fo.write(str.getBytes());
            fo.flush();
            while(true) {
                int n=fi.read();
                if (n==-1){
                    break;
                }
                System.out.println(n);
            }

       }catch (FileNotFoundException e){
           System.out.println("文件路径有误");
       }catch (IOException e){
           System.out.println("文件读写有误");
       }catch (Exception e){
           System.out.println("未知错误");
       }finally {
           if (fi!=null) {
               try {
                   fi.close();
               } catch (IOException e) {
                   throw new RuntimeException(e);
               }
           }
           if (fo!=null){
               try {
                   fo.close();
               } catch (IOException e) {
                   throw new RuntimeException(e);
               }
           }

       }
    }
}

9:编程:利用 IO 流,完成以下程序

        (1)将 26 个大写字母(A~Z)写入到当前项目a.txt 文件中

        (2) 读取文件中的内容,将读取的内容连接为一个字符串,并将所有的大写字母转变为小写字母打印输出转换的结果

package com.by.homework7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test8 {
    public static void main(String[] args) {
        FileOutputStream fo=null;
        FileInputStream fi=null;
        try{
            fo=new FileOutputStream("file/a.txt");
            fi=new FileInputStream("file/a.txt");
            String str="ABCDEFGHIGKLMNOPQRSTUVWXYZ";
            fo.write(str.getBytes());
            fo.flush();
            while(true) {
                int n=fi.read();
                if (n==-1){
                    break;
                }
                char c= (char) n;
                String s= String.valueOf(c);
                System.out.print(s.toLowerCase());
            }

        }catch (FileNotFoundException e){
            System.out.println("文件路径有误");
        }catch (IOException e){
            System.out.println("文件读写有误");
        }catch (Exception e){
            System.out.println("未知错误");
        }finally {
            if (fi!=null) {
                try {
                    fi.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fo!=null){
                try {
                    fo.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }
}

"C:\Program Files\Java\jdk1.8.0_341\bin\java.exe" "-javaagent:C:\Program com.by.homework7.Test8

abcdefghigklmnopqrstuvwxyz

10:编程:当前项目的根目录 c.txt 文件中的内容为”abddbskshlsjdhhhiw”;编写程序读取文件中的内容,要求去除重复的字母并按照字母的自然排序后将内容写入到当前项目的根目录 d.txt 文件中。

package com.by.homework7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class Test10 {
    public static void main(String[] args) {
        Set<String> set=new HashSet<>();
        Set<String> set1=new TreeSet<>();
        try(FileOutputStream fo=new FileOutputStream("file/d.txt");
                FileInputStream fi=new FileInputStream("file/c.txt")
        ){
             while (true){
                 int n=fi.read();
                 if (n==-1){
                     break;
                 }
                 char c= (char) n;
                 String str= String.valueOf(c);
                 set.add(str);
             }
            set1.addAll(set);

            String str2= set1.toString();
            /*for (String s:set1
                 ) {
                bytes=s.getBytes();
            }*/
            byte[] bytes = str2.getBytes();
            fo.write(bytes);

        }catch (FileNotFoundException e){
            System.out.println("文件路径有误");
        }catch (IOException e){
            System.out.println("文件读写有误");
        }catch (Exception e){
            System.out.println("未知错误");
        }
//        set.forEach(string-> System.out.println(string));
//        set1.addAll(set);
        set1.forEach(string-> System.out.print(string));
    }
}

11:使用字节节点流将 a~z 26个小写字母写入到“file\a.txt”文件中。

package com.by.homework7;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test11 {
    public static void main(String[] args) {

        try(FileOutputStream fo=new FileOutputStream("file/a1.txt");){
            String str="abcdefghijklmnopqrstuvwxyz";
            byte[] bs=str.getBytes();
            fo.write(bs);
        }catch (FileNotFoundException e){
            System.out.println("文件路径有误");
        }catch (IOException e){
            System.out.println("文件读写有误");
        }catch (Exception e){
            System.out.println("未知错误");
        }
    }
}

12:使用字节节点流将第11题生成的文件内容读入到内存。

package com.by.homework7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test11 {
    public static void main(String[] args) {

        try(FileOutputStream fo=new FileOutputStream("file/a1.txt");
        FileInputStream fi=new FileInputStream("file/a1.txt");){
            String str="abcdefghijklmnopqrstuvwxyz";
            byte[] bs=str.getBytes();
            fo.write(bs);
            fo.flush();
            while (true){
                int n=fi.read();
                if (n==-1){
                    break;
                }
                System.out.println(n);
            }

        }catch (FileNotFoundException e){
            System.out.println("文件路径有误");
        }catch (IOException e){
            System.out.println("文件读写有误");
        }catch (Exception e){
            System.out.println("未知错误");
        }
    }
}

13:使用字节节点流将第11题生成的文件进行拷贝备份“file\b.txt”。

 package com.by.homework7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test13 {
    public static void main(String[] args) {

        try(FileOutputStream fo=new FileOutputStream("file/b1.txt");
            FileInputStream fi=new FileInputStream("file/a1.txt");){
/*            String str="abcdefghijklmnopqrstuvwxyz";
            byte[] bs=str.getBytes();
            fo.write(bs);
            fo.flush();*/
            while (true){
                int n=fi.read();
                if (n==-1){
                    break;
                }
                System.out.println(n);
                fo.write(n);
            }
        }catch (FileNotFoundException e){
            System.out.println("文件路径有误");
        }catch (IOException e){
            System.out.println("文件读写有误");
        }catch (Exception e){
            System.out.println("未知错误");
        }
    }
}

14:对第11、12题和第13题进行标准异常处理。

package com.by.homework7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test14 {
    public static void main(String[] args) {
        FileOutputStream fo=null;
        FileInputStream fi=null;
        try{
            fo=new FileOutputStream("file/b1.txt");
            fi=new FileInputStream("file/a1.txt");
/*            String str="abcdefghijklmnopqrstuvwxyz";
            byte[] bs=str.getBytes();
            fo.write(bs);
            fo.flush();*/
            while (true){
                int n=fi.read();
                if (n==-1){
                    break;
                }
                System.out.println(n);
                fo.write(n);
            }
        }catch (FileNotFoundException e){
            System.out.println("文件路径有误");
        }catch (IOException e){
            System.out.println("文件读写有误");
        }catch (Exception e){
            System.out.println("未知错误");
        }finally {
            if (fi!=null){
                try {
                    fi.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fo!=null){
                try {
                    fo.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

15:使用字节流将第9题生成的文件读入到内存中

package com.by.homework7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test8 {
    public static void main(String[] args) {
        FileOutputStream fo=null;
        FileInputStream fi=null;
        try{
            fo=new FileOutputStream("file/a.txt");
            fi=new FileInputStream("file/a.txt");
            String str="ABCDEFGHIGKLMNOPQRSTUVWXYZ";
            fo.write(str.getBytes());
            fo.flush();
            while(true) {
                int n=fi.read();
                if (n==-1){
                    break;
                }
                char c= (char) n;
                String s= String.valueOf(c);
                System.out.print(s.toLowerCase());
              /*  int num=n+25;
                char c=(char)num;
                String s=String.valueOf(c);
                System.out.print(s);*/
            }

        }catch (FileNotFoundException e){
            System.out.println("文件路径有误");
        }catch (IOException e){
            System.out.println("文件读写有误");
        }catch (Exception e){
            System.out.println("未知错误");
        }finally {
            if (fi!=null) {
                try {
                    fi.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fo!=null){
                try {
                    fo.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }
}

16:使用流所学知识点将作业文件夹中图片“gril.jpg”复制到“file”文件下

package com.by.homework7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test16 {
    public static void main(String[] args) {
        try(
                FileOutputStream fo=new FileOutputStream("file/girl.jpg");
                FileInputStream fi=new FileInputStream("C:\\Users\\user\\Desktop\\java学习题\\IO1练习题-无对象过滤流/gril.jpg");
                ){
            while (true){
                int n=fi.read();
                if (n==-1){
                    break;
                }
                fo.write(n);
            }


        }catch (FileNotFoundException e){
            System.out.println("文件路径有误");
        }catch (IOException e){
            System.out.println("文件读写有误");
        }catch (Exception e){
            System.out.println("未知错误");
        }
    }
}

17:使用流所学知识点将作业文件夹中歌曲“Corporate.mp3”复制到“file”文件下

 package com.by.homework7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test17 {
    public static void main(String[] args) {
        try(
                FileOutputStream fo=new FileOutputStream("file/1.mp3");
                FileInputStream fi=new FileInputStream("C:\\Users\\user\\Desktop\\java学习题\\IO1练习题-无对象过滤流/Corporate.mp3");
        ){
            while (true){
                int n=fi.read();
                if (n==-1){
                    break;
                }
                fo.write(n);
            }


        }catch (FileNotFoundException e){
            System.out.println("文件路径有误");
        }catch (IOException e){
            System.out.println("文件读写有误");
        }catch (Exception e){
            System.out.println("未知错误");
        }
    }
}

18:为以上习题添加缓冲过滤流

package com.by.homework7;

import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class Test10 {
    public static void main(String[] args) {
        Set<String> set=new HashSet<>();
        Set<String> set1=new TreeSet<>();
        try(
                BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("file/d.txt"));
                BufferedInputStream bis=new BufferedInputStream(new FileInputStream("file/c.txt"));

               /* FileOutputStream fo=new FileOutputStream("file/d.txt");
                FileInputStream fi=new FileInputStream("file/c.txt");*/
        ){
             while (true){
                 int n=bis.read();
                 if (n==-1){
                     break;
                 }
                 char c= (char) n;
                 String str= String.valueOf(c);
                 set.add(str);
             }
            set1.addAll(set);

            String str2= set1.toString();
            /*for (String s:set1
                 ) {
                bytes=s.getBytes();
            }*/
            byte[] bytes = str2.getBytes();
            bos.write(bytes);

        }catch (FileNotFoundException e){
            System.out.println("文件路径有误");
        }catch (IOException e){
            System.out.println("文件读写有误");
        }catch (Exception e){
            System.out.println("未知错误");
        }
//        set.forEach(string-> System.out.println(string));
//        set1.addAll(set);
        set1.forEach(string-> System.out.print(string));
    }
}

 

  • 27
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这孩子叫逆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值