English 中文(简体)
在没有数据的情况下,在FileOutputStream创建文档
原标题:Skip creating file in FileOutputStream when there is no data in Inputstream

这是一种伐木功能,从外部方案的实施中发现错误。 一切产品都进行罚款。 但是,如果没有错误数据,我不想生成逻辑框架。 目前,它正在创建零规模档案。 请提供帮助。

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

谢谢。

最佳回答

简单地推迟制定<代码> 文件:

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

个人而言,I m 不是PrintWriter的大扇,它只是吞.了所有的例外情况。 我也使用<代码>OutputStreamWriter,以便你能够明确指明编码。 不管怎么说,这从这里的真正问题 aside开。

问题回答

改变现状是显而易见的。

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 纽约总部 split this using the Execute Around idiom.
    if (rawLog != null) {
        rawLog.close();
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签