java printwriter:输出文件为空
java printwriter: Output file empty
问题:如何要求定义的输出class将结果打印在输出文件中作为.txt
所有其他 java classes 似乎都在工作并且运行正常,但它无法在输出文件夹中打印出任何数据。我已导入 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);
}
}
}
我看到的问题是您忘记了 .flush()
和 PrintWriter
在关闭它之前明确地将数据发送到输出目标。
[...]
out.println(bestSol.toString() + "\r\n");
out.flush();
out.close();
[...]
我使用 sendToFile() 创建了一个虚拟程序,它工作正常并创建了一个输出文件。
请粘贴您遇到的错误。我希望你正确地初始化了变量 "cwsSolution" 和 "cwsSolution" 并且没有抛出 null。
请查看以下link是否有帮助:
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");
}
}
问题:如何要求定义的输出class将结果打印在输出文件中作为.txt
所有其他 java classes 似乎都在工作并且运行正常,但它无法在输出文件夹中打印出任何数据。我已导入 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);
}
}
}
我看到的问题是您忘记了 .flush()
和 PrintWriter
在关闭它之前明确地将数据发送到输出目标。
[...]
out.println(bestSol.toString() + "\r\n");
out.flush();
out.close();
[...]
我使用 sendToFile() 创建了一个虚拟程序,它工作正常并创建了一个输出文件。
请粘贴您遇到的错误。我希望你正确地初始化了变量 "cwsSolution" 和 "cwsSolution" 并且没有抛出 null。
请查看以下link是否有帮助: 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");
}
}