如何修改此運行方法,以便不僅可以在輸出窗口中打印srting,還可以在項目目錄中寫入例如outfile.txt的文件。另外,每個字符串應該位於文件的單獨一行中。用java寫入文件
所以我已創建項目目錄中的文件名爲outfile.txt
一個瞬間的代碼是關於窗口打印正常,但在文本文件中
這裏不打印是代碼#
public void run() throws IOException
{
Scanner sc = new Scanner(System.in);
boolean cont = true;
while (cont) {
System.out.println("Enter text");
String s = sc.nextLine();
if ("*".equals(s)) {
cont = false;
} else {
String result = shorthand(s);
System.out.println(result);
PrintWriter pw = new PrintWriter("outfile.txt");
pw.println(result);
}
}
}
PrintWriter out = new PrintWriter(new FileWriter(「outfilename」)); out.write(結果); out.close();但似乎仍然沒有工作,我已經嘗試了文件的位置的完整路徑。任何想法 – luvthug 2010-05-04 13:34:50