简体   繁体   中英

How to write an output of a function in a file.txt using JAVA

I'm working for CLI project commands and if the user writes ( ls > a.txt ) that's mean i should print the output of the function ls in a file called a.txt. I used split to split word a.txt and ls, but i can't put the output of function help in a.txt

        for(int j = 0 ; j < input.length();j++)
        {
            if(input.contains(">"))
            {
                String [] Operator = input.split(Pattern.quote(" > "));
                if (!CLI.parse(Operator[0]))
                {
                    continue;
                }
                cases(CLI);
                File file = new File(Operator[1]);
                file.createNewFile();
               // PrintWriter pw = new PrintWriter(Operator[1]);
               // PrintWriter out = new PrintWriter(new FileWriter(Operator[1], true), true);
                out.close();
                pw.close();
            }
        }

Where the function ls is used to print all the directories/files inside a specific directory

public void ls()
{
    String arr[] = Current.list();
    if(arr.length==0)
    {
        System.out.println("___<<Empty Directory>>___");
    }
    else
    {
       for (String str : arr)
           System.out.println(str);
    }
}

Sorry if my English is bad as it's not my first language!

You can use the Logging API . Java contains the Java Logging API. This logging API allows you to configure which message types are written. Individual classes can use this logger to write messages to the configured log files.

The java.util.logging package provides the logging capabilities via the Logger class.

import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.io.IOException;

public class WriteLogToFile {
    public static void main(String[] args) throws IOException {
        Logger logger = Logger.getLogger(WriteLogToFile.class.getName());

        // Create an instance of FileHandler that write log to a file called
        // app.log. Each new message will be appended at the at of the log file.
        FileHandler fileHandler = new FileHandler("app.log", true);        
        logger.addHandler(fileHandler);

        if (logger.isLoggable(Level.INFO)) {
            logger.info("Information message");
        }

        if (logger.isLoggable(Level.WARNING)) {
            logger.warning("Warning message");
        }

    }
}

There is also a good tutorial with best practices and how to use the logger in depth. Here is a link: https://www.vogella.com/tutorials/Logging/article.html

You can choose what type of log file format you want. Example:

    fileTxt = new FileHandler("Logging.txt");
    fileHTML = new FileHandler("Logging.html");

    // create a TXT formatter
    formatterTxt = new SimpleFormatter();
    fileTxt.setFormatter(formatterTxt);
    logger.addHandler(fileTxt);

    // create an HTML formatter
    formatterHTML = new MyHtmlFormatter();
    fileHTML.setFormatter(formatterHTML);
    logger.addHandler(fileHTML);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM