2012-03-07 13 views

答えて

1

私はあなたのための2例を持っています。最初はテキストファイルからの読み込み、2番目はテキストファイルからの書き込みです。

import java.io.*; 
class FileRead { 
    public static void main(String args[]) { 
     try{ 
      BufferedReader br = new BufferedReader(new FileReader("textfile.txt")); 
      String strLine; 
      //Read File Line By Line 
      while ((strLine = br.readLine()) != null) { 
       // Print the content on the console 
       System.out.println (strLine); 
      } 
      //Close the input stream 
      br.close(); 
     } catch (Exception e){//Catch exception if any 
      System.err.println("Error: " + e.getMessage()); 
     } 
    } 
} 


import java.io.*; 
class FileWrite { 
    public static void main(String args[]) { 
     String var = "var"; 
     try { 
      // Create file 
      FileWriter fstream = new FileWriter("out.txt"); 
      BufferedWriter out = new BufferedWriter(fstream); 
      out.write(var); 
      //Close the output stream 
      out.close(); 
     } catch (Exception e){//Catch exception if any 
      System.err.println("Error: " + e.getMessage()); 
     } 
    } 
} 
+0

ありがとうございます!私はそれをしましたが、私のfile.txtを見つけることができませんでした。プログラムディレクトリを検索しましたが、この名前のファイルはありませんでした。 –

+0

@StelyianStoyanovこれはおそらく、相対パスが予期しない場所にファイルを置いているためです。 "C:\\ test.txt"のような絶対パスを試してみて、それを見つけて確認してください。 –

+0

Riyad Kalla、あなたの応答をありがとうが、私はUNIXマシンを使用しています。この場合、ファイルを保存するにはどうすればいいですか?ありがとう! –

関連する問題