首页 > 编程语言 > 详细

JavaSE习题 第九章 输入输出流

时间:2018-11-10 20:39:58      阅读:665      评论:0      收藏:0      [点我收藏+]

问答题

1.如果准备读取一个文件的内容,应该使用FileInputStream还是FileOutputStream?

FileInputStream

2.FileInputStream流的read()方法与FileReader流的read()方法有何不同?

FIleInputStream的read可以读一个字节,而FileReader中的read可以读一个字符

3.BufferedReader流能指向一个文件对象吗?

不能

4.ByteArrayOutputStream流怎样获取缓冲区的内容?

获取byte数组的字节单元

5.PipedInputStream类和PipedOutputStream类的主要用途是什么?

换线程进行管道输入输出

6.使用ObjectInputStream类和ObjectOutputStream有哪些注意事项?

保证对象是序列化的,要实现Serializable

7.怎样使用输入流和输出流及技术克隆对象?

8.使用RandomAccessFile类读写文件的好处是什么?

可以获取文件指针

作业题

1.编写一个应用程序,读取一个文本文件的内容。

public static void main(String[] args) throws IOException{
        FileInputStream fis=new FileInputStream(new File("1.txt"));
        int b;
        while((b=fis.read())!=-1) {
            System.out.print((char)b);
        }
        fis.close();
    }

 

2.编写一个应用程序,将用户键盘输入的10行文字存入文件

public static void main(String[] args) throws IOException{
        FileWriter fw=new FileWriter("2.txt");
        BufferedWriter bw=new BufferedWriter(fw);
        String str;
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i < 10; i++) {
            str=sc.nextLine();
            bw.write(str+"\n");
        }
        sc.close();
        bw.close();
        fw.close();
    }

3.使用数组字符流将俄文字母写入内存,再从内存取出

public static void main(String[] args) throws IOException{
        String eWen="Аа Бб Вв Гг Дд Ее Ёё Жж Зз Ии Йй Кк Лл Мм Нн Оо Пп Рр Сс Тт Уу Фф Хх Цц Чч Шш Щщ Ъъ Ыы Ьь Ээ Юю Яя";
        char[] a=eWen.toCharArray();
        ByteArrayOutputStream baos=new ByteArrayOutputStream(a.length);
        for (char c : a) {
            baos.write(c);
        }
        ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
        int n;
        while((n=bais.read())!=-1) {
            System.out.print((char)n);
        }
    }

 

4.编写一个应用程序,将用户从键盘输入的10个整数存入文件,再顺序读出

5.编写一个应用程序,要求将一个LinkedList<E>创建的对象写入文件,再读出一个LinkedList<E>对象,并遍历LinkedList<E>节点中的数据

6.使用RandomAccessFile流将一个文本文件倒置输出

JavaSE习题 第九章 输入输出流

原文:https://www.cnblogs.com/littlepage/p/9940328.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!