Made with ScholarlyAsk questions about your own PDFs and videos
Shared by Nathan Guinto
enum keyword and separate the constants with commas. Constants are typically written in uppercase letters.Example of an enum declaration:
enum Level { LOW, MEDIUM, HIGH }
LevelLOW, MEDIUM, HIGHLevel myVar = Level.MEDIUM;
myVar holds the enum value MEDIUM from the enum Level.final keyword.Enums can also be defined inside classes:
public class Main { enum Level { LOW, MEDIUM, HIGH } public static void main(String[] args) { Level myVar = Level.MEDIUM; System.out.println(myVar); } }
Enums can be effectively used within switch statements. Example:
public class Main { public static void main(String[] args) { Level myVar = Level.MEDIUM; switch(myVar) { case LOW: System.out.println("Low level"); break; case MEDIUM: System.out.println("Medium level"); break; case HIGH: System.out.println("High level"); break; } } }
Output: Medium level
You can loop through all constants in an enum using the values() method:
for (Level myVar : Level.values()) { System.out.println(myVar); }
Output:
LOW MEDIUM HIGH
public, static, and final.Use enums for fixed sets of constants, such as days of the week, months, or game levels.
File class from the java.io package for file operations:import java.io.File;
File myObj = new File("filename.txt");
| Method | Type | Description |
|---|---|---|
canRead() |
Boolean | Checks if the file is readable. |
canWrite() |
Boolean | Checks if the file is writable. |
createNewFile() |
Boolean | Creates an empty file if it does not exist. |
delete() |
Boolean | Deletes a specified file. |
exists() |
Boolean | Verifies if the file exists. |
getName() |
String | Gets the name of the file. |
getAbsolutePath() |
String | Fetches the absolute path of the file. |
length() |
Long | Returns the file's size in bytes. |
list() |
String[] | Returns files in a directory. |
mkdir() |
Boolean | Creates a directory. |
To check if the file is readable or writable:
if (myObj.canRead()) { System.out.println("File is readable."); } else { System.out.println("File is not readable."); } if (myObj.canWrite()) { System.out.println("File is writable."); } else { System.out.println("File is not writable."); }
Files cannot be created directly upon object instantiation. Use createNewFile() within a try-catch block to manage exceptions:
try { if (myObj.createNewFile()) { System.out.println("File created successfully."); } else { System.out.println("File already exists."); } } catch (IOException e) { System.out.println("An error occurred."); }
Use Scanner or BufferedReader to read files. Example using Scanner:
try { File myObj = new File("filename.txt"); Scanner myReader = new Scanner(myObj); while (myReader.hasNextLine()) { String data = myReader.nextLine(); System.out.println(data); } myReader.close(); } catch (FileNotFoundException e) { System.out.println("An error occurred."); }
Use FileWriter to write text content:
try { FileWriter myWriter = new FileWriter("filename.txt"); myWriter.write("Hello World!"); myWriter.close(); } catch (IOException e) { System.out.println("An error occurred."); }
To delete files or folders use the delete() method. Folders must be empty before deletion:
if (myObj.delete()) { System.out.println("Deleted the file."); } else { System.out.println("Failed to delete the file."); }
Use a helper method to create a CSV file:
private static void createCsvFileIfNotExists(String filename, String[][] data) { File file = new File(filename); // implement CSV creation logic... }
To read from a CSV file, split lines by commas:
private static String[][] readCsvFile(String filename) { String[][] data = new String[100][]; // read logic }
Writing data to CSV is similar to writing to text files using FileWriter with joined strings:
private static void writeCsvFile(String filename, String[][] data) { // writing logic }
Make from this