Java第十次总结

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

  1. 输入输出:Java中有输入流和输出流
  2. 流的编码方式: Java中有字节流和字符流
  3. 从发生的源头:分为节点流和过滤流类

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

常用子类:

  • ByteArrayInputStream和ByteArrayOutputStream

    1. 场景:在字节数组和流之间搭建桥
    2. 构造方法:public ByteArrayInputStream(byte[] buf) :将字节数组作为字节流的数据源,public ByteArrayOutputStream() :构造一个字节数组输出流,用于将多个字节写入到流中,最后可以整体转为一个字节数组
    3. 对应的字符流: 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()));
	}
}
  • FileInputStream和FileOutputStream

    1. 场景:在文件和流之间搭建桥梁
    2. 构造方法:FileInputStream(String name) :以文件路径名字构造一个文件输入流,打开一个与实际文件的连接,用于从该流中读取文件字节流。FileOutputStream(String name) :以文件路径名字构造一个文件输出流,打开一个与实际文件的连接,用于文件的写字节流操作。
    3. 对应字符流:FileReader和FileWriter
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);} }
}
  • PipedInputStream和PipedOutputStream
    1. 场景: 用于将一个程序的输出连接到另一个程序的输入输出流作为管道的发送端,输入流作为管道的接收端使用前需要调用connect方法将输出流和输入流连接起来,通常一个线程执行管道输出流的写操作,另一个线程执行管道输入流的读操作。
    2. 对应字符流:PipedReader和PipeWriter
    3. 构造方法:
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("cqq.txt"));
  1. 输出字符流转为字节流用到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();
    }
  }
}
  • 3.PrintWriter的使用:可以向该字符流中写入Java基本数据类型,用于包装输出字符流类对象
import java.io.*;
public class Printwrit i
public static void main(String[] args)throws Exception{
Printwriter out = new Printwriter(new Bufferedwriter(new
Filewriter( "foo.txt")));
out.println( “hello"); /l/写入字符串
out.println(3); 1/写入整型
out.close(); 关闭流,系统自动将缓冲区内容flush

5.什么是对象的序列化和反序列化? Java对此提供了哪些支持?

  • 序列化与反序列化的概念:序列化将实现了Seriallizable接口的对象转换成一个字节序列,并能够在以后将这个字节序列完全恢复为原来的对象,后者又称反序列化
        FileOutputStream fout = new FileOutputStream(fname);
        ObjectOutputStream out = new ObjectOutputStream(fout);
        out.writeObject(this);   //**对象序列化**
        out.close();
    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();

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);}
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值