尚学堂-第十章答案

一、选择题

1.使用Java IO流实现对文本文件的读写过程中,需要处理下列( )异常。(选择一项)

A.ClassNotFoundException

B.IOException

C.SQLException

D.RemoteException

2.在Java的IO操作中,( )方法可以用来刷新流的缓冲。(选择两项)

A.void release()

B.void close()

C.void remove()

D.void flush()

3.在Java中,下列关于读写文件的描述错误的是( )。(选择一项)

A.Reader类的read()方法用来从源中读取一个字符的数据

B.Reader类的read(int n )方法用来从源中读取一个字符的数据

C.Writer类的write(int n)方法用来向输出流写入单个字符

D.Writer类的write(String str)方法用来向输出流写入一个字符串

4.阅读下列文件定入的Java代码,共有( )处错误。(选择一项)

import java.io.*;
public class TestIO {
    public static void main(String []args){
        String str ="文件写入练习";
        FileWriter fw = null;        //1
        try{
            fw = new FileWriter("c:\mytext.txt");  //2
            fw.writerToEnd(str);   //3
        }catch(IOException e){   //4
            e.printStackTrace();
        }finally{
            //此处省略关闭流
        }
    }
}

A.0

B.1

C.2

D.3

5.分析如下Java代码,有标注的四行代码中,有错误的是第( )处。(选择一项)

import java.io.FileWriter;
import java.io.IOException;
public class Test {
    public static void main(String[ ] args) {
        String str = "Hello World";
        FileWriter fw = null;
        try {
            fw = new FileWriter("c:\\hello.txt"); // 1
            fw.write(str);                     // 2
        } catch (IOException e) {
            e.printStackTrace();                // 3
        } finally {
            fw.close();                        // 4
        }
    }
}

A.1

B.2

C.3

D.4

二、简答题

  1. 说出本章最基本的四个抽象类及他们的区别?
    InputStream,此抽象类是表示字节输入流的所有类的父类。OutputStream此抽象类是表示字节输出流的所有类的父类。输出流接收输出字节并将这些字节发送到某个目的地。Reader,用于读取的字符流抽象类,数据单位为字符。Writer,用于写入的字符流抽象类,数据单位为字符。

  2. 读入读出流的数据必须是按照顺序,依次读出吗? 如果我想读取某个文件指定位置,如何做到?
    不一定。有RandomAccessFile类,使用seek方法确定定位点,任意读入读出。

  3. 想copy一个文本数据,使用哪些流? 如果考虑效率问题,使用那些流较好?
    FileInputStream,FileOutputStream。可用装饰类BufferedInputStream,BufferedOutputStream。

  4. 对象的序列化接口的特点。
    只有实现了Serializable接口的类的对象才能被序列化。 Serializable接口是一个空接口,只起到标记作用。 static属性不参与序列化。对象中的某些属性如果不想被序列化,不能使用static,而是使用transient修饰。为了防止读和写的序列化ID不一致,一般指定一个固定的序列化ID。

  5. 想把一个字节流转化成字符流,使用什么流?
    InputStreamReader OutputStreamWriter
    三、编码题

  6. 实现字节数组和任何基本类型和引用类型执行的相互转换

    提示:使用ByteArrayInutStream和ByteArrayOutputStream。

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

class ToByteArr{
	public byte[] intToByteArr(int num) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			dos.writeInt(num);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] flush = baos.toByteArray();
		try {
			dos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flush;
	}
	
	public byte[] booleanToByteArr(boolean flag) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			dos.writeBoolean(flag);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] flush = baos.toByteArray();
		try {
			dos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flush;
	}
	
	public byte[] ObjectToByteArr(Object obj) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(baos);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			oos.writeObject(obj);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] flush = baos.toByteArray();
		try {
			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flush;
	}
}

class ToBasic{
	public int byteArrToInt(byte[] flush) {
		ByteArrayInputStream bais = new ByteArrayInputStream(flush);
		DataInputStream dis = new DataInputStream(bais);
		int num = 0;
		try {
			num = dis.readInt();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return num;
	} 
	
	
	public boolean byteArrToBoolean(byte[] flush) {
		ByteArrayInputStream bais = new ByteArrayInputStream(flush);
		DataInputStream dis = new DataInputStream(bais);
		boolean flag = false;
		try {
			flag = dis.readBoolean();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public Object byteArrToObject(byte[] flush) {
		ByteArrayInputStream bais = new ByteArrayInputStream(flush);
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(bais);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		Object obj = null;
		try {
			obj = ois.readObject();
		} catch (ClassNotFoundException e) {
		} catch (IOException e) {
			e.printStackTrace();
		}
		return obj;
	}
}

public class Tenth1 {
	public static void main(String[] args) {
		ToByteArr toByteArr = new ToByteArr();
		ToBasic toBasic = new ToBasic();
		System.out.println(toBasic.byteArrToInt(toByteArr.intToByteArr(5)));
	}
}
  1. 复制文件夹d:/sxtjava下面所有文件和子文件夹内容到d:/sxtjava2。

    提示:涉及单个文件复制、目录的创建、递归的使用


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;

/**
 * 使用文件字节输入流和输出流达到文件的拷贝
 * @author Administrator
 *
 */
public class CopyDir {
	public static void main(String[] args) {
		copyDir("C:/eclipse-workspace/IO_Study02/TestDirSRC", "C:/eclipse-workspace/IO_Study02/TestDirDEST");
	//	File newDir = new File("C:/eclipse-workspace/IO_Study02/TestDirDEST","TestDirSRC");
	//	newDir.mkdirs();
	}
	


public static void copy(String srcPath,String destpath) {
	//1.创建源
	File src = new File(srcPath);	//源头	
	File dest = new File(destpath); //目的地
	//2.选择流
	InputStream is = null;
	OutputStream os = null;
	try {
		is = new FileInputStream(src);
		os = new FileOutputStream(dest);
		//3.操作
		byte[] flush = new byte[1024];
		int len = -1;
		while((len=is.read(flush))!=-1) {
			os.write(flush, 0, len);
		}
		os.flush();
	}catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}finally {
		//4.关闭文件,先打开的后关闭
		try {
			if (null != os) {
				os.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			if (null != is) {
				is.close();
			}	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

public static void copyDir(String srcPath,String destpath) {
	//1.创建源
	File src = new File(srcPath);	//源头	
//	File dest = new File(destpath); //目的地
	
	if(null != src && src.exists()) {
		if(src.isFile()) {
			copy(srcPath, destpath);
		}else {//子孙级
			File newDir = new File(destpath,src.getName());
			newDir.mkdirs();
			for(File s:src.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(s.exists());
					System.out.println(s.isFile());
				}
			}
		}
	}
}



}


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

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值