0

I want to know how my console output can be save in a notepad file?

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;
    }

}
Ram
  • 3,092
  • 10
  • 40
  • 56
satya
  • 25
  • 7
  • Try reading up on Java file streams and I/O operations. Here is a link to get you started: https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html – amklose Sep 28 '15 at 19:49
  • 1
    Suggest visiting this: [Duplicate Question](http://stackoverflow.com/questions/2851234/system-out-to-a-file-in-java) – Thalador Sep 28 '15 at 19:50
  • 2
    possible duplicate of [Redirect all output to file](http://stackoverflow.com/questions/6674327/redirect-all-output-to-file) – ControlAltDel Sep 28 '15 at 19:51
  • 1
    This is a LOT of code for what's ostensibly a very simple question. Can you narrow it down? Have you tried searching the internet for a solution? – Sandy Gifford Sep 28 '15 at 20:08
  • i tried searching on internet.I found but i don't know where i have to put that in my code? – satya Sep 28 '15 at 20:30
  • There a few ways to do it, you can use a logger API (like log4j) or you can redirect the out through your own PrintStream, using System.setOut(...) – MadProgrammer Sep 28 '15 at 21:51

2 Answers2

1

It could be a hacky solution , but if you are running in windows or linux then you can pipe / redirect it.

java HLS1 > notepad.txt

if not what you are looking for , then why not using something called log4j ?

Abhiram mishra
  • 1,597
  • 2
  • 14
  • 34
0

Why don't you write your own utility that has an public static void output(String output) method. Then instead of using System.out.println("...") you call output("...") then in your output(String output) method you can do anything with the output, like first write to the file, then print to the console.

Hope this helps.

Nicholas Robinson
  • 1,359
  • 1
  • 9
  • 20