如何设置System.setOut()的目录

时间:2018-01-10 09:52:45

标签: java eclipse run-configuration

我想将输出保存为指定文件夹中的单独输出。以下代码将保存项目根目录的输出。我怎样才能改变它?

public static void main(String[] args) throws Exception {
      FileOutputStream f = new FileOutputStream("file.txt");
      System.setOut(new PrintStream(f));
      System.out.println("This is System class!!!");
}

此外,我试图更改"运行配置" - > "普通" - > "输出文件"在Eclipse中,但它对我没有帮助。

1 个答案:

答案 0 :(得分:2)

直接将Filename传递给FileOutputStream的Intead需要传递一个File实例,如下所示:

Product <- c("Product1","Product2","Product3","Product4","Product5","Product6","Product7")
Value <- c(1000000,200002,599996,1399994,2199992,2999990,3799988)
df <- data.frame(Product,Value)
df$Label <- paste(Product, paste(round(((df$Value/sum(df$Value))*100),2),"%"), sep="-")

library(ggplot2)

p <-  ggplot(df, aes(x = 1, y = Value, fill = Product)) + geom_bar(stat = "identity")
p <- p + coord_polar(theta = 'y') + theme_void()
p <- p + geom_text(aes(label = Label), position = position_stack(vjust = 0.5))

然后

File directoryLogs = new File("logs");
fileDirectory.mkdirs(); // Create the directory (if not exist)

File fileLog = new File(directoryLogs, "log.txt");
fileLog.createNewFile(); // Create the file (if not exist)

如果要完全更改日志目录的路径,也可以指定 这里的绝对路径

 public static void main(String[] args) throws Exception {

    // Create a log directory
    File directoryLogs = new File("logs");
    fileDirectory.mkdirs();

    // Create a log file
    File fileLog = new File(directoryLogs, "log.txt");
    fileLog.createNewFile();

    // Create a stream to to the log file
    FileOutputStream f = new FileOutputStream(fileLog);

    System.setOut(new PrintStream(f));
    System.out.println("This is System class!!!");

    // You should close the stream when the programs end
    f.close();
}