往文件里用键盘输入了一些字符,长度不一定,但在查询时,输出来的是乱码,用的RandomAccessFile类,不明白读出时指针该怎么定位
写入的程序
void filewrite() throws FileNotFoundException//写入文件
{ File fileName=new File("D:\\Student.txt");
RandomAccessFile file=new RandomAccessFile(fileName,"rw");
try{
long q=file.length();
file.seek( q);
file.writeUTF("学号:"+number+" ");
file.writeUTF("姓名:"+name+" ");
file.writeUTF("性别:"+sex+" ");
file.writeUTF("年龄:"+age+" ");
file.writeUTF("班级:"+clas+" ");
file.writeUTF("专业:"+major+" ");
file.writeUTF("学院:"+academic+" "+"\r\n");
}catch (IOException e){}
}
从文件中读出的程序是
void fileread(int a) throws FileNotFoundException//从文件读出
{File fileName=new File("D:\\Student.txt");
RandomAccessFile file=new RandomAccessFile(fileName,"rw");
String s=null;
long p=0;
try{
for(int i=0;i<a;i++)
{
s=file.readLine();
p=file.getFilePointer();
file.seek(p);
}
System.out.println(s);
}catch (IOException e){}
}
请各位帮忙看一下,该怎么读出呢?谢谢啦,万分感激阿!!!
写入的程序
void filewrite() throws FileNotFoundException//写入文件
{ File fileName=new File("D:\\Student.txt");
RandomAccessFile file=new RandomAccessFile(fileName,"rw");
try{
long q=file.length();
file.seek( q);
file.writeUTF("学号:"+number+" ");
file.writeUTF("姓名:"+name+" ");
file.writeUTF("性别:"+sex+" ");
file.writeUTF("年龄:"+age+" ");
file.writeUTF("班级:"+clas+" ");
file.writeUTF("专业:"+major+" ");
file.writeUTF("学院:"+academic+" "+"\r\n");
}catch (IOException e){}
}
从文件中读出的程序是
void fileread(int a) throws FileNotFoundException//从文件读出
{File fileName=new File("D:\\Student.txt");
RandomAccessFile file=new RandomAccessFile(fileName,"rw");
String s=null;
long p=0;
try{
for(int i=0;i<a;i++)
{
s=file.readLine();
p=file.getFilePointer();
file.seek(p);
}
System.out.println(s);
}catch (IOException e){}
}
请各位帮忙看一下,该怎么读出呢?谢谢啦,万分感激阿!!!
不仅程序难以理解,而且直接打开也很难看懂.
可以将每个学生的信息保存成一行,然后每部分信息之间用分隔符号隔开,分隔符号可以使用不使用的特殊字符比如";",这样文本文件的内容如下:
学号;姓名;性别;......
存取的时候可以使用readLine和writeLine方法.
本来就不适合在这种记录长度不等的情况下用
这样根本不能正确读取要不你把每条记录弄成一样长
要不别用RandomAccessFile
如果我在输入时输入长度都一样行吗?还是得在程序里就得限定他们的长度?
比如int+20byte的姓名+..................你还是用xml 然后用jdom处理比较方便
{
public static void main(String[] args)
{
/**
* test.dat文件内容
* 学号:24244|姓名:大师父是地方|性别:不男不女|年龄:shouyutianqi|班级:的发生地发生|专业:道德法|学院:是地方的是
* 学号:213|姓名:大师地方税阿多少方|性别:不男不女|年龄:shouyutianqi|班级:的阿房地方的是生地发生|专业:对手德法|学院:是地方地方税的是
* 学号:24212344|姓名:大第三丰富的是方|性别:不男不女|年龄:shouyutianqi|班级:的发生斯蒂芬发生|专业:道对手法|学院:是地哈哈的是
* 学号:213321|姓名:大师的是非得失是地方|性别:不男不女|年龄:shouyutianqi|班级:的斯蒂芬生地发生|专业:斯蒂芬德法|学院:是地宏观的是
*/
try
{
BufferedReader reader = new BufferedReader(new FileReader("C:\\test.dat"));
String readline = "";
while((readline = reader.readLine()) != null)
{
System.out.println(readline);
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
不是,我是说用BufferedReader从键盘读到程序中的只有一个人的信息呀 ,还是有别的办法?
“xml jdom”我不懂啊,呵呵,是什么呀?谢谢阿
String s = null;
byte[] buf = new byte[1028];
long p = 0;
try {
// for (int i = 0; i < a; i++) {
while(p < file.length()){
int len = file.read(buf);
p = file.getFilePointer();
file.seek(p);
s = new String(buf,0,len,"utf-8");
System.out.println(s);
}