Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关闭时写入程序未将文本写入子目录中的文件_Java_File - Fatal编程技术网

Java 关闭时写入程序未将文本写入子目录中的文件

Java 关闭时写入程序未将文本写入子目录中的文件,java,file,Java,File,我有一个非常简单的操作,将一些文本写入子目录中的文件。在我看过的所有教程中,这应该是可行的,但是每次文件都是空的。它在不写入子目录时工作,不会引发异常 File file = new File("./Decks"); public void saveDeck(Deck deck) { File deckFile = new File(file, deck.getName() + ".xml"); try { if(!deckFile.exists()){

我有一个非常简单的操作,将一些文本写入子目录中的文件。在我看过的所有教程中,这应该是可行的,但是每次文件都是空的。它在不写入子目录时工作,不会引发异常

File file = new File("./Decks");

public void saveDeck(Deck deck) {
    File deckFile = new File(file, deck.getName() + ".xml");

    try {
        if(!deckFile.exists()){
            deckFile.createNewFile();
        }


        Writer writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(deckFile.getName()), "utf-8"));
        writer.write(deck.toXml());
        writer.close();

    } catch (IOException x) {
        System.err.format("IOException: %s%n", x);
    }

}
下面是甲板类的外观:

public class Deck {
String name;
String cardLocation;

public Deck(String name, String cardLocation) {
    this.name = name;
    this.cardLocation = cardLocation;
}

public String toXml(){
    StringBuilder builder = new StringBuilder();
    builder.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?> \n");
    builder.append("<deck> \n");
    builder.append("<name> " + this.name + " </name> \n");
    builder.append("<cardLocation> " + this.cardLocation + " </cardLocation> \n");
    builder.append("</deck> \n");
    return builder.toString();
}

public String getName() {
    return name;
}

public String getCardLocation() {
    return cardLocation;
}
}
公共类甲板{
字符串名;
字符串位置;
公共组(字符串名称、字符串位置){
this.name=名称;
this.cardLocation=cardLocation;
}
公共字符串toXml(){
StringBuilder=新的StringBuilder();
生成器。追加(“\n”);
生成器。追加(“\n”);
builder.append(“+this.name+”\n”);
builder.append(“+this.cardLocation+”\n”);
生成器。追加(“\n”);
返回builder.toString();
}
公共字符串getName(){
返回名称;
}
公共字符串getCardLocation(){
返回位置;
}
}
File.getName()
返回路径的文件名部分, 没有父目录

鉴于此:

deckFile.getName()
的结果是
deck.getName()+“.xml”
。 路径部分丢失

因此,修复方法是将
deckFile.getName()
替换为
deckFile

Writer writer = new BufferedWriter(new OutputStreamWriter(
        new FileOutputStream(deckFile), "utf-8"));
writer.write(deck.toXml());
writer.close();

你试过调试它吗?您在
deck
中有什么?抱歉,我问这个问题时不清楚,deck.getName()只返回一个字符串。如果我正确地遵循file的构造函数,file(file,fileName)等同于do。/Decks/fileName@EricH我想我的信息可能会更清楚。问题在于
新文件输出流(deckFile.getName())
。这将从
deckFile
中删除路径部分。它需要是
新文件输出流(deckFile)
Writer writer = new BufferedWriter(new OutputStreamWriter(
        new FileOutputStream(deckFile), "utf-8"));
writer.write(deck.toXml());
writer.close();