簡體   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