Java File Handling Flashcards

1
Q

Which package contains the File class?

A

The java.io class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the syntax to import the file class?

A

import java.io.File; // Import the File class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the syntax for specifying a new file?

A

File myObj = new File(“filename.txt”); // Specify the filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are some of the most often used methods on the file class?

A

canRead() , canWrite(), createNewFile() , delete() , exists() , getName() , length() , mkdir()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the syntax to import an exception?

A

import java.io.IOException; // Import the IOException class to handle errors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What class needs to be imported to write to a file?

A

import java.io.FileWriter; // Import the FileWriter class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the syntax to make the file to be writable?

A

FileWriter myWriter = new FileWriter(“filename.txt”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the method to write to a file?

A

write()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the syntax to write to a file?

A

FileWriter myWriter = new FileWriter(“filename.txt”);
myWriter.write(“Files in Java might be tricky, but it is fun enough!”);
myWriter.close();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What class is used to read contents of a text file?

A

Scanner class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the syntax to import the Scanner class?

A

import java.util.Scanner; // Import the Scanner class to read text files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the syntax to read a file?

A
File myObj = new File("filename.txt");
      Scanner myReader = new Scanner(myObj);
      while (myReader.hasNextLine()) {
        String data = myReader.nextLine();
        System.out.println(data);
      }
      myReader.close();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are some of the methods and syntax to get more information about a file?

A
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());
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the syntax to delete a file?

A

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.");
    } 
  } 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the syntax to delete a folder?

A

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.");
    } 
  } 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the file class?

A

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.
17
Q

What are some of the constructor options?

A

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.

18
Q

What is another way to create a new file with the file output stream?

A

String data = “Test data”;

FileOutputStream out = new FileOutputStream(“c://temp//testFile2.txt”);

out. write(data.getBytes());
out. close();

19
Q

What are the steps to create a file?

A

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

20
Q

How to create a file in Java 8?

A
//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 !!");
}
21
Q

What is a stream?

A

Is how data is handled in Java and is a stream of data

22
Q

What is the method to write to a file?

A
Buffereed writer class with FileWriter bw.write
bw.newLine()
23
Q

How to read a file?

A

BufferedReader, FileReader and then loop over the line, readLine and it will read all of the lines

24
Q

What are the two classes to read files?

A

BufferedReader & Scanner class