有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何更改保存路径

我创建了xml文档,但它们默认保存到netbeans项目中的项目文件夹中

我有一个需要保存的文件路径:

public static String XML_DIR = "c:/Users/ericrea/Desktop/413final";

以下是我迄今为止编写的代码,我尝试使用f.renameto()方法,但它不接受字符串文件位置:

DOMSource source = new DOMSource(testDoc);

        File f;
        f=new File(emp.getId() + ".xml");

        f.createNewFile();

        PrintStream ps = new PrintStream(f);
        StreamResult result = new StreamResult(ps);

        TransformerFactory transformerFactory = TransformerFactory
            .newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        transformer.transform(source, result);

共 (1) 个答案

  1. # 1 楼答案

    试试像这样的东西

    File f = new File (XML_DIR, emp.getID() + ".xml");
    

    这将符合

    File(String parent, String child) Creates a new File instance from a parent pathname string and a child pathname string.

    建造师

    File f = new File (XML_DIR + emp.getID() + ".xml" );
    

    这将使用

    File(String pathname) Creates a new File instance by converting the given pathname string into an abstract

    路径名。 文件对象的构造函数。任何一个都应该给你你想要的结果

    有关文件对象here的详细信息