I use the JDOM library. When I write information into an xml file, Eclipse shows errors. The system cannot find the path specified. I try to create the file in the "language" folder. How can I create the folder automatically when I write info into this file? I think the error is in this line:

FileWriter writer = new FileWriter("language/variants.xml");

Here is my code:

package test;

import java.io.FileWriter;
import java.util.LinkedList;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

class Test {
    private LinkedList<String> variants = new LinkedList<String>();

    public Test() {

    }
    public void write() {

        Element variantsElement = new Element("variants");
        Document myDocument = new Document(variantsElement);

        int counter = variants.size();
        for(int i = 0;i < counter;i++) {
            Element variant = new Element("variant");
            variant.setAttribute(new Attribute("name",variants.pop()));
            variantsElement.addContent(variant);
        }


        try {
            FileWriter writer = new FileWriter("language/variants.xml");
            XMLOutputter outputter = new XMLOutputter();
            outputter.setFormat(Format.getPrettyFormat());
            outputter.output(myDocument,writer);
            writer.close();
        }
        catch(java.io.IOException exception) {
            exception.printStackTrace();
        }
    }
    public LinkedList<String> getVariants() {
        return variants;
    }
}

public class MyApp {
    public static void main(String[] args) {
        Test choice = new Test();
        choice.write();
    }
}

Here is the error:

java.io.FileNotFoundException: language\variants.xml (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
    at java.io.FileWriter.<init>(FileWriter.java:63)
    at test.Test.write(MyApp.java:31)
    at test.MyApp.main(MyApp.java:49)`enter code here
有帮助吗?

解决方案

As the name suggests FileWriter is for writing to file. You need to create the directory first if it doesnt already exist:

File theDir = new File("language");
if (!theDir.exists()) {
  boolean result = theDir.mkdir();  
  // Use result...
}
FileWriter writer = ...

其他提示

For creating directories you need to use mkdir() of File class. Example:

File f = new File("/home/user/newFolder");
f.mkdir();

It returns a boolean: true if directory created and false if it failed.

mkdir() also throws Security Exception if security manager exists and it's checkWrite() method doesn't allow the named directory to be created.

PS: Before creating directory, you need to validate if this directory already exists or not by using exists() which also returns boolean.

Regards...

Mr.777

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top