尚学堂第十章课后习题答案

选择题:

1.B
解析:IOException是IO流的错误异常,ClassNotFound是类异常,SQLException是数据库异常,RemoteException系统运行时的异常

2.D
解析:release()方法不清楚,close()是关闭文档流,remove()是删除文档流,flush( )是自动刷新缓存区方法

3.B
解析:Reader类中不存在reader(int n)方法,存在reader()和reader(byte [ ] b)方法

4.C
解析:(一)忘记关闭文档流(二)忘记抛出异常,使用try-catch

5.D
解析:第四步错误在于没有使用try-catch

简答题

1.说出本章最基本的四个抽象类以及他们的区别

1.InputStream 输入字节流
2.OutputStream 输出字节流
3.Reader 输入字符流
4.Writer 输出字符流

字节流和字符流的区别在于单位的不同获取数据,前者以字节为单位获取数据;后者以字符为单位获取数据

2.读入读出流的数据必须是按照顺序读出的吗?如果想读取某个文件的指定位置,如何实现?

读入读出流的数据不一定是要按照顺序读出的,例如随意访问文件流(RandomAccessFile),可以通过== seek()==方法找到指定的位置进行读出数据。

3.想要复制一个文本数据,使用那些流?如果考虑效率问题,使用哪些流更好?

复制文本数据使用文件字节流或者文件字符流都是可以的,但是考虑效率问题则需要使用到缓冲数组,或者使用缓冲字节流或(或者缓冲字符流)进行包装。

4.对象序列化接口的特点

需要实现Serializable接口,并且使用private static final long serialiVersionUID 赋值一个固定的序列化ID。

5.想把一个字节流转换为一个字符流,使用什么流?

使用转换流InputStreamReader和OutputStreamWriter实现字节流转换为字符流。

编程题

1.实现字节数组和任何基本类型与引用类型之间的相互转换(提示:使用ByteArrayInputStream和ByteArrayOutputStream)
package cn.istell.packge.practice06;

import com.sun.beans.editors.ByteEditor;
import com.sun.xml.internal.bind.v2.util.ByteArrayOutputStreamEx;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

import java.io.*;

class transform{
    //byte类型的数组转换为int类型
    public  int toBasicInt(byte [] b){
        ByteArrayInputStream dis = new ByteArrayInputStream(b);
        DataInputStream fis = new DataInputStream(dis);
        int num = 0;
        try{
            num = fis.readInt();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fis != null)
                    fis.close();

            } catch (IOException e) {
                e.printStackTrace();
            }try{
                if (dis != null){
                    dis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return num;
    }

    //将int类型转换为字节数组
    public byte[] toIntByteArray(int num){
        //使用DataOutputStream类进行封装ByteArrayOutputStream的对象
        //使其拥有转换数据的方法
        //在调用writerInt方法写入到bis对象中去
        ByteArrayOutputStream bis = new ByteArrayOutputStream();
        DataOutputStream dis = new  DataOutputStream(bis);
        byte [] b = null;
        try{
            dis.writeInt(num);
        } catch (IOException e) {
            e.printStackTrace();
        }
//      调用toByteArray方法转换byte【】类型
        b = bis.toByteArray();
//      System.out.println("b is length:"+b.length);
        try{
            //遵循后开先关的规则
            if(dis != null){
                dis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }try{
            if(bis != null)
                bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i]+"  ");
        }
        return b;
    }

//  将char类型转换为byte【】类型
    public byte[] toCharByteArray(char ch){
        ByteArrayOutputStream bos = new ByteArrayOutputStreamEx();
        DataOutputStream dos = new DataOutputStream(bos);
        byte [] b = null;
        try{
            dos.writeChar(ch);
        } catch (IOException e) {
            e.printStackTrace();
        }
        b = bos.toByteArray();
        try{
            if(dos != null)
                dos.close();
        }catch (IOException a){
            a.printStackTrace();
        }try{
            if (bos != null)
                bos.close();
        }catch (IOException c){
            c.printStackTrace();
        }
        return b;
    }

    //将byte【】类型转换为char类型
    public char toBasicChar(byte [] b){
        ByteArrayInputStream bis = new ByteArrayInputStream(b);
        DataInputStream dis = new DataInputStream(bis);
        char ch=0;
        try{
            ch = dis.readChar();
        } catch (IOException e) {
            e.printStackTrace();
        }try{
            if(dis != null)
                dis.close();
        }catch (IOException a){
            a.printStackTrace();
        }try{
            if (bis != null)
                bis.close();
        }catch (IOException c){
            c.printStackTrace();
        }
        return ch;
    }

    //将byte类型转换为Object类型
    public Object toBasicObject(byte b[]) {
        ByteArrayInputStream bis = new ByteArrayInputStream(b);
        //对象流 ObjectInputStream
        ObjectInputStream obj = null;
        try{
            obj = new ObjectInputStream(bis);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Object o = null;
        try{
            o = obj.readObject();
        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }try{
            if(obj != null) obj.close();
        }catch (IOException e){
            e.printStackTrace();
        }try{
            if (bis!=null) bis.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        return o;
    }

    //将Object类型转换为byte类型
    public byte[] toObjectByteArray(Object o) {
        ByteArrayOutputStream bos = new ByteArrayOutputStreamEx();
        ObjectOutputStream oos = null;
        byte b[]=null;
        try{
            oos = new ObjectOutputStream(bos);
        } catch (IOException e) {
            e.printStackTrace();
        }try{
            oos.writeObject(o);
        }catch (IOException e){
            e.printStackTrace();
        }
        b = bos.toByteArray();
        try{
            if (oos != null) oos.close();
        }catch (IOException e){
            e.printStackTrace();
        }try{
            if (bos != null) bos.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        return b;
    }

}
public class BufferedArrayInputStream_test {
    //主方法
    public static void main(String xx[]){
        transform t = new transform();
        //通过对象实现该方法
        System.out.println(t.toBasicInt(t.toIntByteArray(25)));
        System.out.println(t.toBasicChar(t.toCharByteArray('c')));
    }
}
2.将文件夹d:/sxtjava中的所有文件和子文件的内容复制到d:/sxtjava2文件中。
package cn.istell.packge.practice06;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileTest {
    public static void main(String[] args) {
        copyDir("D:/javaFileText", "D:/javaFileText2");
    }

    public static void  copy(String srcPath,String desPath){
        File src = new File(srcPath);//将字符串实例化对象
        File dest = new File(desPath);
        InputStream is = null;//使用文件字节流来进行复制粘贴
        OutputStream os = null;
        try{
            is = new FileInputStream(src);
            os = new FileOutputStream(dest);
            byte[ ] b = new byte[1024];
            int n = -1;
            while((n = is.read(b)) != -1){//将字节保存在b数组中,read方法的返回值是长度,如果到尾部则返回-1
                os.write(b,0,n);//off代表的是偏移量(起始位置),n代表的是长度,b代表数组
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (os != null)
                    os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (is != null)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void copyDir(String srcPath,String desPath) {
        File file = new File(srcPath);
        if (file != null && file.exists()){
            if (file.isFile()){
                copy(srcPath,desPath);
            }else{
                File newDir = new File(desPath,file.getName());//获取原地址的路径,建立一个目的地的File对象
                newDir.mkdirs();//在目的地创建同样的目录
                for(File s:file.listFiles()){//得到该文件下的目录数组
                    if (s.isFile()){
                        File newFile = new File(newDir,s.getName());
                        copy(s.getAbsolutePath(),newFile.getAbsolutePath());//取绝对路径进行复制粘贴
                    }
                    else{//如果当前位置是目录,那么递归当前方法,进行寻找该目录的下一个目录
                        copyDir(s.getAbsolutePath(),newDir.getAbsolutePath());
                        System.out.println("执行中...");
                    }
                }
            }
        }
    }

}



3.使用输入输出包中的类读取exam.txt文本文件内容,每读取一次,将每行作为一个输入放入ArrayList的泛型集合中,并将集合中的内容使用加强for进行输出显示。
package cn.istell.packge.practice06;

//        使用输入输出包中的类读取exam.txt文本文件内容,
//        每读取一次,将每行作为一个输入放入ArrayList的泛型集合中,并将集合中的内容使用加强for进行输出显示。

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class question3 {
    public static void main(String xx[]) {
        FileReader fr = null;
        String str=null;
        List<String > list = new ArrayList<String >();
        try{
            fr = new FileReader("D:/javaFileText/exam.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader br = new BufferedReader(fr);
        while(str!=null){
            try{
               str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            list.add(str);
        }
        for(String str0:list){
            if (str0!=null)
                System.out.println(str0);
        }
    }
}

参考博文link

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值