Input/Output and Streams in Java
Alex Liu
Software Development Engineer
$$
$$
$$


                  Java File Operations
                           Creation
                            Deletion
                Directory management

                  Java File Operations        Iterators and Streams
                           Creation                   Processing collections
                            Deletion                     Transforming data
                Directory management

                  Java File Operations        Iterators and Streams      Custom Methods and More
                           Creation                   Processing collections               Build scalable 
                            Deletion                     Transforming data                 Java applications
                Directory management
Import the File class
import java.io.File;
Make a File object named dataTextFile
File dataTextFile = new File("data.txt");
To create the file on the computer, call the .createNewFile() method
boolean result = dataTextFile.createNewFile();
true if the file is created successfully; false if it already existsCreate a File object named exampleFile that references the file we want to remove
File exampleFile = new File("example.txt");
Use the .delete() method to attempt to delete the file
boolean deleted = exampleFile.delete();
true if the file was deletedfalse if file could not be deleted (e.g., due to insufficient permissions).exists() method returns true if the file exists// Use `.exists()` to check if a file already exists on our disk
if (dataTextFile.exists()) {
    // Print message if file exists
    System.out.println("The file already exists.");
} else {
    // Attempt to create the file if it doesn't exist
    boolean result = dataTextFile.createNewFile();
}
IOExceptiontry {
    //Any file operation
} catch (IOException e) {
    e.getMessage();
}
Input/Output and Streams in Java