Java File Handling Flashcards
Which package contains the File class?
The java.io class
What is the syntax to import the file class?
import java.io.File; // Import the File class
What is the syntax for specifying a new file?
File myObj = new File(“filename.txt”); // Specify the filename
What are some of the most often used methods on the file class?
canRead() , canWrite(), createNewFile() , delete() , exists() , getName() , length() , mkdir()
What is the syntax to import an exception?
import java.io.IOException; // Import the IOException class to handle errors
What class needs to be imported to write to a file?
import java.io.FileWriter; // Import the FileWriter class
What is the syntax to make the file to be writable?
FileWriter myWriter = new FileWriter(“filename.txt”);
What is the method to write to a file?
write()
What is the syntax to write to a file?
FileWriter myWriter = new FileWriter(“filename.txt”);
myWriter.write(“Files in Java might be tricky, but it is fun enough!”);
myWriter.close();
What class is used to read contents of a text file?
Scanner class
What is the syntax to import the Scanner class?
import java.util.Scanner; // Import the Scanner class to read text files
What is the syntax to read a file?
File myObj = new File("filename.txt"); Scanner myReader = new Scanner(myObj); while (myReader.hasNextLine()) { String data = myReader.nextLine(); System.out.println(data); } myReader.close();
What are some of the methods and syntax to get more information about a file?
File myObj = new File("filename.txt"); if (myObj.exists()) { System.out.println("File name: " + myObj.getName()); System.out.println("Absolute path: " + myObj.getAbsolutePath()); System.out.println("Writeable: " + myObj.canWrite()); System.out.println("Readable " + myObj.canRead()); System.out.println("File size in bytes " + myObj.length());
What is the syntax to delete a file?
import java.io.File; // Import the File class
public class DeleteFile { public static void main(String[] args) { File myObj = new File("filename.txt"); if (myObj.delete()) { System.out.println("Deleted the file: " + myObj.getName()); } else { System.out.println("Failed to delete the file."); } } }
What is the syntax to delete a folder?
import java.io.File;
public class DeleteFolder { public static void main(String[] args) { File myObj = new File("C:\\Users\\MyName\\Test"); if (myObj.delete()) { System.out.println("Deleted the folder: " + myObj.getName()); } else { System.out.println("Failed to delete the folder."); } } }
What is the file class?
The File class is Java’s representation of a file or directory path name. Because file and directory names have different formats on different platforms, a simple string is not adequate to name them. The File class contains several methods for working with the path name, deleting and renaming files, creating new directories, listing the contents of a directory, and determining several common attributes of files and directories.
It is an abstract representation of file and directory pathnames. A pathname, whether abstract or in string form can be either absolute or relative. The parent of an abstract pathname may be obtained by invoking the getParent() method of this class. First of all, we should create the File class object by passing the filename or directory name to it. A file system may implement restrictions to certain operations on the actual file-system object, such as reading, writing, and executing. These restrictions are collectively known as access permissions. Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change.
What are some of the constructor options?
Constructors
File(File parent, String child) : Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname) : Creates a new File instance by converting the given pathname string into an abstract pathname.
File(String parent, String child) : Creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri) : Creates a new File instance by converting the given file: URI into an abstract pathname.
What is another way to create a new file with the file output stream?
String data = “Test data”;
FileOutputStream out = new FileOutputStream(“c://temp//testFile2.txt”);
out. write(data.getBytes());
out. close();
What are the steps to create a file?
1 Need to import file Class from java.io
2 New to create a new file and input string to define where file to be stored
3 Run the create file method
How to create a file in Java 8?
//Get the file reference Path path = Paths.get("c:/output.txt");
//Use try-with-resource to get auto-closeable writer instance try (BufferedWriter writer = Files.newBufferedWriter(path)) { writer.write("Hello World !!"); }
What is a stream?
Is how data is handled in Java and is a stream of data
What is the method to write to a file?
Buffereed writer class with FileWriter bw.write bw.newLine()
How to read a file?
BufferedReader, FileReader and then loop over the line, readLine and it will read all of the lines
What are the two classes to read files?
BufferedReader & Scanner class