java printwriter:输出文件为空

时间:2015-05-06 12:47:18

标签: java filepath printwriter

问题:如何要求定义的输出类将输出文件中的结果打印为.txt

所有其他java类似乎都正常工作并且运行正常,但无法打印输出文件夹中的任何数据。我已导入PrintWriter,但它不会创建文件

代码:

import java.io.IOException;
 import java.io.PrintWriter;



public class Outputs
{
    //INSTANCE FIELDS

    private Solution cwsSolution;
    private Solution bestSol;

    // CLASS CONSTRUCTOR

    Outputs()
    {   cwsSolution = null;
        bestSol = null;
    }

    // SET METHODS

    public void setCWSSol(Solution cwsSol)
    {   cwsSolution = cwsSol;
    }

    public void setOBSol(Solution obSol)
    {   bestSol = obSol;
    }

    // GET METHODS

    public Solution getCWSSol()
    {   return cwsSolution;
    }

    public Solution getOBSol()
    {   return bestSol;
    }

    //PUBLIC METHOD 
printOutputFile()

    public void sendToFile(String outFile)
    {
        try 
        {   PrintWriter out = new PrintWriter(outFile);
            out.println("***************************************************");
            out.println("*                      OUTPUTS                    *");
            out.println("***************************************************");
            out.println("\r\n");
            out.println("--------------------------------------------");
            out.println("Clarke & Wright Solution (parallel version)");
            out.println("--------------------------------------------");
            out.println(cwsSolution.toString() + "\r\n");
            out.println("--------------------------------------------");
            out.println("\r\n OUR BEST SOLUTION:\r\n");
            out.println("--------------------------------------------");
            out.println(bestSol.toString() + "\r\n");
            out.close();
        } catch (IOException exception) 
        {   System.out.println("Error processing output file: " + exception);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我使用sendToFile()创建了一个虚拟程序,它正常工作并创建一个输出文件。

请粘贴您收到的错误。我希望你正在初始化变量" cwsSolution"和" cwsSolution"正确并且没有抛出null。

请查看以下链接是否有帮助: How to use PrintWriter and File classes in Java?

示例代码如下:

   public class Outputs
 {
    String bestSol="bestSol";
    String cwsSolution="cwsSolution";

   public void sendToFile(String outFile)
   {

   try 
   {   PrintWriter out = new PrintWriter(outFile);
       out.println("***************************************************");
       out.println("*                      OUTPUTS                    *");
       out.println("***************************************************");
       out.println("\r\n");
       out.println("--------------------------------------------");
       out.println("Clarke & Wright Solution (parallel version)");
       out.println("--------------------------------------------");
       out.println(cwsSolution.toString() + "\r\n");
       out.println("--------------------------------------------");
       out.println("\r\n OUR BEST SOLUTION:\r\n");
       out.println("--------------------------------------------");
       out.println(bestSol.toString() + "\r\n");
       out.close();
   } catch (IOException exception) 
   {   System.out.println("Error processing output file: " + exception);
   }
 }

   public static void main(String[] args) {
        Outputs output = new Outputs();
        output.sendToFile("C:\\t.txt");
   }
}

答案 1 :(得分:-1)

我看到的问题是,您忘记.flush() PrintWriter在关闭数据之前将数据明确地发送到输出目标。

[...]
out.println(bestSol.toString() + "\r\n");
out.flush();
out.close();
[...]