2015-01-13 36 views
-1

我想要做的是讓程序運行時它將以普通的方式運行,但如果用戶選擇他們希望顯示在HTML中,那麼它將運行普通程序將執行的操作,而不是顯示它將在控制檯中將控制檯中出現的內容寫入用戶指定的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 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"); 
    } 
} 
+0

所以只寫保存結果,而不是在屏幕上顯示它的HTML文件? – Ascalonian

+0

我該怎麼做?該程序將讀取一個文本文件,並以某種方式顯示它們,然後如何將這個文件寫入一個html文件並在用戶指定他們希望在html中打開該文件時打開該文件? – Rajesh

回答

1
//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 
} 
+0

那麼這是如何工作的?我以正確的方式輸入它,但是當我打開html文件時,我得到的只是Foobar 輸入文件名輸入文件名輸入文件名輸入文件名輸入文件名輸入文件名輸入文件名輸入文件名輸入文件名而不是應該寫入的文件名文件 – Rajesh

+0

@Rajesh - 這是一個例子,而不是一個完整的編碼解決方案 – Ascalonian