IO流的作业题
来源:互联网 发布:魔兽世界数据库1.11 编辑:程序博客网 时间:2024/05/19 23:04
1、PrintStream、BufferedWriter、PrintWriter的比较?
答:PrintStream:属于字节流,即可以打印字节也可以打印数组
写入文件不用close或flush
e.printStackTrace()、System.out.println()都是它的实例。
BufferedWriter、PrintWriter:属于字符流,只可以打印字符或字符串
因为有缓存,要想写入文件必须close或flush
2、java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?
答:字节流,字符流。字节流继承于InputStream \ OutputStream,字符流继承于InputStreamReader \ OutputStreamWriter。
处理流,PrintStream节点流,转换流InputStreamReader,OutputStreamWriter。
3、什么是java序列化,如何实现java序列化?
答:java中的序列化机制能够将一个实例对象(只序列化对象的属性值,而不会去序列化什么所谓的方法。)的状态信息写入到一个字节流中使其可以通过socket进行传输、或者持久化到存储数据库或文件系统中;然后在需要的时候通过字节流中的信息来重构一个相同的对象。一般而言,要使得一个类可以序列化,只需简单实现java.io.Serializable接口即可。
4、解释一下java.io.Serializable接口(面试常考)
答:实现 java.io.Serializable 接口的类是可序列化的。没有实现此接口的类将不能使它们的任一状态被序列化或逆序列化。序列化类的所有子类本身都是可序列化的。这个序列化接口没有任何方法和域,仅用于标识序列化的语意。
通俗点说,所谓的Serializable,就是java提供的通用数据保存和读取的接口。至于从什么地方读出来和保存到哪里去都被隐藏在函数参数的背后了。这样子,任何类型只要实现了Serializable接口,就可以被保存到文件中,或者作为数据流通过网络发送到别的地方。也可以用管道来传输到系统的其他程序中。这样子极大的简化了类的设计。只要设计一个保存一个读取功能就能解决上面说得所有问题。
允许非序列化类的子类型序列化,子类型可以假定负责保存和恢复父类型的公有的、保护的和(如果可访问)包的域的状态。只要该类(扩展)有一个无参构造子,可初始化它的状态,那么子类型就可承担上述职责。在这种情况下申明一个可序列化的类是一个错误。此错误将在运行时被检测。
在逆序列化期间,将使用非序列化类的公用的或保护的无参构造子对它的域进行初始化。可序列化子类必须能访问父类的无参构造子。它的所有域将从相应流中被恢复。
当遍历一个图象时,很可能遇到一个不支持序列化接口的对象。此时将抛出 NotSerializableException 异常,且标识该非序列化对象类。
在序列化处理过程中需要特定句柄的类,必须使用如下这些恰当的标记实现特定的方法:
private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;
writeObject 方法负责写特定类的对象的状态,以至相应的 readObject 方法能恢复它。 通过调用 out.defaultWriteObject 可激活保存对象域的缺省机制。此方法不必关心状态是属于它的父类还是子类。 使用 writeObject 方法或基本类型支持的 DataOutput 方法将每个域的状态保存到 ObjectOutputStream 中。
readObject 方法负责从此流中读取并恢复类的域。它可能调用 in.defaultReadObject 方法来调用该对象的非静态和非暂时的域的缺省恢复机制。 defaultReadObject 方法使用该流中的信息,用当前对象中的已命名域分配保存在流中该对象的相应域。当类已改进增加了新域时,这个接口也做相应处理。此方法不必关心状态是属于它的父类还是子类。 使用 writeObject 方法或基本类型支持的 DataOutput 方法将每个域的状态保存到 ObjectOutputStream 中。
5、选择题
1、B;2、BF;3、EF;4、BFGH;5、D;6、A;7、ABCD;8、B9、DACBE;10、BCADEGF;11、BD;12、B
6、将一个GBK编码的文本文件转存为一个UTF-8编码的文本文件。
package Day1301;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class ChangeCoding {
public static void changcoding(String inEncoding,
String outEncoding, String inFileName,
String outFileName) throws IOException{
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(inFileName), inEncoding));
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(outFileName),outEncoding));
String s = null;
while((s = reader.readLine()) != null){
writer.write(s, 0, s.length());
writer.newLine();
}
writer.flush();
writer.close();
reader.close();
}
public static void main(String[] args) {
try{
changcoding("GBK", "UTF-8", "gbk.text","utf8.text");
}catch(IOException e){
System.out.println("转换失败的原因" + e.getMessage());
}
}
}
7、编写一个程序,把a.txt中的单词与b.txt中的单词交替合并到c.txt中,a.txt中的单词用空格分隔b.txt中的单词用回车或空格分隔。
package Day1301;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class PinJie {
public static void main(String[] args) {
try{
File a = new File("f:\\a.txt");
File b = new File("f:\\b.txt");
FileWriter fw = new FileWriter("F:\\c.txt");
Youio ra = new Youio(a);
Youio rb = new Youio(b);
String aword = null;
String bword = null;
while(ra.hasNext() && rb.hasNext()){
aword = ra.next();
fw.write(aword);
bword = rb.next();
fw.write(bword);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
class Youio{
int id = 0;
String[] arr;
File form = null;
FileReader reader = null;
public Youio(File f) throws IOException{
this.form = f;
reader = new FileReader(form);
char[] c = new char[(int) form.length()];
int index = reader.read(c);
String str = new String(c, 0, index);
arr = str.split("\n");
for(String s : arr){
System.out.println(s);
}
}
public boolean hasNext(){
if(id < arr.length){
return true;
}
return false;
}
public String next(){
return arr[id++];
}
}
8、package Day1301;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class StudentInfo {
private List<Student> studentList;
public static void main(String[] args) {
StudentInfo studentInfo = new StudentInfo();
studentInfo.readStudentInfo("f:\\student.txt");
studentInfo.comparator();
studentInfo.print();
}
public void print(){
for(Student stu : studentList){
System.out.println(stu);
}
}
public void comparator(){
Collections.sort(this.studentList, new Comparator<Student>(){
public int compare(Student o1, Student o2) {
return o1.getAge().compareTo(o2.getAge());
}
});
//翻转List
Collections.reverse(this.studentList);
}
public void readStudentInfo(String filePath){
studentList = new ArrayList<Student>();
BufferedReader reader = null;
try{
reader = new BufferedReader(
new InputStreamReader(new FileInputStream(filePath)));
String line = "";
while((line = reader.readLine()) != null){
if(!line.startsWith("#")){
String[] strs = line.split("\\s+");
if(strs.length == 3){
Student stu = new Student();
stu.setName(strs[0]);
stu.setSex(strs[1]);
try{
stu.setAge(new Integer(strs[2]));
}catch(NumberFormatException number){
stu.setAge(new Integer("0"));
}
studentList.add(stu);
}else{
throw new RuntimeException("存在不正确的输入行");
}
}
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(null != reader){
try{
reader.close();
reader = null;
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
class Student{
private String name;
private String sex;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer strs) {
this.age = strs;
}
public String toString(){
return
"Student[name" + name + ",sex" + sex + ",age" + age + "]";
}
}
9、
mport java.io.File;
public class FileTest
{
//递归删除指定路径下的所有文件
public static void deleteAll(File file)
{
if(file.isFile() || file.list().length == 0)
{
file.delete();
}
else
{
File[] files = file.listFiles();
for(File f : files)
{
deleteAll(f);//递归删除每一个文件
f.delete();//删除该文件夹
}
}
}
public static void main(String[] args)
{
File file = new File("d:/abc/xyz");
deleteAll(file);
}
}
- IO流的作业题
- 一个计算机网络的作业题
- 我的作业题
- 小纪的作业题
- 关于查询的作业题
- 作业题
- 作业题
- 作业题
- 作业题
- 一道Java的课后作业题
- 【GDKOI2014】小纪的作业题
- 今天留的作业题。自己写的。
- 国外某所大学的java作业题
- boj 469. 暑假作业题-数字的读法
- 最近做的一道c++作业题
- 一道关于SVM的机器学习作业题
- 关于微信红包的作业题的解决思路
- 去年的算法作业题_01背包_矩阵相乘
- git 配置快捷键
- 悬挂“else”
- 快速Android开发系列网络篇之Retrofit
- 学习函数模板,意外学习sizeof
- 路由器桥接(WDS) 无法从副路由(B路由,从路由)上网 解决方法(DHCP设置)
- IO流的作业题
- 8 次尝试,带你走进 iOS 精益编程--cocoaChina
- Spring IoC学习笔记(1):注解配置Bean
- C#中实现并发的几种方法以及其性能测试
- Nginx+php fastcgi 发生 Access Denied
- todo:关于Delphi的几个问题
- android 消息机制
- Java基础之理解Annotation
- centos7下安装composer