Java PrintWriter写入文件

Java PrintWriter写入文件,java,xml,xstream,Java,Xml,Xstream,} 上面是我的代码。我目前有一个应用程序使用对象序列化实现数据持久化,但我正在将其转换为XML。我正在使用Xstream库创建XML,但在将其写入光盘时遇到了一些问题。Xstream将XML作为字符串提供给我,然后我尝试使用PrintWriter将其写入文本文件。但是,文本文件是空的,但我试图写入的字符串不是空的。我对PrintWriter的理解是,您提供它应该写入的文件名,它尝试写入该文件(如果它不存在,则创建它),然后它应该将字符串的内容写入该文件 如蒙协助,将不胜感激。我不知道我会错在哪里

}

上面是我的代码。我目前有一个应用程序使用对象序列化实现数据持久化,但我正在将其转换为XML。我正在使用Xstream库创建XML,但在将其写入光盘时遇到了一些问题。Xstream将XML作为字符串提供给我,然后我尝试使用PrintWriter将其写入文本文件。但是,文本文件是空的,但我试图写入的字符串不是空的。我对PrintWriter的理解是,您提供它应该写入的文件名,它尝试写入该文件(如果它不存在,则创建它),然后它应该将字符串的内容写入该文件


如蒙协助,将不胜感激。我不知道我会错在哪里

您需要调用
PrintWriter::flush()
PrintWriter::close()

您需要添加

package healthbuddy;

/**
 *
 * @author tpzap_000
 */
import java.io.*;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import com.thoughtworks.xstream.persistence.FilePersistenceStrategy;
import com.thoughtworks.xstream.persistence.PersistenceStrategy;
import com.thoughtworks.xstream.persistence.XmlArrayList;
import java.util.List;
import java.util.Scanner;



public class PersistentDataModelCntl implements Serializable{

private File theFile = new File("PDM.txt");
private XStream xstream = new XStream(new StaxDriver());
public static PersistentDataModelCntl thePDMCntl;
private PersistentDataModel thePDM;

public PersistentDataModelCntl(){
    this.readPDMFile();
}
public static PersistentDataModelCntl getPDMCntl(){
    if(thePDMCntl == null){
        thePDMCntl = new PersistentDataModelCntl();
    }
    return thePDMCntl;
}   
public void readPDMFile(){
    try
    {
        System.out.println("in read file");
        StringBuilder fileContents =  new StringBuilder();
        Scanner in = new Scanner(theFile);
        String tempXML;
        boolean test = in.hasNextLine();
        System.out.println(test);
            while(in.hasNextLine()){
                fileContents.append(in.nextLine());
                System.out.println("reading file contents");
            }
             tempXML = fileContents.toString();

             thePDM = (PersistentDataModel)xstream.fromXML(tempXML);
    }

    //If the file does not exist, thePDM is instantiated to be a new, empty, PDM file. The file      is then written to disk, and then read from disk
    // using some recursive stuff. Also creates a test UserList so that I don't get a NullPointerException in the LoginCntl.
    catch(FileNotFoundException ex){
        System.out.println("FileNotFound");
        thePDM = new PersistentDataModel();
        thePDM.thePDMFoodList = new FoodList();
        thePDM.thePDMMealList = new MealList();
        thePDM.thePDMDietList = new DietList();
        thePDM.thePDMDiet = new Diet();
        //Creates new attributes if things are null. 
        this.writePDMFile();
        this.readPDMFile();
        System.out.println("FileNotFound Exception");

    }
    catch(IOException ex){
        System.out.println("IO Exception");
        ex.printStackTrace();

    }

} 

//Problem Code is here:
public void writePDMFile(){

    try{
       String xml = xstream.toXML(thePDM);
        PrintWriter writer = new PrintWriter(theFile);
        System.out.println(xml);
        writer.println(xml);

    }
    catch(Exception ex){
        System.out.println("There was a problem writing the file.");
    }
}
public PersistentDataModel getPDM(){
    return thePDM;
}

直到代码的末尾。写入程序只在文件关闭时才写入文件。

我是个白痴。我没有打电话给我的打印机

尝试在将xml写入文件后关闭PrintWriter

使用双参数形式的
toXML
直接写入输出流,而不是将xml在内存中构建为字符串并单独写入,这样效率会更高。当然,您仍然需要关闭流。让XStream直接与二进制流对话也可以避免任何字符编码问题(获取
文件的
PrintWriter
构造函数始终使用平台默认编码,而
StaxDriver
将编写后续解析器视为UTF-8的XML).在那之前,这些人回答了你的问题,你可能应该接受其他三个答案中的一个。我建议你在最后一个街区里这样做。如果您正在进行大量处理并出现错误,您可能仍然希望将文件刷新到磁盘。如果这种情况发生在web应用程序中,您可能也希望关闭句柄以避免泄漏。
writer.close()