3

特定のディレクトリにファイルを作成しようとしましたが、FileNotFoundエラーが表示されます。なんで?不可能な道を使っていますか?本当にわかりませんが、コードは機能しているはずです。

    String day=/1;
String zn="/zn";
    File_name=zn
String root= Environment.getExternalStorageDirectory().toString();            
    File_path=root+day;

        File file1 = new File(File_path,File_name);
        file1.mkdirs();
        if(!file1.exists()) {
            try {
                file1.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 


        try {
            OutputStream fos= new FileOutputStream(file1);
            String l,d,p;
            l = lessnum.getText().toString();
            d = desc.getText().toString();
            p = place.getText().toString();

            fos.write(l.getBytes());
            fos.write(d.getBytes());
            fos.write(p.getBytes());

            fos.close();
4

5 に答える 5

1

SDカードにファイルを作成するためのコードを変更してください

String root= Environment.getExternalStorageDirectory().getAbsolutePath();
String File_name = "File_name.Any_file_Extension(like txt,png etc)";


File file1 = new File(root+ File.separator + File_name);
if(!file1.exists()) {
    try {
         file1.createNewFile();
       } catch (IOException e) {
          e.printStackTrace();
      }
} 

現在、ファイル名のファイル拡張子も欠落しているため、文字列znを次のように変更しますzn="/zn.txt";

に Sd カードのアクセス許可を追加したことを確認しますAndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2012-12-03T11:25:43.930 に答える
1

まず、ディレクトリを作成します

String root= Environment.getExternalStorageDirectory().toString();      
 String dirName =
     root+ "abc/123/xy"; 
     File newFile =  new File(dirName);
     newFile.mkdirs();

次に、そのディレクトリ内にファイルを作成します

String testFile = "test.txt"; File file1 = new File(dirName,testFile); if(!file1.exists()){ try { file1.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

次に、ファイル書き込み操作を行います

try { OutputStream fos= new FileOutputStream(file1);

文字列 l、d、p; l = lessnum.getText().toString(); d = desc.getText().toString(); p = place.getText().toString(); os.write(l.getBytes()); fos.write(d.getBytes()); fos.write(p.getBytes()); fos.close(); } catch (Exception e) { // TODO 自動生成された catch ブロック e.printStackTrace(); }

私はこれがあなたを助けると思う...

ありがとう...

于 2012-12-04T03:42:57.923 に答える
0

以下の行をマニフェストに追加して、アプリにSDカードへの書き込みを許可する正しい権限を与える必要があります。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

そして、http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29を確認してください

于 2012-12-03T11:16:16.053 に答える
0
String root= Environment.getExternalStorageDirectory().toString();      
     String dirName =
         root+ "abc/123/xy"; 
         File newFile =  new File(dirName);
         newFile.mkdirs();

         String testFile = "test.txt";
         File file1 = new File(dirName,testFile);
        if(!file1.exists()){
             try {
                file1.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

そして<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> マニフェストファイルで...

ありがとう...

于 2012-12-03T11:31:42.230 に答える
0

これがあなたの最新の試みです:

File_path = root + File.separator + day; 
File f_dir = new File(File_path); 
f_dir.mkdirs(); 
File file1 = new File(f_dir, File_name); 
if (!file1.exists()) { 
    try { 
        file1.createNewFile(); 
    } catch (IOException e) {
        e.printStackTrace(); 
    } 
}
try { 
    OutputStream fos= new FileOutputStream(file1); 

完全なスタック トレースとエラー メッセージを表示していただければ、何が問題なのかを簡単に特定できますが、いくつかの可能性が考えられます。

  1. によって返された値をチェックしていないため、ディレクトリ パスが作成されなかったことを示す値が返されf_dir.mkdirs()ている可能性があります。falseこれは、次のことを意味する可能性があります。
    • ディレクトリはすでに存在していました。
    • 何かが存在しましたが、それはディレクトリではありませんでした。
    • ディレクトリ パスの一部を作成できませんでした ... 考えられる理由の 1 つです。
  2. オブジェクトによって指定されたパス名を持つものが存在する場合、呼び出しfile1.exists()は戻ります。何かが存在するという事実は、必ずしも書き込みのために開くことができるという意味ではありません。 true
    • ディレクトリである可能性があります。
    • アプリケーションが書き込み権限を持っていないファイルである可能性があります。
    • 読み取り専用ファイル システム上のファイルである可能性があります。
    • そして、他のいくつかのこと。

これを書くとしたら、次のように書きます。

File dir = new File(new File(root), day);
if (!dir.exists()) {
    if (!dir.mkdirs()) {
        System.err.println("Cannot create directories");
        return;
    }
}
File file1 = new File(dir, fileName);
try (OutputStream fos= new FileOutputStream(file1)) {
    ...
} catch (FileNotFoundException ex) {
   System.err.println("Cannot open file: " + ex.getMessage());
}

必要な場合にのみディレクトリの作成を試み、作成が成功したことを確認します。次に、ファイルを開いて書き込みを試みます。ファイルが存在しない場合は作成されます。作成できない場合は、FileNotFoundException メッセージで理由が説明されます。

変数名の選択で発生したスタイル エラーも修正したことに注意してください。

于 2012-12-03T13:10:16.773 に答える