javaIO中的类使用方法

/**
ObjectInputStream、
ObjectOutputStream 通过在流中使用文件可以实现对象的持久存储。
*/
import java.io.*;
public class ObjectStreamDemo{
	public static void main(String args[])throws Exception{
		//WriteObj();
		ReadObj();
	}
	public static void ReadObj()throws Exception{
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
		Person p = (Person)ois.readObject();
		System.out.println(p);
	}
	public static void WriteObj()throws IOException{
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
		oos.writeObject(new Person("name",18));
		oos.close();
	}
}

/**
管道输入流应该连接到管道输出流;管道输入流提供要写入管道
输出流的所有数据字节。通常,数据由某个线程从 PipedInputStream 
对象读取,并由其他线程将其写入到相应的 PipedOutputStream。
不建议对这两个对象尝试使用单个线程,因为这样可能死锁线程。
管道输入流包含一个缓冲区,可在缓冲区限定的范围内将读操作和写操作分离开。
如果向连接管道输出流提供数据字节的线程不再存在,则认为该管道已损坏。 
*/
import java.io.*;
class Read implements Runnable{
	private PipedInputStream pis;
	public Read(PipedInputStream i){
		this.pis = i;
	}
	public void run(){
		byte []b = new byte[1024];
		try{
		int line = pis.read(b);
		String info = new String(b,0,line);
		System.out.println("content:"+info);
		pis.close();
		}catch(IOException e){
			throw new RuntimeException("管道流读取失败!");
		}
	}
}
class Write implements Runnable{
	private PipedOutputStream pos;
	public Write(PipedOutputStream o){
		this.pos = o;
	}
	public void run(){
		String n = "hello world!";
		try{
		pos.write(n.getBytes());
		pos.close();
		}catch(IOException e){
			throw new RuntimeException("管道流写入失败!");
		}
	}
}
public class PipStream{
	public static void main(String args[])throws Exception{
		PipedInputStream pis = new PipedInputStream();
		PipedOutputStream pos = new PipedOutputStream();
		pos.connect(pis);
		Read r = new Read(pis);
		Write w = new Write(pos);
		new Thread(r).start();
		new Thread(w).start();
	}
}

/**
RandomAccessFile类支持对随机访问文件的读取和写入
其中的seek方法可以从文件中的指定位置读取
同时该类 还提供RandomAccessFile(String name, String mode) 的
构造方法,mode是设置权限r,rw等.
其中提供了各种各样的write和read方法,具体可以查看文档.
当文件设置成r时,如果文件不存在,不能自动创建文件。
如果为rw时,如果文件不存在则自动创建文件,如果文件存在,则覆盖该文件
------该类可以应用于多线程的读写文件--->及多线程下载功能
*/
import java.io.*;
public class RandomAccessFileDemo{
	public static void main(String args[])throws IOException{
		//writeRan();
		readRan();
	}
	public static void readRan()throws IOException{
		RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
		int i=0;
		byte[] b= new byte[6];
		while(i<5){
			raf.seek(i*10);
			
			int len = raf.read(b);
			String name = new String(b,0,len);
			int age = raf.readInt();
			System.out.println("name="+name+"::age="+age);
			i++;
			
		}
		raf.close();
	}
	public static void writeRan()throws IOException{
		RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
		for(int i=0;i<5;i++){
		//	raf.seek(20*i);  //设置每次写入不能超过20字节
			raf.write(("李四"+i).getBytes());
			raf.writeInt(i*10);
		}
		raf.close();
	}
}

/*
DataInputStream 和 DataOutputStream
该类便于读写java类型的基本数据类型
*/
import java.io.*;
public class DataStreamDemo{
	public static void main(String args[])throws IOException{
		//writeFile();
		readFile();
	}
	public static void readFile()throws IOException{
		DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
		int a = dis.readInt();
		double d = dis.readDouble();
		float f = dis.readFloat();
		boolean b = dis.readBoolean();
		System.out.println("a="+a+"-d="+d+"-f="+f+"-b="+b);
		dis.close();
	}
	public static void writeFile()throws IOException{
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
		dos.writeInt(23);
		dos.writeDouble(23.222d);
		dos.writeFloat(33.22f);
		dos.writeBoolean(true);
		dos.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值