java 将文本写入 html 文件类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11643351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:53:52  来源:igfitidea点击:

writing text to an html file type

javafilewebpage

提问by Kerry G

I am trying to write the result of the following method to a webpage. I think it is possible to do, I am just having trouble figuring out exactly how to do it. Any help would be very appreciated. Thanks.

我正在尝试将以下方法的结果写入网页。我认为这是可能的,我只是在弄清楚如何做到这一点时遇到了麻烦。任何帮助将不胜感激。谢谢。

...
      System.out.println("Final Register");
      for (int i=0; i < ch.length; i++)
        {
         System.out.println(cd.getDenomLiteralAt(i)+" "+cd.getDenomNumAt(i));
        }
   }

...

回答by MaVRoSCy

In java there are many ways to write data on a file. To write text data the easiest way is by using a BufferedWriter.

在java中有很多方法可以将数据写入文件。要写入文本数据,最简单的方法是使用 BufferedWriter。

See demo below

见下面的演示

FileWriter fWriter = null;
BufferedWriter writer = null;
try {
    fWriter = new FileWriter("fileName.html");
    writer = new BufferedWriter(fWriter);
    writer.write("<span>This iss your html content here</span>");
    writer.newLine(); //this is not actually needed for html files - can make your code more readable though 
    writer.close(); //make sure you close the writer object 
} catch (Exception e) {
  //catch any exceptions here
}

回答by Dan

All you need to do is know the path to the public HTML file of your webserver.

您需要做的就是知道您的网络服务器的公共 HTML 文件的路径。

Run the Java code and write to a file below that path. It's no different than writing to any other file on a machine, except this one the world can see. Only thing is, if your Java will be creating the file, you should be sure to set protective enough permissions for the file. 0755 is moderately safe.

运行 Java 代码并写入该路径下的文件。这与写入机器上的任何其他文件没有什么不同,除了这个世界可以看到的文件。唯一的问题是,如果您的 Java 将创建该文件,您应该确保为该文件设置足够的保护权限。0755 是中等安全的。