第二十一天,其他流对象和一些程序

android培训java培训、期待与您交流!


import java.io.*;

class RandomAccessFileDemo
{
	public static void main(String[] args) throws IOException
	{
		//ranWrite();
		//ranReader();
		ranWrite2();
	}
	public static void ranReader()throws IOException
	{
		RandomAccessFile raf = new RandomAccessFile("acc.txt","r");
		//raf.write("haha".getBytes());

		raf.seek(8*1);//指针

		//raf.skipBytes(8*1);//跳过,不能向回走

		byte[] buf = new byte[4];
		raf.read(buf);
		String name = new String(buf);
		int age = raf.readInt();
		System.out.println("name=" + name);
		System.out.println("age=" + age);

		
		

		raf.close();
	}

	
	public static void ranWrite2()throws IOException
	{
		RandomAccessFile raf = new RandomAccessFile("acc.txt","rw");

		raf.seek(8*4);
		raf.write("一啊".getBytes());
		raf.writeInt(99);
		raf.seek(8*4);

		raf.close();

	}

	public static void ranWrite()throws IOException
	{
		RandomAccessFile raf = new RandomAccessFile("acc.txt","rw");
		raf.write("小啊".getBytes());
		raf.writeInt(99);

		raf.write("二啊".getBytes());
		raf.writeInt(90);

		raf.close();
		
	}
}

/*
ObjectOutputStream:直接操作对象的流。
对象本身存在堆内存中,用流的方式将这个对象存到硬盘上。
Serializable:序列化的对象才能使用,没有方法的接口称为标记接口。
静态不能被序列化,原理:静态在,序列化在堆里。
非静态的我们不想被序列化,加上关键字transient

*/
import java.io.*;
class  ObjectStream
{
	public static void main(String[] args) throws Exception
	{
		 //writeObj();
		 readObj();
	}
	public static void writeObj()throws Exception
	{
		ObjectOutputStream oos = 
			new ObjectOutputStream(new FileOutputStream("obj.txt")); 
		oos.writeObject(new Person("wangwu",49,"kr"));
		oos.close();
	}
	public static void readObj()throws Exception
	{
		ObjectInputStream ois = 
			new ObjectInputStream(new FileInputStream("obj.txt"));
		Person p = (Person) ois.readObject();
		System.out.println(p);
		ois.close();
	}
}

import java.io.*;
class Person  implements Serializable

{
	//public static final long serialVersionUID = 42L;
	String name;
	transient int age;//transient修饰不能被序列化
	Static String country ="cn";//static 静态不能被序列化
	Person(String name,int age,String country)
	{
		this.name = name;
		this.age = age;
		this.country = country;

	}
	
	public String toString()
	{
		return name +":"+ age+":"+country;
	}
}

import java.io.*;

class EncodeStream 
{
	public static void main(String[] args) throws IOException
	{
		//writeText();
		readText();
	}

	public static void readText()throws IOException
	{
		InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");
		
		char buf[] = new char[10]; 
		int len = isr.read(buf);

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

		System.out.println(str);
		
	}

	public static void writeText()throws IOException
	{
		OutputStreamWriter osw = 
			new OutputStreamWriter(new FileOutputStream("utf.txt"),"UTF-8");

		osw.write("你好");
		osw.close();
	}
}
import java.io.*;

class EncodeStream 
{
	public static void main(String[] args) throws IOException
	{
		//writeText();
		readText();
	}

	public static void readText()throws IOException
	{
		InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");
		
		char buf[] = new char[10]; 
		int len = isr.read(buf);

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

		System.out.println(str);
		
	}

	public static void writeText()throws IOException
	{
		OutputStreamWriter osw = 
			new OutputStreamWriter(new FileOutputStream("utf.txt"),"UTF-8");

		osw.write("你好");
		osw.close();
	}
}

import java.io.*;
import java.util.*;
class Student implements Comparable
{
	private String name;
	private int ma,cn,en;
	private int sum;
	Student(String name,int ma,int cn,int en)
	{
		this.name = name;
		this.ma = ma;
		this.cn = cn;
		this.en = en;
		sum = ma+cn+en;
	}

	public int compareTo(Student stu)
	{
		int num = new Integer(this.sum).compareTo(new Integer(stu.sum));
		if (num == 0)
		{
			return this.name.compareTo(s.name);
		}
		return num;
	}

	public String getName()
	{
		return name;	
	}
	public int getSum()
	{
		return sum;
	}
	public int hashCode()
	{	
		return name.hashCode()+sum*78;
	}

	public boolean equals(Object obj)
	{
		if (!(obj instanceof Student))
		{
			throw new ClassCastException("类型不符");
		}
		Student s = (Student)obj;
		return this.name.equals(s.name) && this.sum == s.sum;
	}

	public String toString()
	{
		return "Student["+name+","+ma+","+cn+","+en+"]";
	}


}

Class StudentInfoTool
{
	public static Set<Student> getStudents()throws IOException
	{
		getStudents(null);
	}

	public static Set<Student> getStudents(Comparator<Student> cmp)throws IOException
	{
		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
		String line = null;
		Set<Student> stus =null;
		if (cmp == null)
		{
			stus = new TreeSet<Student>();
		}
		else
			stus = new TreeSet<Student>(cmp);
		while ((line = bufr.readLine())!=null)
		{
			if ("over".equals(line))
			{
				break;
			}
			String[] info = line.split(",");
			Student stu = new Student(info[0],Integer.parseInt(info[1])
												Integer.parseInt(info[2])
												Integer.parseInt(info[3]));
			Stus.add(stu);
			
		}
		bufr.close();
	}

	public static void write2File(Set<Student> stus)throws IOException
	{
		BufferedWriter bufw = new BufferedWriter(new FileWriter("infostu.txt"));
		for (Student s:stus )
		{
			bufw.write(s.toString()+"\t");
			bufw.write(s.getSum()+"");
			bufw.newLine();
			bufw.flush();
		}
		bufw.close();
	}
}


class ClassDemo3 
{
	public static void main(String[] args) throws IOException
	{
		Comparator cmp = Collections.reverseOrder();
		Set<Student> stus = StudentInfoTool.getStudents(cmp);
		StudentInfoTool.write2File(stus);
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值