如果文件名包含'#',则在java.io.File(String)中截断文件名

时间:2014-10-18 09:49:56

标签: java file-io

我正在尝试使用java.io.File(String)创建一个新文件。如果文件路径字符串包含“#”符号,则创建的文件名称将被截断。请有人解释我为什么会这样,以及如何使用java中的'#'创建文件名。

我的代码。

new File("d:\\file#test.xml");

预期产出:

file#test.xml

实际输出:

file

注意:我需要在windows和Unix文件系统中创建文件。

通常情况下,允许Windows和Unix系统使用#。

创建文件名

编辑:

感谢您的回复。是的,问题不在java.io.File(String)中。实际上,当我尝试使用xml转换器创建xml文件时,我遇到了这个问题。

请在下面找到完整的代码。

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;


public class XMLWriterDOM {

public static void main(String[] args) {

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder dBuilder;

    try {

        dBuilder = dbFactory.newDocumentBuilder();

        Document doc = dBuilder.newDocument();

        //add elements to Document

        Element rootElement =

            doc.createElementNS("http://www.journaldev.com/employee", "Employees");

        //append root element to document

        doc.appendChild(rootElement);



        //append first child element to root element

        rootElement.appendChild(getEmployee(doc, "1", "Pankaj", "29", "Java Developer", "Male"));



        //for output to file, 

        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        Transformer transformer = transformerFactory.newTransformer();

        //for pretty print

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        DOMSource source = new DOMSource(doc);




        StreamResult file = new StreamResult(new File("d:\\\\emps#1.xml"));



        //write data



        transformer.transform(source, file);

        System.out.println("DONE");



    } catch (Exception e) {

        e.printStackTrace();

    }

}





private static Node getEmployee(Document doc, String id, String name, String age, String role,

        String gender) {

    Element employee = doc.createElement("Employee");



    //set id attribute

    employee.setAttribute("id", id);



    //create name element

    employee.appendChild(getEmployeeElements(doc, employee, "name", name));



    //create age element

    employee.appendChild(getEmployeeElements(doc, employee, "age", age));



    //create role element

    employee.appendChild(getEmployeeElements(doc, employee, "role", role));



    //create gender element

    employee.appendChild(getEmployeeElements(doc, employee, "gender", gender));



    return employee;

}





//utility method to create text node

private static Node getEmployeeElements(Document doc, Element element, String name, String value) {

    Element node = doc.createElement(name);

    node.appendChild(doc.createTextNode(value));

    return node;

}



}

请建议我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我无法在Mac OS X下重现您的问题。

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) {
        String temp = System.getProperty("java.io.tmpdir");
        Path path = Paths.get(temp, "foo#bar.txt");
        System.out.println(path.toAbsolutePath());
    }
}

输出:

/var/folders/qm/0t0z2hfx6lb53gf7d8h2srzm0000gp/T/foo#bar.txt

输出的代码是什么?