Io基础

129 阅读5分钟

IO

用来进行输入输出操作的流就称为IO流

输入输出,主要应用于程序和外存(硬盘/网络)中的资源之间进行数据传输

流的概念可以理解为“数据流通的管道”


IO流的分类:

1.根据数据流向划分:输入流和输出流

输入流从外界流向java程序,只进行写操作

输出流从java程序流向外界,只进行读操作

 

2. 根据数据操作的数据单元划分:字节流和字符流

字节流以字节为单位,字符流以字符为单位

字节流能够处理大部分类型的数据,字节就是二进制

字符流只能处理纯文本数据,作用提高处理纯文本数据的效率

 

3. 根据操作对象不同划分: 节点流(基本流)和处理流

可以从或向一个特定的io设备读或者写数据的流称为节点流

节点流是可以直接连接数据源的流

处理流是对一个已存在的流进行连接或封装,不直接与数据资源相连接,通过封装后的流来实现数据读或写功能

处理流是节点流使用装饰着模式来增加更多的功能


四个抽象基础类:

字节输入流、字节输出流、字符输入流、字符输出流

InputStream:

他是一个抽象类,是所有字节输入流类的父类主要包含三个方法

//读取一个字节并以整数的形式返回(0~255),如果返回-1说明已到输入流的末尾。

 int read();

 //读取一系列字节并存储到一个数组buffer,返回实际读取的字节数,如果读取前已到输入流的末尾返回-1。

int read(byte[] buffer);

//读取length个字节并存储到一个字节数组buffer,从off位置开始存,最多len, 返回实际读取的字节数,如果读取前以到输入流的末尾返回-1。

int read(byte[] buffer, int off, int len);

简单示例:

public static void main(String[] args) throws IOException  {

InputStream is = new FileInputStream("新建文本文档.txt");

byte[] b = new byte[1024];

int len = -1;

while((len=is.read(b))!= -1){

String str = new String(b, 0, len);

}

is.close();

}

 

Outputstream:

是所有输出字节流的父类 是一个抽象类主要包含四个方法

//向输出流中写入一个字节数据,该字节数据为参数b的低8位。

void write(int b) ;

//将一个字节类型的数组中的数据写入输出流。

void write(byte[] b);

//将一个字节类型的数组中的从指定位置(off)开始的,len个字节写入到输出流。

 void write(byte[] b, int off, int len);

 //将输出流中缓冲的数据全部写出到目的地。

 void flush();

.

Reader:

 是所有的输入字符流的父类,它是一个抽象类。主要包含三个方法

//读取一个字符并以整数的形式返回(0~255),如果返回-1已到输入流的末尾。

 int read() ;

 //读取一系列字符并存储到一个数组buffer,返回实际读取的字符数,如果读取前已到输入流的末尾返回-1。

int read(char[] cbuf) ;

//读取length个字符,并存储到一个数组buffer,从off位置开始存,最多读取len,返回实际读取的字符数,如果读取前以到输入流的末尾返回-1。

int read(char[] cbuf, int off, int len)

.

Writer :

是所有的输出字符流的父类,它是一个抽象类。主要包含六个方法

//向输出流中写入一个字符数据,该字节数据为参数b的低16位。 void write(int c);

//将一个字符类型的数组中的数据写入输出流,

 void write(char[] cbuf)

//将一个字符类型的数组中的从指定位置(offset)开始的,length个字符写入到输出流。

 void write(char[] cbuf, int offset, int length);

 //将一个字符串中的字符写入到输出流。

 void write(String string);

 //将一个字符串从offset开始的length个字符写入到输出流。 void write(String string, int offset, int length);

 //将输出流中缓冲的数据全部写出到目的地

void flush()

做简单例子:  

 public static void* main(String[] args) **throws** IOException {
  
  Writer w=new FileWriter("新建文本文档.txt");
  
  String t="";
  
 while(!t.equals("88")) {
  
  System.out.println("请输入:");
  
  t=new Scanner(System.in).next();
  
  w.write("\n");
  
  w.write(t);
  
  }
  
  w.close();
  
  }
  
  

缓冲流:

缓冲流的作用是提高流读取、写入的速度

BufferedInputStream:提高了缓冲区功能

BufferedOutputStream:提供了缓冲区功能和flush缓冲区的方法

BufferedReader:提供了缓冲区。ReadLine读一行

BufferedWreter:提供了缓冲区,newLine换行

简单示例使用BufferedReader 读一行功能:

 

 public static void main(String[] args) throws IOException {

 Reader r = new FileReader( "新建文本文档.txt");
 
 BufferedReader br = new BufferedReader(r);
 
 String line = null;

 System.out.println("姓名\t年龄");
 
 while(( line = br.readLine()) != null){
 
 String[] ss = line.split("=");
 
 System.out.println(ss[0]+"\t"+ss[1]);
 
 }
 
 br.close();
 
 }

Objec对象流

举实例解释浅克隆:

    public class Beauty implements Cloneable{
 
 private Cat c;
 
  
 
 public Cat getC() {
 
 return c;

 }

 

 public void setC(Cat c) {

 this.c = c;

 }

 

 public Beauty() {
 
 super();

 }

 

 public Beauty(Cat c) {

 super();

 this.c = c;

 }

 public static void main(String[] args) throws CloneNotSupportedException {

 Cat tttnew Cat();

 Beauty b1=new Beauty(ttt);

 Beauty b2=(Beauty) b1.clone();

 System.out.println(b1==b2);

 System.out.println(b1.getC()==b2.getC());

 }

 }

加上一个Cat类即可

 运行结果为 false

            True

  浅克隆:需要把beauty类实现cloneable接口,clone()由object类提供的,beauty类直接使用即可

  Beauty对象得到了克隆,但是cat对象没有克隆

深克隆示例:

 Beauty类

public class Beauty implements Serializable{
       
private static final long serialVersionUID = 110L;
    
private Cat c;

public Cat getC() {
	return c;
}

public void setC(Cat c) {
	this.c = c;
}

public Beauty() {
	super();
}

public Beauty(Cat c) {
	super();
	this.c = c;
}


public static void main(String[] args) throws  IOException, ClassNotFoundException {
	Cat ttt=new Cat();
	Beauty b1=new Beauty(ttt);
	ObjectOutputStream oos=new ObjectOutputStream( new FileOutputStream("huhuhu.oos"));
	oos.writeObject(b1);
	oos.close();
	
	ObjectInputStream ois= new ObjectInputStream(new FileInputStream("huhuhu.oos"));
	Beauty b2=(Beauty) ois.readObject();
	System.out.println(b1==b2);
	System.out.println(b1.getC()==b2.getC());
}
   }

Cat类:

public class Cat implements Serializable{

private static final long serialVersionUID = 10L;

   }

运行结果为 False   False

     

深克隆:需要把beauty类实现serializable接口,添加一个序列化版本号 ,cat类也需要实现serializable接口

使用objectinputstream/objectoutoutstream来进行写和读对象,此时 beauty类对象和cat对象都得到了克隆

 

 

      

 

 

 \

 


\