0

I've got Problems, when I try to generate a file in order to write into. I don't get any exceptions and the sysouts are also displayed, but the File is not generated.. Is there something I don't see..? this is the class, which does everything:

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;
}
}

And it is called like this:

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

for the context: I've got a Spring 3 Application which connects to a Database and I want to write result sets into a generated xml-File! I just copied the code from another application where it worked properly

bethlis
  • 55
  • 1
  • 3
  • 15
  • you mean something like file.createNewFile()...? I tried a view seconds ago, but it didn't work either... Is Spring special about that..? – bethlis Oct 23 '12 at 14:59
  • it's strange, because I tested the code in a smaller context, before pasting it to the bigger on and there it worked fine! – bethlis Oct 23 '12 at 15:33
  • 1
    You don't need to call `createNewFile`(). Clearly the file is being createed somewhere where you're not looking for it. – user207421 Oct 25 '12 at 10:01

1 Answers1

0

Try by giving full path like "C:\Users\DELL\Desktop\myfile.txt" in the code

File file=new File("C:\Users\DELL\Desktop\myfile.txt");
Ruli
  • 2,592
  • 12
  • 30
  • 40