2012-07-19 2 views
0

내부 저장소 파일에서 특정 데이터를 읽는 방법. 예 :. , 나는 다음 1. 장치 2. 시간 (신기원 형식) 3. 단추 텍스트내부 저장소 파일 (Android)에서 읽기

  CharSequence cs =((Button) v).getText(); 
      t = System.currentTimeMillis()/1000; 
    s = cs.toString(); 
    buf = (t+"\n").getBytes(); 
    buf1 = (s+"\n").getBytes(); 


    try { 
     FileOutputStream fos = openFileOutput(Filename, Context.MODE_APPEND); 
     fos.write("DVD".getBytes()); 
     fos.write(tab.getBytes()); 
     fos.write(buf); 
     fos.write(tab.getBytes()); 
     fos.write(buf1); 
     //fos.write(tab.getBytes()); 
     //fos.write((R.id.bSix+"\n").getBytes()); 
     fos.write(newline.getBytes()); 
     //fos.flush(); 
     fos.close(); 

    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

을 저장 한 읽는 동안, 우리는 어떻게 파일에서만 가격을 읽을 수 있습니까? (fos.read를 사용하여())

감사

+0

자세한 내용을 추가하십시오. 장치, 시간 및 가격을 어떻게 "저장"했습니까? 몇 가지 코드를 보여주십시오. –

+0

수정 된 게시물보기 – user1468534

답변

0
나는 다음과 같이 좀 더 구조화 된 방법으로 파일을 작성 제안

:

long t = System.currentTimeMillis()/1000; 
String s = ((Button) v).getText(); 
DataOutputStream dos = null; 
try { 
    dos = new DataOutputStream(openFileOutput(Filename, Context.MODE_APPEND)); 
    dos.writeUTF("DVD"); 
    dos.writeLong(t); // Write time 
    dos.writeUTF(s); // Write button text 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} finally { 
    if (dos != null) { 
     try { 
      dos.close(); 
     } catch (IOException e) { 
      // Ignore 
     } 
    } 
} 

이 같은 뭔가 그것을 다시 읽으려면 :

DataInputStream dis = null; 
try { 
    dis = new DataInputStream(openFileInput(Filename)); 
    String dvd = dis.readUTF(); 
    long time = dis.readLong(); 
    String buttonText = dis.readUTF(); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} finally { 
    if (dis != null) { 
     try { 
      dis.close(); 
     } catch (IOException e) { 
      // Ignore 
     } 
    } 
}