2011-04-01 1 views
1

작업을 위해 파일을 사용해야합니다. 처음 64 바이트는 헤더 여야합니다. 그래서 처음 64 바이트를 읽은 다음 파일의 끝까지 읽어야합니다. J2ME으로 파일을 읽고 쓰려면 어떻게해야합니까? 이다처음 64 바이트 이후에 나머지 파일을 읽으려면 어떻게해야합니까?

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(); 

답변

1

는 전체 파일을 읽기 하기.

// 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()); 
+0

그건 좋은 생각이야. 고맙습니다. 하지만 어떻게 64 바이트 후에 쓸 수 있을까? –

+0

자세한 정보가 포함 된 답변을 편집했습니다. 유용하다고 생각하시면 받아주세요. –

0

사용 : 당신은 같은

DataInputStream.skipBytes() 
+0

고맙습니다. 그리고 내가 처음 64 바이트 만 읽고 쓸 수 있다면 어떻게 읽을 수 있습니까? –

관련 문제