0

これは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」(増分順)などとして保存されます..誰か私にこれを行う方法を教えてもらえますか??

4

1 に答える 1

1

このアプローチを試してください:

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
}

ファイルが存在するかどうかを確認し、対応する番号を追加できます。

于 2013-07-03T06:05:49.077 に答える