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);
}
}
本文介绍如何通过Java和Android培训提升技能,并提供代码示例进行实践交流。

被折叠的 条评论
为什么被折叠?



