2014-10-13 13 views
7

đang gốc và làm việc của nó lưu dữ liệu vào thẻ nhớ SDOutputStreamWriter không gắn

// Writing data to internal storage 
btnSaveData.setOnClickListener(new View.OnClickListener() { 

@Override 
public void onClick(View v) { 
    if (isSDCardWritable()) { 
     String dataToSave = etData.getText().toString(); 
     try { 
     // SD Card Storage 
     File sdCard = Environment.getExternalStorageDirectory(); 
     File directory = new File(sdCard.getAbsolutePath()+"/MyFiles"); 
     directory.mkdirs(); 
     File file = new File(directory, "text.txt"); 
     FileOutputStream fos = new FileOutputStream(file); 
     OutputStreamWriter osw = new OutputStreamWriter(fos); 

     // write the string to the file 
     osw.write(dataToSave); 
     osw.flush(); 
     osw.close(); 
     . . . 

Và sau đó tôi đã thay đổi mã để nối thêm một giá trị mới như nó phải được theo những gì tôi cần:

  osw.append(dataToSave); 
      osw.flush(); 
      osw.close(); 

Sự cố là: Nó ghi đè tệp văn bản thay vì phụ thêm. Tôi đã bỏ lỡ cái gì? Cảm ơn bạn đã trợ giúp

Trả lời

16

Constructor FileOutputStream (Tệp tệp) luôn ghi đè tệp. Nếu bạn muốn nối thêm vào tập tin, bạn phải sử dụng hàm tạo FileOutputStream chung hơn (File file, boolean append). Nếu bạn đặt tham số 'chắp thêm' thành tệp thực thì không được ghi đè.

+2

cảm ơn bạn đã cứu tôi. bị bệnh chấp nhận câu trả lời thời điểm tôi có thể. cảm ơn lần nữa – LadyWinter

Các vấn đề liên quan