我想寫我的Java控制檯輸出到我的桌面上的.txt文件。 但是,當該方法啓動時,我有控制檯輸出和txt文件被創建,但它是空的,我意識到BufferedReader(in)不工作...(因爲「ok1 - i」語句)寫控制檯輸出到.txt文件
問題是爲什麼?或者我的代碼有什麼錯誤? 這是我的代碼,所以你可以看到並運行它
package noobxd;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class Main {
public static void main(String[] args) throws IOException {
String path = "C:\\Users\\Mario\\Desktop\\output.txt";
generate_codes();
writetxt(path);
}
private static void writetxt(String path) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new FileWriter(path));
try {
String inputLine;
inputLine = "";
int i=0;
System.out.println("Starting");
while (!inputLine.isEmpty()){
System.out.println("ok1"+i);
inputLine = in.readLine();
System.out.println("ok2"+i);
out.write(inputLine);
System.out.println("ok3"+i);
out.newLine();
System.out.println("ok4"+i);
i++;
}
System.out.print("Write Successful");
} catch (IOException e1) {
System.out.println("Error during reading/writing");
} finally {
out.close();
in.close();
}
}
private static void generate_codes() {
Random rnd = new Random();
for (int i = 0; i < 30; i++) {
int code = rnd.nextInt(201) + 100;
int students = rnd.nextInt(31) + 40;
int j = rnd.nextInt(4);
String type = new String();
switch (j) {
case 0:
type = "Theory";
break;
case 1:
type = "Lab";
break;
case 2:
type = "Practice";
break;
case 3:
type = "Exam";
break;
}
System.out.println("TEL" + code + "-TopicNumber" + i + "-" + students + "-" + type);
}
}
}
感謝您的時間,請幫我解決我的問題。
+1的noobxd包名;) –
@UncleIroh HAHAH笑那是因爲這是基於一個「額外的應用程序」幫我導出txt文件在我的Java-SQL中使用應用(主要的,可以導入一個txt文件),所以這只是在控制檯,然後我把它命名爲noobxd – Mgustavoxd1