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.
Working with directories in Java - Java Tutorial
From the course: Advanced Java Programming
Working with directories in Java
- [Instructor] Sometimes it is useful to be able to work with directories in a file system. For example, you might need to list files in a directory or create a new directory. To list everything inside a directory, I first need to create an array of strings which I will call contents. Then I put equals new file. I want to list everything in my current directory so I just need to put a speech mark and a dot between them. Then I call the list method. I also need to make sure I have the imports for file. Now I have an array containing the name of every item in my current directory. I will add a simple for loop to print out each string in the array on a new line. When I run the program, I see all of the contents in my current directory. Some of them are files, for example, build.xml and manifest.mf. The others, which do not have file extensions, are more directories. It is also simple to create new directories. If I want to create a new directory called myDirectory, I just create a new…
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.