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

As you mentioned, you have NO file in the device! To work with static files, insert them in your assets folder and then:

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 回答