-3

Okay, updated this right now

As of now I have this:

File saveGame = new File(Config.saveDir,Config.saveName);

Now how would I create that into a text file? My saveDir has already been created (mkDir), and my saveName is defined as "xyz.txt", so what method do I use to create this file (and later add text into it)?

  • in your question, tell us what is happening, what is going wrong, what you expect to happen. – Sean F May 24 '13 at 03:34
  • 4
    And ask a question... – MadProgrammer May 24 '13 at 03:34
  • To create a new file, you might have a look at `File.createNewFile()`. – James Montagne May 24 '13 at 03:35
  • EDIT: Here is an [answer](http://stackoverflow.com/a/1053475/778118) that shows how to write text to a file. I found it by googling "_java write text to file_" and clicking the first result (I originally posted the wrong link, sorry). The accepted answer is best if you can use _Apache Commons IO_, but for homework the linked answer should be exactly what you want. – jahroy May 24 '13 at 04:47

1 Answers1

2

new File(...) doesn't crete a file on disk, it only creates a path for a Java program to use to refer to a file that may or may not exist on disk (hence the File.exists() method).

Try userFile.createNewFile(); to actually create the file on disk.

To make the directory you would need to use the File.mkdirs() method, but don't call it on userFile or it will make a directory with the Savegame.txt in it.

Edit:

File dir = new File(Config.userpath + "/test/");
File file = new File(dir, "," + Config.name + " Savegame.txt");

dir.mkdir(); // should check to see if it succeeds (if(dir.mkdir())...)
file.createNewFile(); // should also check that this succeeds.
TofuBeer
  • 60,850
  • 18
  • 118
  • 163