炎炎设计
我认为您的期望和现实不符(但是什么时候才出现;)基本上,您认为写入文件的位置与实际写入文件的位置不相等(嗯,也许我应该写一条if语句;))public class TestWriteFile { public static void main(String[] args) { BufferedWriter writer = null; try { //create a temporary file String timeLog = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()); File logFile = new File(timeLog); // This will output the full path where the file will be written to... System.out.println(logFile.getCanonicalPath()); writer = new BufferedWriter(new FileWriter(logFile)); writer.write("Hello world!"); } catch (Exception e) { e.printStackTrace(); } finally { try { // Close the writer regardless of what happens... writer.close(); } catch (Exception e) { } } }}另请注意,您的示例将覆盖所有现有文件。如果要将文本附加到文件,则应执行以下操作:writer = new BufferedWriter(new FileWriter(logFile, true));