2012-06-04 24 views
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(); } 
    } 

我想這個問題是該設備還沒有文件,但如何我可以在應用程序啓動時上傳它嗎?

在此先感謝

+0

你有沒有在你的manifest.xml給這個許可使用許可權的android:NAME =「android.permission.WRITE_EXTERNAL_STORAGE」 /> – Aerrow

+0

是我但它沒有找到它: 06-04 16:50:36.674:W/System.err(24897):java.io.FileNotFoundException:/data/data/neves.ProjectNodes/files/Nodes.txt(No such文件或目錄) –

回答

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()); 
     } 
    } 
} 
+0

它工作的人;)非常感謝..另外一件事..對不起我是一個noob ..: -/hod可以關閉一個線程在這裏?對於這個愚蠢的問題感到抱歉 –

+0

關閉InputStream更好,但這裏沒有線程!如果這對你有用,請不要忘記接受這個答案,以幫助其他人找到正確的解決方案! –

+0

是的,我知道,但我在討論論壇中的帖子。我已經看到一些封閉。這就是爲什麼我想知道我是否必須這樣做,以及如何 –