JAVA---IO流

流:

什么是流?

流是一种抽象概念,它代表了数据的无结构化传递。按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列。从流中取得数据的操作称为提取操作,而向流中添加数据的操作称为插入操作。用来进行输入输出操作的流就称为IO流。换句话说,IO流就是以流的方式进行输入输出。

通俗地讲:流就是一根管道,连接着设备和程序,用于进行数据传输。

数据流的方向分为输入流和输出流;

按照处理单位的划分,分为字节流和字符流

按照功能的划分,分为节点流和处理流

节点流:可以从或向一个特定的地方(节点)读写数据

处理流:是对一个已存在的流的连接和封装,通过所封装的流的功能调用实现数据读写

以下是字符流:

import java.io.*;
public class IO {
	public static void main(String[]args)throws Exception
	{
		FileReader fr=new FileReader("H:/a.txt");
		FileWriter fw=new FileWriter("H:/b.txt");
		int ch;
		ch=fr.read();
		while(ch!=-1)
		{
			fw.write(ch);
			ch=fr.read();
			
		}
		fw.flush();//刷新缓冲区
		fr.close();
		fw.close();
	}
}


刷新输出流非常重要。如果服务器写入了几百个字节的请求,通常你会等待响应,然后再发送更多的数据。不过,如果输出流有有一个1024字节的缓冲区,那么这个流在发送缓冲区中的数据之前会等待更多的数据到达。在服务器响应到达之前不会向流写入更多数据,但是响应永远也不会到来,因为请求还没有发送,flush方法可以强迫缓冲的流发送数据,即使缓冲区还没有满,以此来打破这种死锁的状态


以下是字节流:

import java.io.*;
public class IO {
	public static void main(String[]args)throws Exception
	{
		FileInputStream fr=new FileInputStream("H:/a.txt");
		FileOutputStream fw=new FileOutputStream("H:/b.txt");
		int ch;
		ch=fr.read();
		while(ch!=-1)
		{
			fw.write(ch);
			ch=fr.read();
		}
		fw.flush();//刷新缓冲区
		fr.close();
		fw.close();
	}
}


字符流一般用来用于文本上;字节流可用于文本,视频,图片等上

缓冲流:

缓冲流就是带有缓冲区的的输入输出流

缓冲流可以显著减少我们对IO访问的次数,保护我们的硬盘!

缓冲流本身就是处理流,缓冲流必须得依附节点流

处理流是包裹在原始节点流上的流

缓冲字节流:

import java.io.*;
public class IO {
	public static void main(String[]args)throws Exception
	{
		BufferedInputStream bis=new BufferedInputStream(new FileInputStream("H:/a.txt"));
		BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("H:/b.txt"));
		byte buf[]=new byte[1024];
		int len;
		len=bis.read(buf);
		while(len!=-1)
		{
			bos.write(buf,0,len);
			len=bis.read(buf);
		}
		bos.flush();//刷新缓冲区
		bis.close();
		bos.close();
	}
}

缓冲字符流

import java.io.*;
public class IO {
	public static void main(String[]args)throws Exception
	{
		BufferedReader bs=new BufferedReader(new FileReader("H:/a.txt"));
		BufferedWriter bw=new BufferedWriter(new FileWriter("H:/b.txt"));
		String str=bs.readLine();
		while(str!=null)
		{
			bw.write(str);
			bw.newLine();//插入一个换行符
			str=bs.readLine();
		}
		bw.flush();//刷新缓冲区
		bs.close();
		bw.close();
	}
}


数据流

DataInputStream能够以一种与机器无关的方式,直接从底层字节输入流读取JAVA基本类型和String类型的数据

DataOutputStream能够以一种与机器无关的方式,直接将JAVA基本类型和String类型写到其他的字节输出流

import java.io.*;
public class IO {
	public static void main(String[]args)throws Exception
	{
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		DataOutputStream dos=new DataOutputStream(baos);
		int a=123456;
		dos.writeInt(a);
		byte b[]=baos.toByteArray();//返回一个字节数组
		ByteArrayInputStream bais=new ByteArrayInputStream(b);
		DataInputStream dis=new DataInputStream(bais);
		int n;
		n=dis.readInt();
		System.out.println(n);
	}
}
转换流

OutputStreamWriter流是把OutputStream流转化成Writer流

InputeStreamReader流是把InputStream流转化成Reader流

import java.io.*;
public class IO {
	public static void main(String[]args)throws Exception
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String s=br.readLine();
		System.out.println(s);
	}
}

Print流

Print流只输出不输入

PrintWriter 输出字符

PrintStream 输出字节

PrintStream只能封装OutputStream类型的字节流,PrintWriter可以封装OutputStream也可以封装Writer

import java.io.*;
public class IO {
	public static void main(String[]args)throws Exception
	{
		PrintStream ps=new PrintStream("H:/b.txt");
		System.setOut(ps);
		System.out.println(123);
	}
}


对象序列化

所谓序列化:就是把一个Object对象直接转化成字节流,然后把这个字节流直接写入本地硬盘或者网络中

如果想要实现对象序列化,就必须实现Serializable接口(这个接口是空的,这种接口起的作用就是标识作用,潜在含义是告诉编译器这个类是允许被序列化的)

Java中transient修饰的成员表示在对象序列化的时候不能被序列化

import java.io.*;
class Person implements Serializable{
	String name;
	long phone;
	public Person(String name,long phone){
		this.name=name;
		this.phone=phone;
	}
}
public class IO {
	public static void main(String[]args)throws Exception
	{
		ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("H:/b.txt"));
		Person p=new Person("xiaoming",123);
		Person p1=null;
		oos.writeObject(p);
		ObjectInputStream ois=new ObjectInputStream(new FileInputStream("H:/b.txt"));
		p1=(Person)ois.readObject();
		System.out.println(p1.name);
		System.out.println(p1.phone);
	}
}


猜你喜欢

转载自blog.csdn.net/qq_36457148/article/details/77585003