-1

現在、これは私のコードです:

    private void writeToSDFile(){

        File root = android.os.Environment.getExternalStorageDirectory(); 
        File dir = new File (root.getAbsolutePath() + "/download");
        dir.mkdirs();
        File file = new File(dir, "myData.txt");


        try {
            FileOutputStream f = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(f);
            pw.println(d8 + d7 + ":" + d6 + d5 + ":" + d4 + d3 + ":" + d2 + d1);
            pw.flush();
            pw.close();
            f.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.i(TAG, "******* File not found. Did you" +
                    " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }

基本的に、存在しない場合にのみファイル/ディレクトリを作成する方法があるかどうか(おそらくifステートメントを使用して)知りたいです。現時点では、 writetoSDCard() が呼び出されるたびに両方を再作成します。

私がやりたいのは、ファイルの最後にデータを追加し続けることだけです。参考までに、次のように保存されます: 00:00:00:00

ありがとう :)

また、私がここにいる間、簡単な質問ですが、プログラムが txt ドキュメントから 2 桁の数字のリストを読み取り、それらを加算する方法はありますか? 例: 20 20 30 画面上の結果は 70 ですか? 私は本当にこのようなものを見つけていません:/

4

3 に答える 3

3

これを試して

String FOLDERNAME="/download";
File dir = new File(Environment.getExternalStorageDirectory(), FOLDERNAME);
if(!dir.exists())
{
dir.mkdir();
}

コピーと過去

private void writeToSDFile(){

        String FOLDERNAME="/download";
        File dir = new File(Environment.getExternalStorageDirectory(), FOLDERNAME);
        if(!dir.exists())
        {
        dir.mkdir();
        }

    File file = new File(dir, "myData.txt");


    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println(d8 + d7 + ":" + d6 + d5 + ":" + d4 + d3 + ":" + d2 + d1);
        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found. Did you" +
                " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }   
}

/////////////////////////////////////////////// /////////////////// ファイルの編集

   File f;
  f=new File(dir, "myData.txt");
  if(!f.exists())
  {
  f.createNewFile();
  }
于 2012-11-22T05:19:43.960 に答える
1
String path=Environment.getExternalStorageDirectory()+"/dirpath";
File myDirectory = new File(path);
    if(!myDirectory.isDirectory()){
        myDirectory.mkdirs();
        }
于 2012-11-22T05:19:59.817 に答える
1

exists()メソッド forを呼び出して確認するか、またはdir/fileのように特定の場所に移動できます。isDirectory()isFile()

File dir = new File (root.getAbsolutePath() + "/download");
if(!dir.exists())
        dir.mkdirs();

or 

if(!dir.isDirectory())
        dir.mkdirs();

ファイルに対してもできるのと同じ方法

于 2012-11-22T05:21:29.837 に答える