补充题:
1.Java中流的分类有哪些?
流动方向
:分为输入流和输出流
- 输入流:如System.in是一个InputStream类型输入流
- 输出流:如System.out 是一个PrintStream类型输出流
读取类型
:分为字节流和字符流
- 字节流:如System.in是一个InputStream类型字节流
- 字符流:如new InputStreamReader(System.in)是一个字符流对象
发生的源头
:分为节点流和过滤流类
- 节点流:直接操作目标设备对应的流 如文件流,标准输入输出流
- 过滤流:继承带有关键字Filter的流 用于包装操作节点流,方便读写各种类型的数据
2.字节流InputStream和OutputStream的子类分别有哪些?请举例说明其使用场景。与其对应的字符流分别有哪些?
FileInputStream和FileOutputStream
在文件和流之间搭建桥梁。
FileInputStream构造方法:
FileInputStream(String name) :以文件路径名字构造一个文件输入流,打开一个与实际文件的连接,用于从该流中读取文件字节流
FileOutputStream构造方法:
FileOutputStream(String name) :以文件路径名字构造一个文件输出流,打开一个与实际文件的连接,用于文件的写字节流操作
例:OutputStream中的write(byte b[], int off, int len)
方法:将数组b中从off指定的位置开始,长度为len的数据输出到流中。
3.字节流与字符流的转化是怎样的?Java对此提供了哪些支持?
- 输入字节流转为字符流需要用到inputstreamReader的构造方法:
InputStreamReader(InputStream in)
例如:
InputStreamReader ins = new InputStreamReader(System.in);
InputStreamReader ins = new InputStreamReader(new FileInputStream(“test.txt”));
- 输出字符流转为字节流用到OutputStreamWriter或PrintWriter的构造方法:
OutputStreamWriter(OutputStream out)
PrintWriter(OutputStream out)
例如:
OutputStreamWriter outs = new OutputStreamWriter(new
FileOutputStream(“test.txt”));
4.Java中的过滤流(流的装配)有什么作用?请举例说明常用的过滤流。
过滤流: BufferedInputStream和BufferedOutputStream, 缓存作用,用于装配文件磁盘、网络设备、终端等读写开销大的节点流,提高读写性能
过滤流BufferedReader
的使用:用于缓存字符流,可以一行一行的读
import java.io.*;
public class inDataSortMaxMinIn {
public static void main(String args[]) {
try{
BufferedReader keyin = new BufferedReader(new
InputStreamReader(System.in));
String c1;
int i=0;
int[] e = new int[10];
while(i<10){
try{
c1 = keyin.readLine();
e[i] = Integer.parseInt(c1);
i++;
}
catch(NumberFormatException ee){
System.out.println("请输入正确的数字!");
}
}
}
catch(Exception e){
System.out.println("系统有错误");
}}}
PrintWriter
的使用:可以向该字符流中写入Java基本数据类型,用于包装输出字符流类对象
import java.io.*;
public class PrintWrit {
public static void main(String[] args)throws Exception{
PrintWriter out = new PrintWriter(new BufferedWriter(new
FileWriter("foo.txt")));
out.println(“hello”); //写入字符串
out.println(3); //写入整型
out.close(); //关闭流,系统自动将缓冲区内容flush
}
}
5.什么是对象的序列化和反序列化?Java对此提供了哪些支持?
串行化(Serialization)
:又称序列化,将实现了Seriallizable接口的对象转换成一个字节序列,并能够在以后将这个字节序列完全恢复为原来的对象,后者又称反序列化
串行化的目的:便于介质存储和网络传输
使用ObjectInputStream类和ObjectOutputStream类
import java.io.*;
public class Student implements Serializable { //序列化
int number=1;
String name;
Student(int number,String n1) {
this.number = number;
this.name = n1;
}
public static void main(String arg[]) {
String fname = "Student.obj"; //文件名
Student s1 = new Student(1,"Wang");
s1.save(fname);
s1.display(fname);
}
void save(String fname) {
try{
FileOutputStream fout = new FileOutputStream(fname);
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(this); //对象序列化
out.close();
}
catch (FileNotFoundException fe){}
catch (IOException ioe){}
}
void display(String fname) {
try{
FileInputStream fin = new FileInputStream(fname);
ObjectInputStream in = new ObjectInputStream(fin);
Student u1 = (Student)in.readObject(); //对象反序列化
System.out.println(u1.getClass().getName()+" "+
u1.getClass().getInterfaces()[0]);
System.out.println(" "+u1.number+" "+u1.name);
in.close();
}
catch (FileNotFoundException fe){}
catch (IOException ioe){}
catch (ClassNotFoundException ioe) {}
}
6.Java的File类表示什么?有什么作用?
7.Java对文件的读写分别提供了哪些支持?
package chap14_2;
import java.io.*;
public class OpenFile {
public static void main(String args[]) throws IOException{
try{ //创建文件输入流对象
FileInputStream rf = new FileInputStream("OpenFile.java");
int n=512,c=0;
byte buffer[] = new byte[n];
while ((c=rf.read(buffer,0,n))!=-1 ){ //读取输入流
System.out.print(new String(buffer,0,c));
}
rf.close(); //关闭输入流
}
catch (FileNotFoundException ffe){
System.out.println(ffe);}
catch (IOException ioe){
System.out.println(ioe);
}
}
import java.io.*;
public class Write1 {
public static void main(String args[]){
try{
System.out.print("Input: ");
int count,n=512;
byte buffer[] = new byte[n];
count = System.in.read(buffer); //读取标准输入流
FileOutputStream wf = new FileOutputStream("Write1.txt");
//创建文件输出流对象
wf.write(buffer,0,count); //写入输出流
wf.close(); //关闭输出流
System.out.println("Save to Write1.txt!");
}
catch (FileNotFoundException ffe){
System.out.println(ffe);}
catch (IOException ioe){
System.out.println(ioe);} }
}