WickedProduction
9/27/2019 - 11:15 PM

Java IO - Files and Directories

package me.illuminatiproductions;

import java.io.File;

public class Main {

    public static void main(String[] args) {

        //File Class -  Get the properties of a file or directory
        File file1 = new File("E:\\My Cool File"); //folder i made
        File file2 = new File("E:\\My Cool file", "thing.txt"); //a file in the folder
        File file3 = new File(file1, "thing.txt"); //use the file object to make a new file object

        //Use the methods of the file object to obtain info
        System.out.println("File name: " + file3.getName());
        System.out.println("Path: " + file3.getPath());
        System.out.println("Parent Folder: " + file3.getParent());
        System.out.println(file3.isFile()); //Find out if it's a file or directory

        System.out.println("Is this a directory? " + file1.isDirectory());
        //print out the contents of the directory
        String files[] = file1.list();
        for (int i = 0; i < files.length; i++){
            System.out.println(files[i]);
        }
        
    }
}