2013-04-17 3 views
0

이 코드를 사용하여 파일에 쓰고 있습니다.블랙 베리 파일 읽기/쓰기

 protected void writeFile(String text) { 
      DataOutputStream os = null; 
      FileConnection fconn = null; 
       try { 
        fconn = (FileConnection) Connector.open("file:///store/home/user/documents/file.txt", Connector.READ_WRITE); 
        if (!fconn.exists()) 
         fconn.create(); 
        os = fconn.openDataOutputStream(); 
        os.write(text.getBytes()); 
       } catch (IOException e) { 
        System.out.println(e.getMessage()); 
       } finally { 
        try { 
         if (null != os) 
          os.close(); 
         if (null != fconn) 
          fconn.close(); 
        } catch (IOException e) { 
         System.out.println(e.getMessage()); 
        } 
      }} 

코드가 정상적으로 작동합니다.

제가 처음으로 "Banglore"라고 쓰면 저의 문제는 제가 읽을 때 "Banglore"를 얻는 것입니다. 하지만 두 번째로 "인도"라고 쓰고 읽을 때 "Indialore"라고합니다. 그래서 기본적으로 내용은 텍스트에 따라 변경되지 않습니다. 해결 방법을 알려주십시오.

답변

3

파일을 쓰지 않아도 내용이 제거되지만 내용이 바뀌기 때문에 'Bangalore'위에 'india'를 쓰면 'Banga'가 'India'로 바뀌고 나머지는 동일하게 유지됩니다. 최신 콘텐츠로 이전 콘텐츠를 완전히 삭제하려면 truncate() 최신 데이터가 끝나는 곳의 파일이 필요합니다. truncate(text.getBytes().length)

+0

감사합니다. – user2218773

관련 문제