1.Java中流的分类有哪些?
1.从流动方向上来看,分为输入流与输出流。程序可以用输出流向文件写数据,用输入流从文件读数据。
标准输入:对象是键盘,Java对应类是System.in
标准输出:对象是屏幕,Java对应类是System.out
byte buffer[]=new byte[512];
int count=System.in.read(buffer);
for(int i=0;i<count;i++){
System.out.println(buffer[i]);
}
for(int i=0;i<count;i++){
System.out.println((char)buffer[i]);
}
2.从读取类型上分:一般分为字节流和字符流
字节流是从InputStream和OutputStream派生出来的一系列类,以字节为基本处理单位。
字符流是以Reader和Writer派生出来的一系列类,以Unicode码表示的字符为基本处理单位。
3.从发生的源头:分为节点流和过滤流类
节点流:直接操作目标设备对应的流。如文件流,标准输入输出流
过滤流:继承带有关键字Filter的流。用于包装操作节点流,方便读写各种类型的数据
2.字节流InputStream和OutputStream的子类分别有哪些?请举例说明其使用场景。与其对应的字符流分别有哪些?
1.常用的InputStream方法
int read() 抽象类 :从流中读入数据,需要子类覆盖,而本类中的其他带参数的read方法不是抽象方法,它们都调用read()。它从输入流读取一个8位的字节,把它转换0~255之间的整数,并返回这一整数。
int read(byte[ ] b) :读多个字节到数组中——缓冲区,每调用本方法一次,就从流中读取相应的数据到缓冲区,把它们保存到参数b指定的字节数组中,同时返回读到的字节数目,如果读完则返回-1。
int read(byte[ ] b,int off,int len):从输入流读取若干字节,把它们保存到参数b指定的字节数组中,off指定字节数组开始保存数据的起始下标,len表示读取的字节数目。
close():关闭此输入流并释放与该流关联的所有系统资源
2.常用的OutputStream方法
write(int b)抽象类:将一个整数输出到流中(只输出低八位字节,其他二十四位忽略)
write(byte[ ] b):将字节数组中的数据输出到流中
write(byte[ ] b,int off,int len):将指定的byte数组中从偏移量off开始的len个字节写入输出流
void flush():刷空输出流,并将缓冲区中的数据强制送出,只有BufferedOutputStream给出真正实现,其他流都是调用该流传入对象的相应方法。当需要建立一个输出缓冲区,多次写入,一次写出,一定要BufferedOutputStream,否则写入流的数据没有缓存功能。
close():关闭流并释放相关的系统资源
常用子类:
1.ByteArrayInputStream和ByteArrayOutputStream
场景:在字节数组和流之间搭建桥梁
构造方法:public ByteArrayInputStream(byte[] buf) :将字节数组作为字节流的数据源,public ByteArrayOutputStream() :构造一个字节数组输出流,用于将多个字节写入到流中,最后可以整体转为一个字节数组
对应的字符流: CharArrayReader和CharArrayWriter
ByteArrayInputStream
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
}
}
}
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()));
}
}
2.FileInputStream和FileOutputStream
场景:在文件和流之间搭建桥梁
构造方法:FileInputStream(String name) :以文件路径名字构造一个文件输入流,打开一个与实际文件的连接,用于从该流中读取文件字节流。FileOutputStream(String name) :以文件路径名字构造一个文件输出流,打开一个与实际文件的连接,用于文件的写字节流操作。
对应字符流:FileReader和FileWriter
FileInputStream读取文件
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写入文件
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);} }
}
3.PipedInputStream和PipedOutputStream
场景: 用于将一个程序的输出连接到另一个程序的输入输出流作为管道的发送端,输入流作为管道的接收端使用前需要调用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对此提供了哪些支持?
1.输入字节流转为字符流需要用到inputstreamReader的构造方法InputStreamReader(InputStream in)
InputStreamReader ins = new InputStreamReader(System.in);
InputStreamReader ins = new InputStreamReader(new FileInputStream("test.txt"));
2.输出字符流转为字节流用到OutputStreamWriter或PrintWriter的构造方法:OutputStreamWriter outs = new OutputStreamWriter(new FileOutputStream(“test.txt”));
4.Java中的过滤流(流的装配)有什么作用?请举例说明常用的过滤流。
作用:缓存作用,用于装配文件磁盘、网络设备、终端等读写开销大的节点流,提高读写性能
1.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("系统有错误");
2 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对此提供了哪些支持?
序列化与反序列化的概念:序列化将实现了Seriallizable接口的对象转换成一个字节序列,并能够在以后将这个字节序列完全恢复为原来的对象,后者又称反序列化
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类指系统中的文件和目录(目录是特殊的文件)
作用:可以通过File类在Java程序中操作文件或目录。File类只能用来操作文件或目录(包括新建、删除、重命名文件和目录等操作),但不能用来访问文件中的内容。如果需要访问文件中的内容,则需要使用输入/输出流。
7.Java对文件的读写分别提供了哪些支持?
读取:
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 (IOException ioe)
{ System.out.println(ioe);}
catch (Exception e)
{ System.out.println(e);}
}
}
写入:
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 (IOException ioe)
{ System.out.println(ioe);}
catch (Exception e)
{ System.out.println(e);}
}
}