黑马程序员--IO其他流

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

</pre><p></p><p><span style="font-family:SimSun;font-size:14px;">一IO流ObjectInputStream和ObjectOutputStream</span></p><p><span style="font-family:SimSun;font-size:14px;">1好处:可以直接操作对象,类通过实现serializable借口,以启用其序列化功能,它没有方法,成为标记借口;</span></p><p><span style="font-family:SimSun;font-size:14px;">2举例:</span></p><p></p><pre name="code" class="java"><span style="font-family:SimSun;font-size:14px;">public static void main(String[] args) throws Exception
	{
		//writeobj();
		readobj();
	}
	public static void writeobj() throws IOException
	{
		ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("a.txt"));
		oos.writeObject(new Person("lisi",20,"hanguo"));
		oos.writeObject(new Person("zhangsan",33,"meiguo"));
		oos.close();
	}
	public static void readobj() throws Exception
	{
		ObjectInputStream ois=new ObjectInputStream(new FileInputStream("a.txt"));
		Person p=(Person)ois.readObject();
		System.out.println(p);
		System.out.println(p.getName()+"...."+p.getAge()+".."+p.getCountry());
		ois.close();
	}</span>
<span style="font-family:SimSun;font-size:14px;">import java.io.*;
class Person implements Serializable

{
	public static final long SerialVersionUID=42;
	 String name;
	 transient int age;//被transient修饰后不能被序列化
	static String country;//静态不能被序列化
	Person(String name,int age,String country)
	{
		this.name=name;
		this.age=age;
		this.country=country;
	}
	public int getAge()
	{
		return age;
	}
	public String getName()
	{
		return name;
	}
	public String getCountry()
	{
		return country;
	}
	public String toString()
	{
		return name+age+country;
	}
}</span>
3注意三点: 1静态是不能被序列化的。 2如果非静态想被序列化可以加transient。 3想被序列化的对象要实现Serializable。 IO流(管道流)  PipedInputStream和PipedOutputStream  输入输出可以直接进行连接,通过结合线程使用。

4举例:

<span style="font-family:SimSun;font-size:14px;">import java.io.*;
class PipedInputStream1
{
	public static void main(String[] args) throws IOException 
	{
		PipedInputStream pis=new PipedInputStream();
		PipedOutputStream pos=new PipedOutputStream();
		pis.connect(pos);
		Pipedint int1=new Pipedint(pis);
		Pipedout out1=new Pipedout(pos);
		Thread read1 =new Thread(int1);
		Thread write1=new Thread(out1);
		read1.start();
		write1.start();
		
		
	}
}
class Pipedint implements Runnable
{
	PipedInputStream pis;
	Pipedint(PipedInputStream pis)
	{
		this.pis=pis;
	}
	public void run()
	{
		try
		{
			byte[] arr=new byte[1024];
			int ch=0;
			while ((ch=pis.read(arr))!=-1)
			{
				String ss=new String(arr,0,ch);
				System.out.println(ss);
			}
		}
		catch (IOException e)
		{

		}
		finally 
		{
			try
			{
				pis.close();
			}
			catch (IOException e)
			{
			}
			
		}
		
	}
}
class Pipedout implements Runnable
{
	PipedOutputStream pos;
	Pipedout(PipedOutputStream pos)
	{
		this.pos=pos;
	}
	public void run()
	{
		try
		{
			pos.write("wo lai le".getBytes());
			System.out.println("pipedint");
		}
	
		catch(IOException e)
		{
		
		}
		finally 
		{
			try
			{
				pos.close();
			}
			catch (IOException e)
			{
			}
		}
		
	}
}
</span>

二RandomAccessFile 

1该类不是算是IO体系中子类。而是直接继承自Object。 但是它是IO包中成员。因为它具备读和写功能。 内部封装了一个数组,而且通过指针对数组的元素进行操作。 以通过getFilePointer获取指针位置, 同时可以通过seek改变指针的位置。 其实完成读写的原理就是内部封装了字节输入流和输出流。 通过构造函数可以看出,该类只能操作文件。 而且操作文件还有模式:只读r,,读写rw等。 如果模式为只读 r。不会创建文件。会去读取一个已存在文件,如果该文件不存在,则会出现异常。 如果模式rw。操作的文件不存在,会自动创建。如果存则不会覆盖。

2举例:

<span style="font-family:SimSun;font-size:14px;">import java.io.*;
class RandomAccessFile1
{
	public static void main(String[] args) throws IOException
	{
		write2();
		read2();
	}
	public static void write2() throws IOException 
	{
		RandomAccessFile ras=new RandomAccessFile("a.txt","rw");
		ras.write("李四".getBytes());
		ras.writeInt(88);
		ras.write("张三".getBytes());
		ras.writeInt(38);
		ras.seek(24);
		ras.write("王五".getBytes());
		ras.writeInt(43);
		ras.close();

	}
	public static void read2() throws IOException 
	{
		RandomAccessFile ras=new RandomAccessFile("a.txt","r");
		ras.seek(24);
		byte[] arr=new byte[4];
		ras.read(arr);
		String ss=new String(arr);
		
		int age=ras.readInt();
		System.out.print("name="+ss);
		System.out.print("age="+age);
		ras.close();
	}


}</span>


三用于操作基本数据类型的流:DataInputStream和DataOutputStream

<span style="font-family:SimSun;font-size:14px;">import java.io.*;
class DataInputStream1
{
	public static void main(String[] args) throws IOException
	{
		writeData();
		readData();
	}
	public static void writeData() throws IOException
	{
		DataOutputStream dos=new DataOutputStream(new FileOutputStream("c.txt"));
		dos.writeInt(235);
		dos.writeBoolean(true);
		dos.writeChars("nihao");
		dos.close();
	}
	public static void readData() throws IOException
	{
		DataInputStream dis=new DataInputStream(new FileInputStream("c.txt"));
		int a=dis.readInt();
		boolean b=dis.readBoolean();
		char c=dis.readChar();
		sop(a);
		sop(b);
		sop(c);
		dis.close();
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}</span>

四用于操作字节数组的流对象。ByteArrayInputStream,ByteArrayOutputStream

ByteArrayInputStream :   在构造的时候,需要接收数据源,。而且数据源是一个字节数组。

ByteArrayOutputStream: 在构造的时候,不用定义数据目的,因为该对象中已经内部封装了可变长度的字节数组。 这就是数据目的地。 因为这两个流对象都操作的数组,并没有使用系统资源。
所以,不用进行close关闭。
在流操作规律讲解时:
源设备,
键盘 System.in,硬盘 FileStream,内存 ArrayStream。
目的设备:
控制台 System.out,硬盘FileStream,内存 ArrayStream。

<span style="font-family:SimSun;font-size:14px;">public static void main(String[] args)
	{
		
	
		ByteArrayInputStream bais=new ByteArrayInputStream("abcdef".getBytes());
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		int ch=0;
		while ((ch=bais.read())!=-1)
		{
			baos.write(ch);
		}
		System.out.println(baos.size());
		System.out.println(baos.toString());</span>

五IO流编码表 

1编码表由来: 计算机只能识别二进制数据,早期由来是电信号。 为了方便应用计算机,让它可以识别各个国家的文字。 就将各个国家的文字用数字来表示,并一一对应,形成一张表。这就是编码表。 常见编码表: 
ASCII:美国标准信息交换码。用一个字节的7位可以表示。
ISO8859-1:拉丁码表。欧洲码表用一个字节的8位表示。
GB2312:中国的中文编码表。用两个字节来表示。
GBK:中国的中文编码表升级,融合了更多的中文文字符号。用两个字节来表示。
Unicode:国际标准码,融合了多种文字。所有文字都用两个字节来表示,Java语言使用的就是unicode
UTF-8:最多用三个字节来表示一个字符。

注意: 
ISO8859-1码表之所以能编码解码是因为ISO8859-1不涉及到汉字编码 
而UTF-8编码表涉及到了汉字编码,所以导致了数据的错乱。 
Tomcat服务器默认编码是ISO8859-1。 

六练习

有五个学生,每个学生有3门课的成绩,
从键盘输入以上数据(包括姓名,三门课成绩),
输入的格式:如:zhagnsan,30,40,60计算出总成绩,
并把学生的信息和计算出的总分数高低顺序存放在磁盘文件指定文件中。

思想:
1,通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象。
2,因为学生有很多,那么就需要存储,使用到集合。因为要对学生的总分排序。
所以可以使用TreeSet。
3,将集合的信息写入到一个文件中。

<span style="font-family:SimSun;font-size:14px;">import java.io.*;
import java.util.*;
class StudentExample
{
	public static void main(String[] args) throws IOException 
	{
		Comparator<Person> cmp=Collections.reverseOrder();
		Set<Person> sss=PersonTool.getPerson(cmp);
		PersonTool.writeFile(sss);
	}
}
class Person implements Comparable
{
	private String name;
	private int shu,ying,yu,sum;
	
	Person(String name,int shu,int ying,int yu)
	{
		this.name=name;
		this.shu=shu;
		this.ying=ying;
		this.yu=yu;
		sum=shu+ying+yu;
		
	}
	public String getName()
	{
		return name;
	}
	public int getSum()
	{
		return sum;
	}
	public String toString()
	{
		return "student["+name+","+shu+","+ying+","+yu+"]";


	}
	public int hashCode()
	{
		return name.hashCode()+sum*37;
	}
	public boolean equals(Object obj)
	{
		if (!(obj instanceof Person))
			return false;
		Person p=(Person)obj;
		return name.equals(p.name)&& this.sum==p.sum;
	}
	public int compareTo(Object obj)
	{
		if (!(obj instanceof Person))
			throw new RuntimeException("cuowu");
		Person p=(Person)obj;
		int add=new Integer(sum).compareTo(new Integer(p.sum));
		if (add==0)
			return name.compareTo(p.name);
		return add;

	}
}
class PersonTool
{
	public static Set<Person> getPerson() throws IOException 
	{
		return getPerson(null);
	}
	public static Set<Person> getPerson(Comparator<Person> cmp) throws IOException 
	{
		Set<Person> set=null;
		if (cmp==null)
		{
			set=new TreeSet<Person>();
		}
		else
			set=new TreeSet<Person>(cmp);
		

		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String line=null;
		while ((line=br.readLine())!=null)
		{
			if ("over".equals(line))
				break;
			String[] arr=line.split(",");
			Person stu=new Person(arr[0],Integer.parseInt(arr[1]),Integer.parseInt(arr[2]),Integer.parseInt(arr[3]));
			set.add(stu);
			
			


		}
		br.close();
		return set;
		
	}
	public static void writeFile(Set<Person> set) throws IOException 
	{
		BufferedWriter bw=new BufferedWriter(new FileWriter("b.txt"));
		for (Person stuu:set )
		{
			bw.write(stuu.toString()+"\t");
			bw.write(stuu.getSum()+"");
			bw.newLine();
			bw.flush();
			
		}
		bw.close();
	}
}
</span>


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值