诚心求教java中随机文件读写的问题
往文件里用键盘输入了一些字符,长度不一定,但在查询时,输出来的是乱码,用的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 File
NotFoundException//从文件读出
{
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){}
}
请各位帮忙看一下,该怎么读出呢?谢谢啦,万分感激阿!!!
------解决方案--------------------public class BufferedReaderTest
{
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();
}
}
}