-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)?

4
  • in your question, tell us what is happening, what is going wrong, what you expect to happen. Commented May 24, 2013 at 3:34
  • 4
    And ask a question... Commented May 24, 2013 at 3:34
  • To create a new file, you might have a look at File.createNewFile(). Commented May 24, 2013 at 3:35
  • EDIT: Here is an answer 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. Commented May 24, 2013 at 4:47

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

How exactly could I do this? A few lines of code would help :3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.