Search code examples
javafilepathprintwriter

java printwriter: Output file empty


Question: How do you ask the defined output class to print result in the output file as .txt

All other java classes seem to be working and runs alright but it fails to print out any data in the output folder. I have imported the PrintWriter, but it will not create the file

Code:

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

Solution

  • i created a dummy program using the sendToFile() and it is working fine and creating an output file.

    Please paste the error that you are getting. I hope you are initializing the variables "cwsSolution" and "cwsSolution" correctly and no null is being thrown.

    Please see if following link helps: How to use PrintWriter and File classes in Java?

    Sample code is below:

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