2013-07-03 2 views
0

SD 카드에 파일을 쓰는 코드입니다.여러 파일 이름을 가진 SD 카드에 같은 파일 저장

try { 
     File file = Environment.getExternalStorageDirectory(); 
     File myFile = new File(file, "sample.txt"); 
     myFile.createNewFile(); 
     FileOutputStream fOut = new FileOutputStream(myFile); 
     OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
     myOutWriter.append(Globals.obj.toString()); 
     myOutWriter.close(); 
     fOut.close(); 
     Toast.makeText(getApplicationContext(), "Written successfully", 
       Toast.LENGTH_SHORT).show(); 
    } catch (Exception e) { 
     Toast.makeText(getApplicationContext(), e.getMessage(), 
       Toast.LENGTH_SHORT).show(); 
    } 

여기 'sample.txt'는 SD 카드에 저장되는 파일입니다. 사용자가 EditText 값을 입력하고 Button 클릭시 카드에 저장됩니다. 다른 사용자가오고 그의 내용은 'sample1.txt '로 저장되고 다른 사용자는 'sample2.txt', 'sample3.txt'(증분 순서) 등으로 저장됩니다. 아무도이 작업 방법을 알려 줄 수 있습니까? ??

File file = getContext().getFileStreamPath(FILE_NAME); 
if(file.exists()){ 
... 
} 

또는

File sdCardRoot = Environment.getExternalStorageDirectory(); 
File yourDir = new File(sdCardRoot, "yourpath"); 
for (File f : yourDir.listFiles()) { 
    if (f.isFile()) 
     String name = f.getName(); 
     // substr the name to find the last digits 
} 

당신은 파일이 존재하는지 확인하고 해당 번호를 추가 할 수 있습니다 :

답변

1

이 방법을 사용해보십시오.

+0

감사합니다. 더 많은 코드를 진행할 수 있습니까? – DroidLearner

+0

먼저 시도해보십시오. 그럼 어떤 이유로 당신이 계속할 수 없다면, 당신이 쓴 것을 알려주십시오. 우리는 무언가에 대해 생각할 것입니다 ;-) – g00dy

+0

나는이 http://pastebin.com/hAaZTyKm과 같은 것을 시도했고 두 파일 모두에서 동일한 텍스트가 저장되었습니다 . – DroidLearner

관련 문제