当Inputstream中没有数据时跳过在FileOutputStream中创建文件

发布于 2024-09-11 18:54:22 字数 517 浏览 4 评论 0原文

这是一个日志记录功能,用于记录外部程序执行过程中的错误流。一切正常。但我不想在错误流中没有数据时生成日志文件。目前它正在创建零大小的文件。请帮忙。

FileOutputStream fos = new FileOutputStream(logFile);
PrintWriter pw = new PrintWriter(fos);

Process proc = Runtime.getRuntime().exec(externalProgram);

InputStreamReader isr = new InputStreamReader(proc.getErrorStream());
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
{
   if (pw != null){
      pw.println(line);
      pw.flush(); 
   }
}

谢谢。

This is a logging function which logs error stream from the execution of an external program. Everything works fine. But I do not want to generate the log file when there is no data in error stream. Currently it is creating zero size file. Please help.

FileOutputStream fos = new FileOutputStream(logFile);
PrintWriter pw = new PrintWriter(fos);

Process proc = Runtime.getRuntime().exec(externalProgram);

InputStreamReader isr = new InputStreamReader(proc.getErrorStream());
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
{
   if (pw != null){
      pw.println(line);
      pw.flush(); 
   }
}

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我纯我任性 2024-09-18 18:54:23

显而易见的事情是更改

FileOutputStream fos = new FileOutputStream(logFile);
PrintWriter pw = new PrintWriter(fos);
....
   if (pw != null){
   ...
   }

FileOutputStream rawLog = null;
try {
    PrintWriter Log = null;
    ....
       if (log == null) {
           rawLog = new FileOutputStream(logFile);
           log = new PrintWriter(log, "UTF-8");
       }
       ...
} finally {
    // Thou shalt close thy resources.
    // Icky null check - might want to split this using the Execute Around idiom.
    if (rawLog != null) {
        rawLog.close();
    }
}

The obvious thing to do is to change

FileOutputStream fos = new FileOutputStream(logFile);
PrintWriter pw = new PrintWriter(fos);
....
   if (pw != null){
   ...
   }

to

FileOutputStream rawLog = null;
try {
    PrintWriter Log = null;
    ....
       if (log == null) {
           rawLog = new FileOutputStream(logFile);
           log = new PrintWriter(log, "UTF-8");
       }
       ...
} finally {
    // Thou shalt close thy resources.
    // Icky null check - might want to split this using the Execute Around idiom.
    if (rawLog != null) {
        rawLog.close();
    }
}
你没皮卡萌 2024-09-18 18:54:22

只需推迟 FileOutputStreamPrintWriter 的创建,直到您需要它:

PrintWriter pw = null;

Process proc = Runtime.getRuntime().exec(externalProgram);

InputStreamReader isr = new InputStreamReader(proc.getErrorStream());
BufferedReader br = new BufferedReader(isr);
String line;
while ( (line = br.readLine()) != null)
{
   if (pw == null)
   {
      pw = new PrintWriter(new FileOutputStream(logFile));
   }
   pw.println(line);
   pw.flush(); 
}

就我个人而言,我不是 PrintWriter 的忠实粉丝 - 事实上,它只是吞掉了所有与我有关的例外情况。我还使用 OutputStreamWriter 以便您可以显式指定编码。无论如何,这与这里真正的问题无关。

Simply defer the creating of the FileOutputStream and PrintWriter until you need it:

PrintWriter pw = null;

Process proc = Runtime.getRuntime().exec(externalProgram);

InputStreamReader isr = new InputStreamReader(proc.getErrorStream());
BufferedReader br = new BufferedReader(isr);
String line;
while ( (line = br.readLine()) != null)
{
   if (pw == null)
   {
      pw = new PrintWriter(new FileOutputStream(logFile));
   }
   pw.println(line);
   pw.flush(); 
}

Personally I'm not a big fan of PrintWriter - the fact that it just swallows all exceptions concerns me. I'd also use OutputStreamWriter so that you can explicitly specify the encoding. Anyway, that's aside from the real question here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文