![](/img/trans.png)
[英]How to write to a txt file “using the keyboard and PrintWriter Class ” created in java
[英]Specifying file location for PrintWriter class (Java) and automatically appending .txt
假設我要求用戶指定他要調用的新文件
System.out.println("What do you want to call the file?");
String outputFile = keyboard.nextLine();
現在寫文件我會做:
PrintWriter outputFile = new PrintWriter(fileName);
我的問題是:
我知道默認情況下它將保存到本地文件夾。 我該如何做才能將其保存到用戶桌面?
如何自動將.txt附加到他的給定文件名中,這樣他就不必這樣做了?
if(!fileName.endsWith(".txt")) fileName = fileName+".txt";
如果要問用戶將文件放在哪里,則可能應該從系統屬性“ user.home”給定的目錄開始,即調用System.getProperty(“ user.home”);。
然后,您可以顯示目錄列表,並要求用戶選擇一個目錄,然后向下鑽取,直到該用戶位於他要使用的目錄中。 實際上,在Windows計算機上,“桌面”目錄位於用戶的主目錄下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.