3

I've tried several different things to get this to work, and none of them have. I'm trying to create a file inside of a folder in Java. The project needs to have several text files that all relate to each other, and it would be more manageable to have them all together in one folder. Ideally, this folder would be stored outside of scr/.

Here is my current code for it (I do check for file existence first):

File testFile = new File("\\appts\\Appointments" + name + ".txt");
try {
    testFile.createNewFile();
} catch (Exception e) {
    e.printStackTrace();
}

However, I get an IOException when I try to run the code. I have tried it how it is above, also /appts/Appointments, appts/Appointments, appts\\Appointments. I tried searching online but couldn't find anything that worked.

Edit: Here is how my project setup currently looks like:

Project_Folder
    src
        com
            weebly
                roboticplayer
                    appointmentbook
                        CLASSES

Here is how I want it to look:

Project_Folder
    src
        com
            weebly
                roboticplayer
                    appointmentbook
                        CLASSES
    appts
RoboticPlayer
  • 43
  • 1
  • 9
  • And what is the full stack trace of the exception? – JB Nizet Mar 03 '17 at 23:47
  • Which OS are you using? – smttsp Mar 03 '17 at 23:49
  • `java.io.IOException: The system cannot find the path specified` `at java.io.WinNTFileSystem.createFileExclusively(Native Method)` `at java.io.File.createNewFile(Unknown Source)` `at com.weebly.roboticplayer.appointmentbook.AppointmentRunner.main(AppointmentRunner.java:55) ` Line 55: `testFile.createNewFile();` OS is Windows 10, IDE is Eclipse – RoboticPlayer Mar 03 '17 at 23:49
  • 1
    So, what don't you understand in the message? You're trying to create a file under a directory path that does not exist. Make sure the directories exist. And last time I checked, an absolute windows path looks like c:\foo\bar (Java also accepts c:/foo/bar). – JB Nizet Mar 03 '17 at 23:51
  • So then how would I create the path? That's my whole question. Other posts here had said that you can create a file in a directory using the `File` class. – RoboticPlayer Mar 03 '17 at 23:53
  • 1
    Yes, you can. But the File class has more than one method. They're all documented and say clearly what they do. Read the javadoc, that's what it's for: http://docs.oracle.com/javase/8/docs/api/java/io/File.html#mkdirs-- – JB Nizet Mar 03 '17 at 23:58

3 Answers3

3

There are two easy ways to do this:

1) Absolute path

  "C:\\users\\....\\parent_folder\\filename.txt";

2) relative path,

 . (Single dot) is current directory
 ..(double dots) is parent directory

For example, you want to create text files under project folder. And the following is your file structure.

Project_folder
    src
        Java_main_file.java
    appts

You want to create a file under appts from Java_main_file.java

String filename = "..\\appts\\filename.txt"

Then, create your file with filename. Here is a link how to create a text file.

Note that you need to make sure the folder under which you create the files exists. If it doesn't exist, you will get an error.

Community
  • 1
  • 1
smttsp
  • 4,011
  • 3
  • 33
  • 62
  • But my question was how do I make the folder. You told me how to put the file in the folder, which is helpful, but I still don't know how to make the folder. I tried calling `testFile.mkdir();` but it did nothing. – RoboticPlayer Mar 04 '17 at 00:36
  • Assume you want to create `appts` folder under `Project_folder`. `new File("appts\\").mkdirs()` will create the folder. – smttsp Mar 04 '17 at 00:38
  • Edited post with how I want it to look. It compiles and runs fine and everything, but I can't find the file or folder. – RoboticPlayer Mar 04 '17 at 00:45
0

You can't create a file and its non-existent parent directories in one step, but you can create the parent directories first with File.mkdirs() and then create the file.

0

If your using JDK7 you can use nio package.

 Path path = Paths.get("C:\\appts\\Appointments");
 Files.createDirectories(path);

If you want to do a path relative to your current folder:

 FileSystems.getDefault().getPath("appts", "Appointments");

If you want to see the absolute pathname:

 FileSystems.getDefault().getPath("appts", "Appointments").toAbsolutePath().toString();

If you need that file object:

 FileSystems.getDefault().getPath("appts", "Appointments").toFile();

You can also do .toFile() after the call toAbsolutePath().

Carlos Cook
  • 141
  • 3