2015-09-28 182 views
0

我想知道我的控制檯輸出可以保存在記事本文件中嗎?如何在記事本文件中打印控制檯輸出?

import java.awt.EventQueue; 

public class HLS1 { 

    private JFrame frmHttpsLiveStreaming; 
    private JTextField textField; 
    // file is accessed to the whole class 
    private File file; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        HLS1 window = new HLS1(); 
        window.frmHttpsLiveStreaming.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public HLS1() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frmHttpsLiveStreaming = new JFrame(); 
     frmHttpsLiveStreaming.setTitle("HTTPS Live Streaming"); 
     frmHttpsLiveStreaming.setBounds(100, 100, 494, 112); 
     frmHttpsLiveStreaming.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frmHttpsLiveStreaming.getContentPane().setLayout(null); 

     JButton btnBrowse = new JButton("Open File"); 
     btnBrowse.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 

       System.out.println("Argument:" + arg0); 
       JFileChooser fs = new JFileChooser(new File("c:\\")); 
       fs.setDialogTitle("Open a file"); 
       fs.setFileFilter(new FileTypeFilter(".m3u8", "")); 
       fs.setFileFilter(new FileTypeFilter(".m3u", "")); 
       fs.showOpenDialog(null); 
       file = fs.getSelectedFile(); 
       textField.setText(file.getAbsolutePath()); 

      } 
     }); 
     btnBrowse.setBounds(336, 7, 89, 23); 
     frmHttpsLiveStreaming.getContentPane().add(btnBrowse); 

     JButton btnNewButton_1 = new JButton("Clear"); 
     btnNewButton_1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 

       textField.setText(""); 

      } 
     }); 
     btnNewButton_1.setBounds(237, 39, 89, 23); 
     frmHttpsLiveStreaming.getContentPane().add(btnNewButton_1); 

     JLabel lblUrl = new JLabel("URL"); 
     lblUrl.setBounds(83, 11, 24, 14); 
     frmHttpsLiveStreaming.getContentPane().add(lblUrl); 

     textField = new JTextField(); 
     textField.setBounds(116, 11, 210, 19); 
     frmHttpsLiveStreaming.getContentPane().add(textField); 
     textField.setColumns(10); 

     JButton btnNewButton = new JButton("Check"); 
     btnNewButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 

       try { 
        List<String> fileArray = new ArrayList<String>(); 
        List<String> errors = new ArrayList<String>(); 
        String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; 
        Scanner s = null; 

        if(textField.getText().matches(regex)){ 
         URL url = new URL(textField.getText()); 
         s= new Scanner(url.openStream()); 
        }else{ 
         s = new Scanner(new BufferedReader(new FileReader(file))); 
        } 
        if(s != null){ 
         while(s.hasNextLine()){ 
          String line = s.nextLine(); 
          if(!line.isEmpty()){ 
           fileArray.add(line); 

          } 
          System.out.println(line); 
         } 
        } 
        s.close(); 

        errors.addAll(validateEXTM3U(fileArray)); 


        for (String error : errors) { 
         System.out.println(error); 

        }      

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
     btnNewButton.setBounds(126, 39, 89, 23); 
     frmHttpsLiveStreaming.getContentPane().add(btnNewButton); 




    } 

    private List<String> validateEXTM3U(List<String> fileArray){ 
     List<String> errors = new ArrayList<String>(); 
     String tag = fileArray.get(0); 
     if(!tag.equals("#EXTM3U")){ 
      errors.add("First line in the menifest file is not #EXTM3U"); 
     } 

     return errors; 
    } 

} 
+0

嘗試在Java的文件流和I/O操作讀了。這裏是一個鏈接,讓你開始:https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html – amklose

+1

建議訪問this: [重複的問題(http://stackoverflow.com/問題/ 2851234 /系統輸出到一個文件合的java) – Thalador

+2

可能重複的[重定向所有輸出到文件(http://stackoverflow.com/questions/6674327/redirect-all-output-to-文件) – ControlAltDel

回答

1

這可能是一個哈克的解決方案,但如果你是在Windows或Linux上運行,那麼你可以管/重定向它。

java HLS1 > notepad.txt 

如果不是你在找什麼,那麼爲什麼不使用稱爲log4j的東西?

0

你爲什麼不寫自己的工具,它有一個public static void output(String output)方法。然後,而不是使用System.out.println("...")調用output("...")然後在output(String output)方法,你可以與輸出做任何事情,就像第一次寫入該文件,然後打印到控制檯。

希望這會有所幫助。

相關問題