——- android培训、java培训、期待与您交流! ———-
RandomAccessFile
不是IO体系中的子类,而是直接继承Objcet,但是IO包中的成员,因为其具备读和写功能,内部封装了一个数组,而且通过指针对数组元素进行操作。可以通过getFilePointer获取指针位置,通过seek改变指针的位置。
其实完成读写的原理就是内部封装了字节输入流和输出流
如果模式为只读,不会创建文件,会读取一个已存在的文件,若文件不存在会出现异常
如果模式为rw,操作文件不存在,会自动创建,如果存在则不会覆盖
代码学习记录:
public class RandomAccessDemo {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
readFile();
}
public static void readFile() throws Exception {
RandomAccessFile raf = new RandomAccessFile("ran.txt", "r");
//调整指针
raf.seek(8);
byte[] buf = new byte[4];
raf.read(buf);
String s = new String(buf);
int age = raf.readInt();
System.out.println(s);
System.out.println(age);
raf.close();
}
/**
* 写
* @throws Exception
*/
public static void writeFile() throws Exception {
RandomAccessFile raf = new RandomAccessFile("ran.txt", "rw" );
raf.write("李四".getBytes());
raf.writeInt(97);
raf.write("王二".getBytes());
raf.writeInt(99);
raf.close();
}
}
管道流
PipedInputStream
管道输入流应该连接到管道输出流;管道输入流提供要写入管道输出流的所有数据字节。通常,数据由某个线程从 PipedInputStream 对象读取,并由其他线程将其写入到相应的 PipedOutputStream。不建议对这两个对象尝试使用单个线程,因为这样可能死锁线程。
PipedOutputStream
以将管道输出流连接到管道输入流来创建通信管道。管道输出流是管道的发送端。通常,数据由某个线程写入 PipedOutputStream 对象,并由其他线程从连接的 PipedInputStream 读取。不建议对这两个对象尝试使用单个线程,因为这样可能会造成该线程死锁。
两个类方法与其父类基本一致。
代码学习记录:
class Read implements Runnable {
private PipedInputStream in;
Read (PipedInputStream in) {
this.in = in;
}
@Override
public void run() {
// TODO Auto-generated method stub
byte[] buf = new byte[1024];
try {
System.out.println("准备读取数据,没有阻塞。");
int len = in.read(buf);
System.out.println("读取数据完毕,准备打印。");
String s = new String(buf, 0, len);
System.out.println(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (in!= null)
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Write implements Runnable {
private PipedOutputStream out;
Write (PipedOutputStream out) {
this.out = out;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
System.out.println("数秒后开始写数据");
Thread.sleep(5000);
out.write("bingo".getBytes());
System.out.println("数据写入完毕。");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (out!=null)
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class PipedStreamDemo {
public static void main(String[] args) {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
try {
in.connect(out);//输入流接收输出流
Read r = new Read(in);
Write w = new Write(out);
new Thread(r).start();
new Thread(w).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ObjectStream
类的序列化描述符。它包含类的名称和 serialVersionUID
代码学习记录:
public class ObjectStreamDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
// writeObj();
readObj();
}
/**
* 将数据序列化
*/
public static void writeObj() {
try {
//目的地
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("objectStream.txt"));
oos.writeObject(new Person("lisi", 23));
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 反序列化
*/
public static void readObj() {
try {
//目的地
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("objectStream.txt"));
Person p = (Person) ois.readObject();
System.out.println(p);
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
练习:
* 有五个学生,每个学生有3门课
* 从键盘输入以上数据(姓名,3门课成绩)
* 输入格式:zhangsan,30,40,50计算总成绩
* 并把学生的信息和计算的总分高低顺序放在磁盘文件“stu.txt”中
练习步骤:
* 1,描述一个学生对象
* 2,定义一个可以操作学生对象的工具
*
* 思想:
* 1,通过键盘录入一行数据,并将该行的信息取出封装成学生对象
* 2,因为学生很多,需要存储,使用到集合,因为要对总分排序
* 使用TreeSet
* 3,将集合中的信息写到文件中。
代码学习记录:
/**
* Comparable
* 此接口强行对实现它的每个类的对象进行整体排序。
* 这种排序被称为类的自然排序,
* 类的 compareTo 方法被称为它的自然比较方法。
* @author 弥尔
*
*/
class Student implements Comparable<Student> {
private String name;
private int ma,cn,en;
private int sum;
public Student(String name,int ma,int cn,int en ) {
// TODO Auto-generated constructor stub
this.name = name;
this.cn = cn;
this.ma = ma;
this.en = en;
this.sum = ma + cn + en;
}
public String getName() {
return this.name;
}
public int getSum() {
return this.sum;
}
/**
* String.hashCode()
* 返回此字符串的哈希码。
*/
public int hashCode() {
return name.hashCode()+sum*78;
}
public boolean equal(Object obj) {
if(!(obj instanceof Student)) {
throw new ClassCastException("类型不匹配!");
}
Student s = (Student)obj;
return this.name.equals(s.name) && this.sum==s.sum;
}
/**
* 自然排序,总分从低到高
*/
@Override
public int compareTo(Student s) {
// TODO Auto-generated method stub
int num = new Integer(this.sum).compareTo(new Integer(s.sum));
if (num==0) {
return this.name.compareTo(s.name);
}
return num;
}
public String toString() {
return "student["+name+","+ma+","+cn+","+en+"]";
}
}
class StudentInfoTool {
/**
* 没有比较器的方法
* 可以调用有比较器方法,传参数为null
* @return
* @throws IOException
*/
public static Set<Student> getStudents() throws IOException {
return getStudents(null);
}
/**
* 有比较器的方法
* @param cmp
* @return
* @throws IOException
*/
public static Set<Student> getStudents(Comparator<Student> cmp) throws IOException {
System.out.println("录入学生信息!");
BufferedReader br = 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=br.readLine())!=null) {
if("over".equalsIgnoreCase(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);
}
br.close();
return stus;
}
public static void writeFile(Set<Student> stus) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("stuinfo.txt"));
for ( Student s : stus ) {
bw.write(s.toString()+"\t");
bw.write(s.getSum()+"");//s.getSum()---->整数,后八位,不能识别,
bw.newLine();
bw.flush();
}
bw.close();
}
}
public class StudentInfoTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
/**
* 强行反转比较器
* Collections.reverseOrder(); ------>返回一个反向比较器
*/
Comparator<Student> cmp = Collections.reverseOrder();
Set<Student> stus = StudentInfoTool.getStudents(cmp);
StudentInfoTool.writeFile(stus);
}
/**
* reverse
* 反转指定列表中元素的顺序。
*/
public void test() {
StringBuffer value = new StringBuffer();
value.append("abcdfg");
StringBuffer v = value.reverse();
System.out.println(v.equals(value));
}
}