1、Java中流的分类有哪些?
(1)流动方向:
- 输入流
- 输出流
(2)读取类型 - 字节流
- 自符流
(3)发生源头 - 节点流
- 过滤流
2、字节流InputStream和OutputStream的子类分别有哪些?请举例说明其使用场景。与其对应的字符流分别有哪些?
- ByteArrayInputStream与ByteArrayOutputStream
使用场景: 在字节数组和流之间搭建桥梁
构造方法public ByteArrayInputStream(byte[] buf)
:将字节数组作为字节流的数据源
对应的字符流:CharArrayReader
和CharArrayWriter
import java.io.*;
public class ByteArrayStream {
public static void main(String[] args) {
byte[] b = "hello".getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
int n = 0;
while((n = bais.read())!= -1) {
System.out.println((char)n) ; // hello
}
}
}
构造方法public ByteArrayOutputStream()
:构造一个字节数组输出流,用于将多个字节写入到流中,最后可以整体转为一个字节数组
import java.io.*;
public class ByteArrayStream {
public static void main(String[] args) {
byte[] b = "hello".getBytes();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(b,0,b.length);
System.out.println(new String(baos.toByteArray()));
}
}
- FileInputStream与FileOutputStream
使用场景: 在文件和流之间搭建桥梁
构造方法FileInputStream(String name)
: 以文件路径名字构造一个文件输入流,打开一个与实际文件的连接,用于从该流中读取文件字节流
与之对应的字符流:FileReader
和FileWriter
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.println(new String(buffer,0,c));
}
rf.close(); //关闭输入流
}
catch (FileNotFoundException ffe) {
System.out.println(ffe);
}
catch (IOException ioe) {
System.out.println(ioe);
}
}
}
构造方法FileOutputStream(String name)
:以文件路径名字构造一个文件输出流,打开一个与实际文件的连接,用于文件的写字节流操作
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);} }
}
- PipedInputStream与PipeOutputStream
使用场景: 用于讲一个程序的输出连接到另一个程序的输入
输出流作为管道的发送端,输入流作为管道的接收端
使用前需要调用connect
方法将输出流和输入流连接起来
通常一个线程执行管道输出流的写操作,另一个线程执行管道输入流的读操作
与之对应的字符流:PipedReader
和PipeWriter
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedStream {
public static void main(String[] args) throws IOException {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
new Thread(new Input(in)).start();
new Thread(new Output(out)).start();
}
}
class Input implements Runnable {
private PipedInputStream in;
public Input(PipedInputStream in) {
this.in = in;
}
public void run() {
byte[] buf = new byte[1024];
int len;
try {
len = in.read(buf);
String s = new String(buf,0,len);
System.out.println("in "+s);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Output implements Runnable{
private PipedOutputStream out;
public Output(PipedOutputStream out)
{
this.out = out;
}
public void run() {
try {
out.write("hello".getBytes());
}
catch (IOException e) {
e.printStackTrace();
}
}
}
3、字节流与字符流的转化是怎样的?Java对此提供了哪些支持?
- 输入字节流转化为字符流,使用
InputStreamReader
的构造方法;如:
InputStreamReader ins = new InputStreamReader(System.in);
InputStreamReader ins = new InputStreamReader(new FileInputStream("test.txt"));
- 输出字符流转化为字节流,使用
OutputStreamWriter
或PrintWriter
的构造方法
OutputStreamWriter outs = new OutputStreamWriter(new FileOutputStream(“test.txt”));
4、Java中的过滤流(流的装配)有什么作用?请举例说明常用的过滤流。
作用: 缓存作用,用于装配文件磁盘、网络设备、终端等读写开销大的节点流,提高读写性能
- 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("系统有错误");
- DataInputStream和DataOutputStream :可以从字节流中写入,读取Java基本数据类型,不依赖于机器的具体数据类型,方便存储和恢复数据
import java.io.*;
public class DataStream {
public static void main(String[] args)throws Exception{
try {
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new
FileOutputStream("test.txt")));
dos.writeInt(3);//写入整型
dos.writeDouble(3.14);//写入浮点型
dos.writeUTF(“hello”);//写入字符串
dos.close();
DataInputStream dis = new DataInputStream(new BufferedInputStream(new
FileInputStream("test.txt")));
System.out.println(dis.readInt()); //读取整型,输出3
System.out.println(dis.readDouble()); //读取浮点型,输出3.14
System.out.println(dis.readUTF()); //读取字符串,输出hello
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
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) {}
}
/**************
运行结果:
Student interface java.io.Serializable
1 Wang
****************/
6、Java的File类表示什么?有什么作用?
File类是对文件和路径名的抽象表示。
作用: 对文件和路径名进行修改、删除、创建、重命名等多种操作。
7、Java对文件的读写分别提供了哪些支持?
FileInputStream
类
以字节流的形式顺序读文件FileOutputStream
类
以字节流的形式顺序写文件FileReader
类
以字符流的形式顺序读文件FileWriter
类
以字符流的形式顺序写文件RandomAccessFile
类
支持文件的随机访问