0

当我尝试生成文件以便写入时,我遇到了问题。我没有得到任何异常,系统输出也显示了,但是没有生成文件..有什么我看不到的..?这是类,它可以做所有事情:

public class GraphMLWriter {

public FileWriter writer;
public File file;

private static GraphMLWriter instance;

private GraphMLWriter() {
    initFile();
}

public void initFile() {
    file = new File("myFile.txt");

    try {
        writer = new FileWriter(file, true);
                    System.out.println("File erzeugt");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public void writeToFile() {
    try {
        writer.write("tolltoll");
        writer.write(System.getProperty("line.separator"));
        writer.write("es klappt");
        writer.flush();
        writer.close();
        System.out.println("File geschrieben");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static synchronized GraphMLWriter getInstance() {
    if (instance == null) {
        instance = new GraphMLWriter();
    }
    return instance;
}
}

它是这样调用的:

writer = GraphMLWriter.getInstance();
writer.writeToFile();

对于上下文:我有一个连接到数据库的 Spring 3 应用程序,我想将结果集写入生成的 xml 文件!我刚刚从另一个可以正常工作的应用程序中复制了代码

4

1 回答 1

0

尝试在代码中提供完整路径,如“C:\Users\DELL\Desktop\myfile.txt”

File file=new File("C:\Users\DELL\Desktop\myfile.txt");
于 2021-03-06T04:14:38.213 回答