Java作业10

1.Java中的流的分类有哪些?

流动方向上:一般分为输入流和输出流。如System.in是一个InputStream类型输入流;System.out是一个OutputStream类型输出流

读取类型上:一般分为字节流和字符流。如System.in是一个InputStream类型字节流;new InputStreamReader(System.in)是一个字符流对象

发生源头上:分为节点流和过滤流类。节点流:直接操作目标设备对应的流,如文件流,标准输入输出流;过滤流:继承带有关键字Filter的流,用于包装操作节点流,方便读写各种类型的数据。

2.字节流InputStream和OutputSrteam的子类分别有哪些?请举例说明其使用场景。与其对应的字符流分别有哪些?

(1)FileInputStream和FileOutputStream类。使用场景:在文件和流之间搭建桥梁。对应字符流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);
		}
	}
}
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);} }
}

 (2)PipeInputStream和PipeOutputStream类。使用场景:用于讲一个程序的输出连接到另一个程序的输入输出流作为管道的发送端,输入流作为管道的接收端。对应字符流PipeReader和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)ByteArrayInputStream与ByteArrayOutputStream类。使用场景: 在字节数组和流之间搭建桥梁。对应的字符流: 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
		}
	}
}

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()));
	}
}

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) {}
}

6.Java的File类表示什么?有什么作用?

File类不仅指系统众多文件,也指目录,因为目录也是特殊的文件。                                                                                                                                                                    作用:一个File对象而可以代表一个文件或目录,
File可以实现获取文件和目录属性等功能,
可以实现对文件和目录的创建,删除等功能。


7.Java对文件的读写分别提供了哪些支持?

本例从OpenFile.java中读取字节并存储在缓存区buffer中,再根据buffer中实际读到的字节数量将它们构造成字符串显示在屏幕上。例:

读取文件:

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);}
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值