From the course: Advanced Java Programming
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Creating a new file - Java Tutorial
From the course: Advanced Java Programming
Creating a new file
- In this example, I want to create a new file on my computer. I don't actually want to put anything in it at the moment, I just want it to exist. If I wanted to write something to a file, I would need to use either a file output stream or a file writer, but to simply create a file, I don't need to use these. In the main method of my file-creation example class, I will create a new file object called myFile. I'll put = new File(). Then I need to pass in the path of where I would like the file to be. For now, I will just put the file on my desktop, and I will call it myFile.txt. I also need to add input statements to use File. If I run the program now, it builds successfully. However, I haven't actually created a file, I've just created a file object. This is just an abstract representation of a file. If I open my desktop folder, there is no file called myFile.txt. To create a file, I need to call another method, so I need to do myFile.createNewFile(). I will also need to handle the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.