第二十一天,其他流对象和一些程序
来源:互联网 发布:it综合服务 编辑:程序博客网 时间:2024/05/22 14:06
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>();}elsestus = 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);}}
- 第二十一天,其他流对象和一些程序
- 第二十一天-Java_IO(其他工具类)
- 世界杯第二十一天
- 第二十一天
- 第二十一天:总结
- 第二十一天
- 毕业设计第二十一天
- 编程第二十一天
- CSS3----第二十一天
- 第二十一天
- 第二十一天总结
- 黑马程序员——>第二十一天<io流(对象序列化-管道流-字符编码)>
- java 十一天 对象和行为
- 第二十一天:听课笔记
- 第二十一天总结0418
- 实习日记->第二十一天
- 第二十一天:网络编程
- php实战第二十一天
- Ajax:拥抱JSON,让XML走开(2)
- XP安装.NET 3.5时报错的解决方法
- C# windowsService安装
- cookie 在多网站的平台下,会混乱 把名称设置复杂一下,即可解决
- 菜鸟stm32学习笔记(1)系统时钟初始化函数
- 第二十一天,其他流对象和一些程序
- Caused by: java.lang.OutOfMemoryError: PermGen space
- [设计模式]_[中级]_[模板方法C++例子实现举例]
- 下载内核映像和根文件系统到开发板
- 前后摄切换会死机 及各种sensor调试方法
- hdu 1598 find the most comfortable road(枚举+并查集)
- Ajax:拥抱JSON,让XML走开(3)
- ThinkPHP笔记
- windows phone:基本导航