繁体   English   中英

Java PrintWriter写入文件

[英]Java PrintWriter Writing to File

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作为字符串,然后我尝试使用PrintWriter将其写入文本文件。 但是,文本文件为空,但我尝试写入的字符串却不是。 我对PrintWriter的理解是,您为其提供了应写入的文件名,它尝试写入该文件(如果不存在则创建该文件),然后将String的内容写入该文件。

任何帮助将不胜感激。 不知道我要去哪里错了。

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

您需要添加

writer.close()

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

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

我是个白痴。 我没有在PrintWriter上调用close。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM