티스토리 뷰

반응형

안드로이드에서만 지원하는 파일 관련 함수들이 있다.

 

 

위 함수를 사용해 파일 입/출력을 수행한 예시코드는 다음과 같다.

// 파일쓰기
try {
	FileOutputStream fosMemo = mContext.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
    fosMemo.write(strData.getBytese());
    fosMemo.close();
}
catch(Exception e) {
	e.printStackTrace();
}


// 파일읽기
try {
	FileInputStream fisMemo = mContect.openFileInput(FILE_NAME);
    byte[] memoData = new byte[fisMemo.available()];
    
    while(fisMemo.read(memoData) != -1) {}
    fisMemo.close();
}


// 파일삭제
mContext.deleteFile(FILE_NAME);

 

 

 


객체 자체로 읽기 또는 저장

public void save(HashMap<String String> objData) {
	ObjectOutputStream oos = null;
    FileOutputStream fos = null;
    
    try {
    	fos = mContext.openFileOutput(FILE_NAME, MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(objData);
        oos.close();
    }
    catch(Exception e) {
    	e.printStackTrace();
    }
}

public HashMap<String, String> load() {
	try {
    	FileInputStream fis = mContext.openFileInput(FILE_NAME);
        ObjectInputStream ois = new ObjectInputStream(fis);
        
        HashMap<String, String> memoData = null;
        memoData = (HashData<String, String>)ois.readObject();
        
        ois.close();
        
        return memoData;
    }
    catch(Exception e) {
    	e.printStackTrace();
    }
}

 

반응형

'Android > Concept' 카테고리의 다른 글

[Android] SQLite Database  (0) 2020.12.04
[Android] Shared Preference  (0) 2020.12.04
[Android] Remote Bound Service (AIDL 사용)  (0) 2020.11.30
[Android] Local Broadcast  (0) 2020.11.29
[Android] Broadcast Receiver 의 ANR  (0) 2020.11.25
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함