File io 在前64个字节之后,如何读取文件的其余部分?

File io 在前64个字节之后,如何读取文件的其余部分?,file-io,java-me,File Io,Java Me,我必须用档案来工作。第一个64字节必须是标头。所以我必须先读取64个字节,然后读取到文件的末尾。如何使用J2ME读写文件 完全读取文件是指: FileConnection fconn= (FileConnection) Connector.open(path+fName,Connector.READ); InputStream is=fconn.openInputStream(); DataInputStream dis = new DataInputStream(is); int ch; S

我必须用档案来工作。第一个64字节必须是标头。所以我必须先读取64个字节,然后读取到文件的末尾。如何使用J2ME读写文件

完全读取文件是指:

FileConnection fconn= (FileConnection) Connector.open(path+fName,Connector.READ);
InputStream is=fconn.openInputStream(); 
DataInputStream dis = new DataInputStream(is);
int ch;
String str = new String();
while ( (ch = dis.read()) != -1) 
{
       str+=((char)ch);
}
dis.close();
is.close();
完全写入文件是:

FileConnection fconn= (FileConnection)Connector.open(path+fName,Connector.READ_WRITE);
if(!fconn.exists()){
      items.alert=new Alert(" ","Select The Directory",null,AlertType.INFO);
      switchDisplayable(items.alert,items.getList());
      fconn.create(); 
}
os = fconn.openDataOutputStream();
fconn.truncate(0);
os.write(text.getBytes());
os.close(); 
fconn.close();
使用:

使用:


像现在这样阅读整个文件。
然后使用此选项拆分从文本读取的数据:

// Assuming `str` contains the data read from file
String header=str.substring(0,64);
String the_rest_of_file=str.substring(64);
如果您想更改文件中的某些内容,但不想更改头中的内容,那么在写入时实际上不必跳过64个字节。只需写入相同的标题,然后写入修改后的数据:

// Change `the_rest_of_file` somehow
// ...
os.write(header.getBytes());
os.write(the_rest_of_file.getBytes());

像现在这样阅读整个文件。
然后使用此选项拆分从文本读取的数据:

// Assuming `str` contains the data read from file
String header=str.substring(0,64);
String the_rest_of_file=str.substring(64);
如果您想更改文件中的某些内容,但不想更改头中的内容,那么在写入时实际上不必跳过64个字节。只需写入相同的标题,然后写入修改后的数据:

// Change `the_rest_of_file` somehow
// ...
os.write(header.getBytes());
os.write(the_rest_of_file.getBytes());

非常感谢。如果我只想读写第一个64字节,我怎么读呢?谢谢。如果我只想读写前64个字节,我怎么读呢?这是个好主意。非常感谢。但是怎么能在64字节后写呢?用更多的信息编辑了答案。如果你觉得有用的话,请接受它。这是个好主意。非常感谢。但是怎么能在64字节后写呢?用更多的信息编辑了答案。如果你觉得有用,请接受它