Java PrintWriter写入文件

时间:2014-12-08 15:48:39

标签: java xml xstream

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的过程中。我正在使用Xstream库来创建XML,但是我在将其写入光盘时遇到了一些麻烦。 Xstream将XML作为String提供给我,然后我尝试使用PrintWriter将其写入文本文件。但是文本文件为空,但是我尝试写入的字符串不是。我对PrintWriter的理解是,你提供它应该写入的文件名,它试图写入该文件(如果它不存在则创建它),然后它应该将String的内容写入文件。

非常感谢任何帮助。不知道我哪里出错了。

4 个答案:

答案 0 :(得分:0)

您需要致电PrintWriter::flush()PrintWriter::close()

答案 1 :(得分:0)

您需要添加

writer.close()

到代码的末尾。编写器仅在文件关闭时写入文件。

答案 2 :(得分:0)

在将xml写入文件

后尝试关闭PrintWriter

答案 3 :(得分:-1)

我是个白痴。我没有在我的PrintWriter上打电话。