JavaIO之RandomAccessFile(一)
来源:互联网 发布:在线客服软件有哪些 编辑:程序博客网 时间:2024/06/06 17:18
package three.day.io;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileTs {
public static void main(String[] args) throws IOException{
Person5 person51 = new Person5("zhangsan", 21);
Person5 person52 = new Person5("lisi", 22);
Person5 person53 = new Person5("wangwu", 23);
try {
RandomAccessFile file = new RandomAccessFile("RandomAccessFile.txt","rw");
file.write(person51.getName().getBytes());
file.writeInt(person51.getAge());
file.write(person52.getName().getBytes());
file.writeInt(person52.getAge());
file.write(person53.getName().getBytes());
file.writeInt(person53.getAge());
file.close();
RandomAccessFile file1 = new RandomAccessFile("RandomAccessFile.txt","r");
file1.skipBytes(12);
byte[] b = new byte[8];
file1.read(b, 0, b.length);
System.out.println(new String(b).trim());
System.out.println(file1.readInt());
file1.read(b, 0, b.length);
System.out.println(new String(b).trim());
System.out.println(file1.readInt());
file1.seek(0);
file1.read(b, 0, b.length);
System.out.println(new String(b).trim());
System.out.println(file1.readInt());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
class Person5 {
private String name;
private int age;
private static final int NAME_LEN = 8;
public Person5(String name, int age) {
super();
if(name.length()>NAME_LEN)
this.name = name.substring(0, 8);
else{
this.name = name;
for(int i=0;i<NAME_LEN - name.length();i++){
this.name = this.name + "\0";
}
}
System.out.println(name.length() + " " + this.name.length());
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static int getNameLen() {
return NAME_LEN;
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileTs {
public static void main(String[] args) throws IOException{
Person5 person51 = new Person5("zhangsan", 21);
Person5 person52 = new Person5("lisi", 22);
Person5 person53 = new Person5("wangwu", 23);
try {
RandomAccessFile file = new RandomAccessFile("RandomAccessFile.txt","rw");
file.write(person51.getName().getBytes());
file.writeInt(person51.getAge());
file.write(person52.getName().getBytes());
file.writeInt(person52.getAge());
file.write(person53.getName().getBytes());
file.writeInt(person53.getAge());
file.close();
RandomAccessFile file1 = new RandomAccessFile("RandomAccessFile.txt","r");
file1.skipBytes(12);
byte[] b = new byte[8];
file1.read(b, 0, b.length);
System.out.println(new String(b).trim());
System.out.println(file1.readInt());
file1.read(b, 0, b.length);
System.out.println(new String(b).trim());
System.out.println(file1.readInt());
file1.seek(0);
file1.read(b, 0, b.length);
System.out.println(new String(b).trim());
System.out.println(file1.readInt());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
class Person5 {
private String name;
private int age;
private static final int NAME_LEN = 8;
public Person5(String name, int age) {
super();
if(name.length()>NAME_LEN)
this.name = name.substring(0, 8);
else{
this.name = name;
for(int i=0;i<NAME_LEN - name.length();i++){
this.name = this.name + "\0";
}
}
System.out.println(name.length() + " " + this.name.length());
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static int getNameLen() {
return NAME_LEN;
}
}
- JavaIO之RandomAccessFile(一)
- 黑马程序员--javaIO 之RandomAccessFile
- javaIO RandomAccessFile
- JavaIO之ObjectStream(一)
- JavaIO之管道(一)
- JavaIO之ByteArrayStream(一)
- JavaIO之RandomAccessFile随机访问文件
- JavaIO之创建文件(一)
- JavaIO之标准输入输出(一)
- JavaIO之输入输出流链(一)
- javaIO-RandomAccessFile类
- JavaIO(一)
- JavaIO总结(一)
- JavaIO流(一)
- 黑马程序员之JAVAIO(一)
- RandomAccessFile(一)
- JavaIO简单摘要(一)
- 学习笔记之JavaIO中的文件处理——File和RandomAccessFile
- oracle decode
- Rotate List
- 初识python
- perl脚本自动化控制之android模拟器
- 编辑单个CPP文件,出现1>LIBCMTD.lib(crt0.obj) : error LNK2019: 无法解析的外部符号 _main,该符号在函
- JavaIO之RandomAccessFile(一)
- Android的四大组件之ContentProvider总结
- 堆排序(最小堆)
- 你不可以不知道的findViewById
- javascript 显示和隐藏控制
- Android实现三级联动下拉框 下拉列表spinner
- 关于用FTPClient资源库上传下载以中文命名的文件的问题
- 关于android中chronometer的问题
- dp-poj2229