[英]java.io.File customized file names
我創建了一個代碼,一切正常,但是我希望該程序生成一個.txt文件,並將結果保存起來以備后用。 但是我希望程序在每次執行時都創建不同的文件,因為我計划每月使用一次(用於每月利潤計算),並使用年和月的編號保存它來命名文件。
例如:
File file = new File("C:\...\YearMonth.txt");
但是當然,年份和月份取決於程序中先前通知的int Year
和int Month
變量。
有任何想法嗎? 希望我足夠清楚。
您應該創建一個文件meta_data來存儲先前文件的信息
因此,當您創建一個新文件時,您將需要讀取meta_data文件以生成新文件名
希望這可以幫助您
你要嗎
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = (c.get(Calendar.MONTH) + 1);
String fileName = "profit_"+ month + "_" + year + ".txt";
File file = new File("C:...\\" + fileName);
我相信您正在使用獨立Java程序。 當您想記錄打印在控制台中的結果時,請按以下方式使用
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = (c.get(Calendar.MONTH) + 1);
String fileName = "Log_"+year"_"+month+".txt"
File file = new File (fileName);
FileOutputStream fos = new FileOutputStream(file);
ps = new PrintStream(fos);
System.setOut(ps); // This will write all the console log to a file.
//when exiting the program or before termination Flush the PrintStream
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.