추가, 삭제 가능한 리스트뷰의 아이템(스트링으로 된 이름)들을
저장하고 불러오기를 하려고 하는데요.
파일저장 하는 부분을 찾아보아도 마땅한 예제가 없네요.
프레퍼런스, DB 사용안하고 txt 파일로 저장하고 다시 불러와서 리스트뷰에 뿌리고 싶은데
감을 전혀 못잡고 있습니다.
도와주세요.
저처럼 이것때문에 고생했던 분들이 혹시라고 계실가봐 소스 올립니다.
별거 아니지만 자바를 모르고 해서 정말 힘들었네요. ㅠ.ㅜ
1. 저장함수
final static String FILE_NAME = "test.txt";
private void saveFunc()
{
adapter.notifyDataSetChanged();
try {
FileOutputStream fos = openFileOutput(FILE_NAME, Context.MODE_WORLD_READABLE);
String str = "";
if(list.getCount() != 0) {
for(int i=0; i<list.getCount(); i++) {
str += (String)list.getItemAtPosition(i) + ","; // "," 부호를 끼워 넣어서 저장
}
}
fos.write(str.getBytes());
fos.close();
Toast.makeText(MemberActivity.this,"저장완료",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.getMessage();
}
}
2. 불러오는 함수
private void loadFunc()
{
try {
FileInputStream fis = openFileInput(FILE_NAME);
items = new ArrayList<String>();
byte[] data = new byte[fis.available()];
while(fis.read(data)!= -1) {;}
fis.close();
String strData = new String(data);
String[] strAry = strData.split(",", 0); // "," 부호로 구분하여 다시 분리함.
for(int i=0; i<strAry.length; i++) {
items.add(strAry[i]);
}
} catch (FileNotFoundException e) {
Toast.makeText(MemberActivity.this,"File Not Found",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.getMessage();
}
}




큰 데이터가 아니라면 View 객체의 setTag(Object o)라는 함수를 통해 뷰에 오브젝트를 저장할 수 있습니다.