안녕하세요~! 제가 리스트뷰의 데이터들을 txt파일에 저장하는 예제를 공부하던 도중 문제점이 생겨
질문을 드립니다.
예를 들자면 txt파일에 1,2,3,4,5 의 내용이 나와야 하는데 5,5,5,5,5가 나오는 상황이 생겨 해결방법을 찾지 못해 도움을 요청하고자 글을 씁니다. 고수님들의 많은 조언 부탁드립니다! 해당 소스는 아래에 써 두었습니다.
public boolean saveFile(Context context, String fileName) {
// check if available and not read only
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
Log.w("FileUtils", "Storage not available or read only");
return false;
}
// Create a path where we will place our List of objects on external storage
File file = new File(context.getExternalFilesDir(null), fileName);
PrintStream p = null; // declare a print stream object
boolean success = false;
String item_content1 = cursor.getString(cursor.getColumnIndex(ClothAdapter.KEY_CONTENT1));
String item_content2 = cursor.getString(cursor.getColumnIndex(ClothAdapter.KEY_CONTENT2));
String item_content3 = cursor.getString(cursor.getColumnIndex(ClothAdapter.KEY_CONTENT3));
String item_content4 = cursor.getString(cursor.getColumnIndex(ClothAdapter.KEY_CONTENT4));
String item_content5 = cursor.getString(cursor.getColumnIndex(ClothAdapter.KEY_CONTENT5));
try {
OutputStream os = new FileOutputStream(file);
// Connect print stream to the output stream
p = new PrintStream(os);
for (int i=0; i < cursor.getCount(); i++) {
//p.println("Date : "+item_content1+" Product : "+item_content2+" Inproduct : "+item_content3+" Outproduct : "+item_content4+" Saveproduct : "+item_content5);
}
p.println(item_content1+" Product : "+item_content2+" Inproduct : "+item_content3+" Outproduct : "+item_content4+" Saveproduct : "+item_content5);
Log.w("FileUtils", "Writing file" + file);
success = true;
} catch (IOException e) {
Log.w("FileUtils", "Error writing " + file, e);
} catch (Exception e) {
Log.w("FileUtils", "Failed to save file", e);
} finally {
try {
if (null != p)
p.close();
} catch (Exception ex) {
}
}
return success;
}