Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可以将输出的内容写入HTML文件吗?_Java_Html - Fatal编程技术网

Java 可以将输出的内容写入HTML文件吗?

Java 可以将输出的内容写入HTML文件吗?,java,html,Java,Html,我想做的是,当程序运行时,它将以普通方式运行,但如果用户选择希望以html格式显示,那么它将运行普通程序将要执行的操作,而不是在控制台中显示,它将把控制台中显示的内容写入用户指定的html文件中。这可能吗?我有代码来接受用户输入,并让他们指定他们想要的格式以及打开浏览器,但我不确定这是否可行。我已经得到的代码如下: import java.awt.Desktop; import java.io.*; import java.util.*; public class reader { s

我想做的是,当程序运行时,它将以普通方式运行,但如果用户选择希望以html格式显示,那么它将运行普通程序将要执行的操作,而不是在控制台中显示,它将把控制台中显示的内容写入用户指定的html文件中。这可能吗?我有代码来接受用户输入,并让他们指定他们想要的格式以及打开浏览器,但我不确定这是否可行。我已经得到的代码如下:

import java.awt.Desktop;
import java.io.*;
import java.util.*;

public class reader {
    static int validresults = 0;
    static int invalidresults = 0;
    // Used to count the number of invalid and valid matches
    
    public static boolean verifyFormat(String[] words) {
        boolean valid = true;

        if (words.length != 4) { 
            valid = false;
        } else if (words[0].isEmpty() || words[0].matches("\\s+")) {
            valid = false;
        } else if ( words[1].isEmpty() || words[1].matches("\\s+")) {
            valid = false;
        }
                
        return valid && isInteger(words[2]) && isInteger(words[3]);}
    
    // Checks to see that the number of items in the file are equal to the four needed and the last 2 are integers
    // Also checks to make sure that there are no results that are just whitespace
    public static boolean isInteger(String input) {
        try {
            Integer.parseInt(input);
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }
    // Checks to make sure that the data is an integer
    
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        while (true) { // Runs until it is specified to break
            Scanner scanner = new Scanner(System.in);

            System.out.println("Enter filename");
            String UserFile = sc.nextLine();
            File file = new File(UserFile);

            if (!file.exists()) {
              continue;
            }

            if (UserFile != null && !UserFile.isEmpty()){ 
                System.out.println("Do you want to generate plain (T)ext or (H)TML");
                String input = scanner.nextLine();
                if (input.equalsIgnoreCase("H")) {
                    Desktop.getDesktop().browse(file.toURI());
                } else if (input.equalsIgnoreCase("T")) {
                      processFile(UserFile);
                } else {
                    System.out.println("Do you want to generate plain (T)ext or (H)TML");
                }
            }
          }
        }
    
    // Checks how the user wants the file to be displayed 
    private static void processFile(String UserFile) throws FileNotFoundException {
        String hteam;
        String ateam;
        int hscore;
        int ascore;
        int totgoals = 0;

        Scanner s = new Scanner(new BufferedReader(
                new FileReader(UserFile))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");

        while (s.hasNext()) {
            String line = s.nextLine();
            String[] words = line.split("\\s*:\\s*");
            // Splits the file at colons

            if(verifyFormat(words)) {
                hteam = words[0];       // read the home team
                ateam = words[1];       // read the away team
                hscore = Integer.parseInt(words[2]);       //read the home team score
                totgoals = totgoals + hscore;
                ascore = Integer.parseInt(words[3]);       //read the away team score
                totgoals = totgoals + ascore;
                validresults = validresults + 1;
                
                System.out.println(hteam + " " +  "[" + hscore + "]" +  " " + "|" + " " + ateam + " " + "[" + ascore + "]");   
                // Output the data from the file in the format requested

            }
            else{
                invalidresults = invalidresults + 1;
            }
        }

        System.out.println("Total number of goals scored was " + totgoals);
        // Displays the total number of goals
        System.out.println("Valid number of games is " + validresults);
        System.out.println("Invalid number of games is " + invalidresults);

        System.out.println("EOF");
    }
}
//如果用户选择,我们将在这里编写HTML
私有静态最终字符串输出\u FILENAME=“OUTPUT.html”;
公共静态void main(字符串[]args)引发IOException
{
最终扫描仪=新扫描仪(System.in);
最终字符串content=“Foobar”;
最终布尔值为HTML;
字符串输入;
//获取用户的输入
做
{
System.out.print(“您想要消息吗”
+“写入(P)lain text,或(H)TML?”);
输入=scanner.nextLine();
}while(!(input.equalsIgnoreCase(“p”)
||输入。等信号情况(“h”);
toHtml=input.equalsIgnoreCase(“h”);
if(toHtml)
{
//将标准输出流重定向到HTML文件
final FileOutputStream fileOut//False表示我们没有追加
=新文件输出流(输出文件名,false);
最终打印流外流=新打印流(文件输出);
系统放线(扩流);
}
System.out.println(toHtml
?String.format(“%s

”,content)//将HTML写入文件 :content);//我们没有写入HTML文件:纯文本 }
所以只需编写一个HTML文件来保存结果,而不是将其显示在屏幕上?我将如何做到这一点?程序将读取一个文本文件,并以某种方式显示它们,如果用户指定要使用html格式,我将如何将其写入html文件并打开该文件?那么这是如何工作的?我以正确的方式输入了它,但是当我打开html文件时,我得到的只是Foobar输入filename输入filename输入filename输入filename输入filename输入filename输入filename输入filename输入filename输入filename,而不是应该写入file@Rajesh-这只是一个示例,不是完整编码的解决方案
//This is where we'll write the HTML to if the user's chooses so
private static final String OUTPUT_FILENAME = "output.html";

public static void main(String[] args) throws IOException
{
    final Scanner scanner = new Scanner(System.in);
    final String content = "Foobar";

    final boolean toHtml;
    String input;

    //Get the user's input
    do
    {
        System.out.print("Do you want messages "
                + "written to (P)lain text, or (H)TML? ");

        input = scanner.nextLine();
    } while (!(input.equalsIgnoreCase("p") 
            || input.equalsIgnoreCase("h")));

    toHtml = input.equalsIgnoreCase("h");

    if (toHtml)
    {
        //Redirect the standard output stream to the HTML file
        final FileOutputStream fileOut //False indicates we're not appending
                = new FileOutputStream(OUTPUT_FILENAME, false);
        final PrintStream outStream = new PrintStream(fileOut);

        System.setOut(outStream);
    }

    System.out.println(toHtml 
            ? String.format("<p>%s</p>", content) //Write HTML to file
            : content); //We're not writing to an HTML file: plain text
}