2016-04-30 9 views
0

ファイルから内容を読み取ろうとしていますが、ファイルが見つかりません。例外です。ファイルが見つかりません。ファイルの内容を読み取るための例外です。

私を助けてください。私は以下のコードを使用しています。

File f = new File("C:/sample/welcome.txt"); //working 
System.out.println("File path ="+f.getPath());        
System.out.println("File name = "+f.getName()); 
System.out.println("File size = "+f.length()); 
String ret = ""; 

HttpURLConnection connection = null; 

try { 

     int size = (int) f.getName().length(); 
     byte[] data = new byte[size]; 
     String _contrainerName1 ="/development"; 
     // String content = new String("This is my first upload to dropbox"); 
     //String fileName ="test17.txt"; 

     try { 
      InputStream inputStream = new FileInputStream(f.getName()); 

      if (inputStream != null) { 
      DataInputStream in1 = new DataInputStream(inputStream); 
      // InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in1)); 

      String receiveString = ""; 
      StringBuilder stringBuilder = new StringBuilder(); 

      while ((receiveString = bufferedReader.readLine()) != null) { 
        stringBuilder.append(receiveString); 
      } 

      inputStream.close(); 
      ret = stringBuilder.toString(); 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     System.out.println("login activity"); 

    } catch (IOException e) { 
     e.printStackTrace(); 
     System.out.println("login activity"); 
    } 
+0

私はあなたのコードフォーマットが壊れており、2つの部分に分割されていると思います。それをもう一度確認してください。 – zeromus

+1

これはAndroidとしてタグ付けされています。 Androidには 'C:'ドライブがありません。 – CommonsWare

+0

私はEclipseを使用して簡単なJavaアプリケーションとして実行しています。 – superfine123

答えて

1

あなたは、Windowsのファイルシステム内のファイルを読み込むしようとしている場合は、二重のバックラッシュを使用する必要があります。また

File f = new File("C:\\sample\\welcome.txt"); 

を、あなたは直接渡すのFileInputStreamをインスタンス化することができますファイルオブジェクト

InputStream inputStream = new FileInputStream(f); 
+0

ありがとう、働いてくれました – superfine123

+0

@ superfine123うれしい助けるために。あなたが好きなら、答えを受け入れたものとしてマークしてください。 – RubioRic

0

最初に行うことは、そのファイルが実際にC:\ドライブに存在するかどうかを確認することです。

C:\ドライブ

C:

/sample/welcome.txtバックスラッシュは最初のヒントである:)

+0

ファイルがcドライブに存在していますが、私はすでにバックスラッシュで試していますが、同じ例外に直面しています – superfine123

+0

あなたはAndroidにいますか?または窓で? – granmirupa

+0

私は上記の使用方法については – superfine123

0
 try { 

     File sdcard = Environment.getExternalStorageDirectory(); 
     File file = new File(sdcard, "location/of/filename.txt"); 
     BufferedReader r = new BufferedReader(new FileReader(file)); 
     StringBuilder total = new StringBuilder(); 
     String line; 
     while ((line = r.readLine()) != null) { 
      total.append(line); 
      total.append('\n'); 
     } 
     String ttt = total.toString(); 
     TextView txt = (TextView)findViewById(R.id.txt); 
     txt.setText(ttt); 



     r.close(); 
     Log.d("File", "File contents: " + total); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

アンドロイド

でファイルを読み取るための正確なコード
0

CommonsWireは、どのようにAndroidでC:が存在しないと言っていますか? あなたはgetExternalStorageDirectoryです。 これを試してみてください:

File myfile = new File(Environment.getExternalStorageDirectory() + File.separator + "myFile.txt"); 
+0

ウィンドウで作業していますが、「getExternalStorageDirectory()メソッドは環境型の定義されていません」となっています。 – superfine123

+0

@ superfine123はAndroidを使用していますか?この環境クラスをインポートしましたか? http://developer.android.com/reference/android/os/Environment.html – granmirupa

+0

私は現在Windows上でやってみようとしています – superfine123

関連する問題