简体   繁体   English

尽管没有抛出异常,但PrintWriter的输出文件不存在

[英]PrintWriter the output file is not there although no exception thrown

I have just started learning Java and I have got following problem I have been struggling for hours with. 我刚刚开始学习Java,但是遇到了几个小时来一直困扰我的问题。 I want to use PrintWriter in order to produce a simple text file. 我想使用PrintWriter来生成一个简单的文本文件。

I do not get any runtime exception, still the file is not appearing in the specified directory. 我没有得到任何运行时异常,但文件仍未出现在指定目录中。

public class Main {
    public static void main(String[] args) {
        try (final PrintWriter writer = new PrintWriter(
                new File("c:\test\new\notes.txt"))) {
            writer.write("Test note");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

What am I doing wrong? 我究竟做错了什么?

\\ represents an escape character so needs to be escaped itself for literal backslash characters. \\代表一个转义字符,因此需要为文字反斜杠字符本身进行转义。 You can also use / and Java will resolve the correct separation character for the platform 您也可以使用/ ,Java将为平台解析正确的分隔符

try (final PrintWriter writer = new PrintWriter("c:\\test\new\\notes.txt")) {

writer.write("Test note") writer.flush()之后添加writer.flush()writer.write("Test note") Windows路径使用双反斜杠(如其他答案所示)。

As Reimeus already said, \\ is an escape character in java. 正如Reimeus所说, \\是Java中的转义字符。 That means that a string containing "\\n" or "\\t" does not represent the stringliteral \\n or \\t! 这意味着包含"\\n""\\t"的字符串不代表字符串文字\\ n或\\ t!

'\\n' represents the newline character and '\\t' represents the TAB character! '\\n'代表换行符, '\\t'代表TAB字符!

For the better understanding, the following code: 为了更好的理解,下面的代码:

System.out.println("c:\test\new\notes.txt");

would not print c:\\test\\new\\notes.txt to the console, it would print 不会将c:\\test\\new\\notes.txt打印到控制台,它将打印

c:    est
ew
otes.txt

to the console! 到控制台!

To be able to write the backslash in a string you'll need to use '\\\\' ! 为了能够在字符串中写反斜杠,您需要使用'\\\\'

I see your question as having 2 parts: 我认为您的问题分为两个部分:

  1. Why doesn't the code work? 为什么代码不起作用?
  2. Why was no exception thrown? 为什么没有抛出异常?

The first question has already been answered, but I think the answer to the second question is at least as important because your current code will still fail silently if there is any problem writing to the file. 第一个问题已经回答了,但是我认为第二个问题的回答至少同样重要,因为如果写入文件有任何问题,您当前的代码仍然会静默失败。

From the documentation of PrintWriter ( http://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html ): 从PrintWriter的文档( http://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html ):

Methods in this class never throw I/O exceptions, although some of its constructors may. 此类中的方法永远不会抛出I / O异常,尽管它的某些构造函数可能会抛出异常。 The client may inquire as to whether any errors have occurred by invoking checkError(). 客户端可以通过调用checkError()来查询是否发生了任何错误。

Therefore it is essential that you call checkerror() after every call to a PrintWriter method or your code will not be reliable. 因此,必须在每次调用PrintWriter方法之后调用checkerror() ,否则代码将不可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM