import static org.junit.Assert.*;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
public class TestCase {
/***
* 电话薄
* id name phone Email time
* …… …… …… …… ……
* 设计文件-----定长记录结构
* start = 372*rowNo
* 0 0 …… …… …… 371 第1条数据
* 1 372 …… …… …… 372*2-1 第2条数据
* 2 372*2 …… …… …… 372*3-1 第3条数据
* 3 372*3 …… …… …… 372*4-1 第4条数据
* ………………………………………………………………………………………………………………
* 0~3 4个byte id编号
* 4~35 32个byte name姓名
* 36~67 32个byte phone电话
* 68~148 80个byte email邮箱
* 148~ 8个byte time时间
* ……………………………………………………………………………………………………………………
* 两个算法:
* 写入地址簿记录
* void writeContect(int id,String name,String phone,
* String email,Date time,RandomAccessFile file,int rowNo){}
* 读取地址簿记录
* Map<String,Object> readContect(RandomAccessFile file,int rowNo){}
*/
@Test
public void test() throws IOException {
/** rw:打开文件,如果文件不存在,自动创建,如果文件存在就改写文件 */
/** RandomAccessFile,建立文件通道,指针的位置为0 */
RandomAccessFile raf =
new RandomAccessFile("contect.dat","rw");
/** 在contect.dat 文件中写内容 */
writeFile(1,"张三","110","zhangsan@tom.com",new Date(),raf,0);
writeFile(2,"王五","119","wangwu@tom.com",new Date(),raf,1);
writeFile(3,"李四","120","李四@tom.com",new Date(),raf,3);
/** 在contect.dat 文件中读取指定行的内容 */
Map<String,Object> contect = readFile(raf,1);
System.out.println(contect);
raf.close();
}
/** 写出联系方式 */
/** id(编号) name(名字) phone(电话) email(邮箱) file(文件) rowNo(写在哪行)
* @throws IOException */
private void writeFile(int id, String name, String phone,
String email,Date time, RandomAccessFile file, int rowNo) throws IOException {
long start = rowNo*372L;
byte[] buf = new byte[372];
file.seek(start);
file.read(buf);//指定的行清空
file.seek(start+0);
file.writeInt(id);//写id---int有4个字节,文件是以字节为单位。
file.seek(start+4);
buf = name.getBytes();//获取name的字节数
file.write(buf);//写name
file.seek(start+36);
buf = phone.getBytes();//获取phone字节
file.write(buf);//写phone
file.seek(start+68);
buf = email.getBytes();//获得email字节
file.write(buf);//写email
file.seek(start+148);
long l = time.getTime();//获得current time
file.writeLong(l);//写time
}
/** 读出 联系方式 */
/** file(文件) rowNo(读 哪行)*/
private Map<String, Object> readFile(RandomAccessFile file, int rowNo) throws IOException {
Map<String,Object> map =
new LinkedHashMap<String,Object>();
long start = rowNo*372L;//读该行
file.seek(start+0);
int id = file.readInt();//读id
file.seek(start+4);
byte[] buf = new byte[32];
file.read(buf);//读name
String name = new String(buf).trim();
file.seek(start+36);
file.read(buf);//读phone
String phone = new String(buf).trim();
file.seek(start+68);
buf = new byte[80];
file.read(buf);//读email
String email = new String(buf).trim();
file.seek(start+148);
long l = file.readLong();//读time
SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd");//时间输出格式
Date date = new Date(l);
String time = sdf.format(date);
map.put("id",id);
map.put("name",name);
map.put("phone", phone);
map.put("email", email);
map.put("time", time);
return map;
}
}
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
public class TestCase {
/***
* 电话薄
* id name phone Email time
* …… …… …… …… ……
* 设计文件-----定长记录结构
* start = 372*rowNo
* 0 0 …… …… …… 371 第1条数据
* 1 372 …… …… …… 372*2-1 第2条数据
* 2 372*2 …… …… …… 372*3-1 第3条数据
* 3 372*3 …… …… …… 372*4-1 第4条数据
* ………………………………………………………………………………………………………………
* 0~3 4个byte id编号
* 4~35 32个byte name姓名
* 36~67 32个byte phone电话
* 68~148 80个byte email邮箱
* 148~ 8个byte time时间
* ……………………………………………………………………………………………………………………
* 两个算法:
* 写入地址簿记录
* void writeContect(int id,String name,String phone,
* String email,Date time,RandomAccessFile file,int rowNo){}
* 读取地址簿记录
* Map<String,Object> readContect(RandomAccessFile file,int rowNo){}
*/
@Test
public void test() throws IOException {
/** rw:打开文件,如果文件不存在,自动创建,如果文件存在就改写文件 */
/** RandomAccessFile,建立文件通道,指针的位置为0 */
RandomAccessFile raf =
new RandomAccessFile("contect.dat","rw");
/** 在contect.dat 文件中写内容 */
writeFile(1,"张三","110","zhangsan@tom.com",new Date(),raf,0);
writeFile(2,"王五","119","wangwu@tom.com",new Date(),raf,1);
writeFile(3,"李四","120","李四@tom.com",new Date(),raf,3);
/** 在contect.dat 文件中读取指定行的内容 */
Map<String,Object> contect = readFile(raf,1);
System.out.println(contect);
raf.close();
}
/** 写出联系方式 */
/** id(编号) name(名字) phone(电话) email(邮箱) file(文件) rowNo(写在哪行)
* @throws IOException */
private void writeFile(int id, String name, String phone,
String email,Date time, RandomAccessFile file, int rowNo) throws IOException {
long start = rowNo*372L;
byte[] buf = new byte[372];
file.seek(start);
file.read(buf);//指定的行清空
file.seek(start+0);
file.writeInt(id);//写id---int有4个字节,文件是以字节为单位。
file.seek(start+4);
buf = name.getBytes();//获取name的字节数
file.write(buf);//写name
file.seek(start+36);
buf = phone.getBytes();//获取phone字节
file.write(buf);//写phone
file.seek(start+68);
buf = email.getBytes();//获得email字节
file.write(buf);//写email
file.seek(start+148);
long l = time.getTime();//获得current time
file.writeLong(l);//写time
}
/** 读出 联系方式 */
/** file(文件) rowNo(读 哪行)*/
private Map<String, Object> readFile(RandomAccessFile file, int rowNo) throws IOException {
Map<String,Object> map =
new LinkedHashMap<String,Object>();
long start = rowNo*372L;//读该行
file.seek(start+0);
int id = file.readInt();//读id
file.seek(start+4);
byte[] buf = new byte[32];
file.read(buf);//读name
String name = new String(buf).trim();
file.seek(start+36);
file.read(buf);//读phone
String phone = new String(buf).trim();
file.seek(start+68);
buf = new byte[80];
file.read(buf);//读email
String email = new String(buf).trim();
file.seek(start+148);
long l = file.readLong();//读time
SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd");//时间输出格式
Date date = new Date(l);
String time = sdf.format(date);
map.put("id",id);
map.put("name",name);
map.put("phone", phone);
map.put("email", email);
map.put("time", time);
return map;
}
}