0

ファイルを開くのに問題が発生しています。さて、私はすでにファイルがどこにあるかを知るために絶対パスを使用しましたが、それでもファイルを開くことができません(ファイルが見つかりません)

    public void ReadFromFile() throws FileNotFoundException
 {
       /** Read the contents of the given file. */


         String SourceID = new String();
         String LogicalID = new String();

         File fileDir = getFilesDir();
         String s = new String();

         s+=fileDir.getAbsolutePath()+"/Nodes.txt";
         Scanner scanner = new Scanner(new FileInputStream(s));


         try 
         { 
            while (scanner.hasNextLine())  
            { 

                SourceID = scanner.nextLine();  
                LogicalID = scanner.nextLine();  
                String ss = new String();
                ss+="    ----------------> "+SourceID+" "+LogicalID+"   ";

                Log.v(TAG, ss);
                ListaNodesSTART.add(new NodesToStart(SourceID,LogicalID));
            } 
         }catch(Exception ee){//Log.v(TAG, "Could not read the file");  
             ERROR.setText("Could Not Read file Nodes.txt");
         ErRorLog.setText("Could Not Read file Nodes.txt");}

         finally{scanner.close(); }
     }

問題はデバイスにファイルがないことだと思いますが、アプリの起動時にファイルをアップロードするにはどうすればよいですか?

前もって感謝します

4

1 に答える 1

0

あなたが言ったように、あなたはデバイスにファイルを持っていません!静的ファイルを操作するには、それらをアセットフォルダーに挿入してから、次の手順を実行します。

AssetManager assetManager = getAssets();
String[] files = null;
try {
    files = assetManager.list("");
} catch (IOException e) {
    Log.d("tag", e.getMessage());
}
for(String filename : files) { 
    if( filename.equals("Nodes.txt") {
        InputStream in = null;
        try {
            // Do your work with file
            in = assetManager.open(filename);
            // ...
        } catch(Exception e) {
            Log.e("tag", e.getMessage());
        }
    }
}
于 2012-06-04T15:51:26.887 に答える