嘿,我是相當新的這個,但我想不硬編碼一個文件被讀入,但我想從終端/命令提示符讀取它。這是我到目前爲止,我在bufferedWriter中硬編碼的文件名,但我怎麼能做到我可以做一個命令,如(Java主< in.txt> out.txt)。提前致謝。如何從終端讀取文本文件並將輸出保存到java中的另一個文件?
public static void main(String[] args) {
String inFile = "in.txt";
String outFile = "out.txt";
if (args.length > 1) {
inFile = args[0];
outFile = args[1];
}
Lexer lexer = new Lexer(inFile);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
Token t;
while ((t = lexer.nextToken()) != null) {
writer.write(t.toString());
writer.newLine();
}
writer.close();
System.out.println("Done tokenizing file: " + inFile);
System.out.println("Output written in file: " + outFile);
} catch (IOException e) {
e.printStackTrace();
}
}
那麼爲什麼我不能使用「<" or ">」?是不是你如何讓程序讀取文件並將結果輸出到其他文件中?此外,當我把System.in或System.out它給了我一個錯誤,說它不適用。 – King11 2014-11-09 07:27:00
@Rod當您使用「<'' or ''>」時,*操作系統會重定向[Standard Streams](http://en.wikipedia.org/wiki/Standard_streams#1995:_Java)stdin和stdout(例如'System。在Java中是'in'和'System.out')。 – 2014-11-09 07:29:01
好吧,那麼我可以在我的代碼中更改哪些內容以允許使用System.in和System.out?我嘗試了不同的事情,但是我得到一個錯誤或者我試圖卡住。 – King11 2014-11-09 07:33:15