2013-06-04 7 views
0

아래에 언급 된 코드에서 "testfile.txt"라는 파일에 더미 내용을 쓰고 있습니다. 하지만 외부 저장소의 파일 중 하나에 더미 내용을 쓰려고합니다. 나는 파일 이름을 하드 코딩하지 않기를 바란다. 어떻게해야합니까?외부 저장소의 파일에 더미 내용 쓰기 android

String root = android.os.Environment.getExternalStorageDirectory().getPath(); 
File myFile = new File(root,"testfile.txt"); 

FileChannel rwChannel = new RandomAccessFile(myFile, "rw").getChannel(); 
int numBytes = (int)rwChannel.size(); 
ByteBuffer buffer = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, numBytes); 
System.out.println("buffer"+buffer); 
byte[] randomBytes = new byte[numBytes]; 
new Random().nextBytes(randomBytes); 
buffer.put(randomBytes); 
rwChannel.write(buffer); 
rwChannel.close(); 

답변

0
File root = android.os.Environment.getExternalStorageDirectory(); 
File[] files = root.listFiles(); 
for (File f : files) { 
    if (f.isDirectory()) 
     continue; 
    //write here 
} 
+0

나는 파일에 변경 유형의 파일로 RandomAccessFile의에서 오류가 발생하고있다. File [] files = root.listFiles(); for (File f : files) {if (f.isDirectory()) \t 계속; FileChannel rwChannel = 새로운 RandomAccessFile (파일, "rw"). getChannel(); – user2435558