简体   繁体   English

Java-如果字符串以“ <”开头,则无法将字符串写入文件

[英]Java - I can't write a string to a file if the string begins with “<”

If I use the following code: 如果我使用以下代码:

try {
        writer = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream(fileName), "utf-8"));


        writer.write("<title>");




    } catch (IOException e) {

      throw new RuntimeException(e);

    } finally {

       try {
           writer.close();
       } catch (Exception e) {
           throw new RuntimeException(e);
       }

    }

nothing shows up in the file, but if I remove the "<" , and try to output "title>" it works fine. 文件中没有任何显示,但是如果我删除“ <”并尝试输出“ title>”,它将正常工作。 How can I get around this? 我该如何解决?

The problem isn't in your code, it's in the viewer (editor) that you're using to view the output. 问题不在您的代码中,而是在您用来查看输出的查看器(编辑器)中。 Instead of showing you the plain plain text, it's interpreting the data in the file, and showing you its interpretation. 它不是在向您显示纯文本,而是在解释文件中的数据,并向您显示其解释。 Use a plain editor such as notepad or vi to see what is in the file. 使用普通的编辑器(例如记事本或vi)查看文件中的内容。

Try flushing your writer after you write: 写完后,尝试刷新您的编写器:

writer.write("<title>");
writer.flush();

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

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