0
我需要您的幫助以下代碼,我無法附加文本(在我的示例中爲Hi
)。該文件正在創建,我在裏面只有1 Hi
,但我在它內部循環(當我運行cmd我可以看到它循環和系統打印幾個喜),但爲什麼我在文件1 hi
?Java字符串在創建時不附加到文件中
我確信這是真的 fw = new FileWriter(file.getAbsoluteFile(), true);
try
{
LineNumberReader rdr = new LineNumberReader(new FileReader(directory+"/Ant_log.log"));
String timeStamp = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
BufferedWriter bw = null;
FileWriter fw = null;
File file = new File(directory+"/Log-Missing-scripts.txt");
String line1 ="";
while((line1 = rdr.readLine())!= null)
{
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// true = append file
fw = new FileWriter(file.getAbsoluteFile(), true);
bw = new BufferedWriter(fw);
System.out.println(rdr.getLineNumber());
if (rdr.getLineNumber()== 3)
{
System.out.println("Hi");
bw.write("Hi");
break;
}
}
bw.close();
writer.close();
}
catch(Exception e)
{
System.out.println("ERROR : In Log File");
}
}
@ScaryWombat感謝它的工作,請添加爲解答此問題的答案 – Moudiz