简体   繁体   中英

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

}

Above is my code. I currently have an app that uses object serialization for it's data persistence, but I'm in the process of converting it to XML. I'm using the Xstream library to create the XML, but I'm having some trouble writing it to disc. Xstream gives me the XML as a String, which I then attempt to write to a text file using PrintWriter. However the text file is empty, but the String I'm attempting to write to it is not. My understanding of PrintWriter is such that you supply it the file name it should be writing to, it attempts to write to that file(creates it if it does not exist), and then it should write the contents of the String to the file.

Any assistance would be greatly appreciated. Not sure where I'm going wrong.

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

You need to add

writer.close()

to the end of your code. The writer only writes to file when it is closed.

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

I'm an idiot. I didn't call close on my PrintWriter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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