JAVA IO Flashcards

1
Q

Some key points on the File System

A
  1. A file is a record within the file system that stores data.
  2. Files are organized into directories.
  3. Directory is a record in the file system that contains files as well as dircetories
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Path

A

String representation of a file/directory within the file system

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

What is the package of a File Class?

A

java.io.File

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

What is the File class used for?

A
  1. Read information about existing files and directories
  2. list the content of a directory
  3. create/delete files and directories
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the instance of the File class represents?

A

Path name of a particular file/directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What can a File class cannot do?
* very important
A

Cannot read/write data within the File system.

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

Can a File class used to represent directories?

A

Yes

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

What is a relative path and what is absolute path?

A
  1. Relative path is the path from the current working directory to file or directory
  2. Absolute Path is the path of file/directory from root directory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which slashdoes Unix and windows use

A

Unix: /
Windows: \

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

What are the two options to retrieve the local seperator character?

A
  1. System property
    System.getProperty(“file.separator”);
  2. Static variable
    java.io.File.separator
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Method to find whether the file exists or not?

A

file.exists()

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

What are the two constructors generally used for creating a file Instance

A
  1. One is taking String as argument representing relative/absolute path
  2. two arguments, one which is existing file path and other relative child path.

File file = new File(“C:\Dilli.txt”);

File file2 = new File(file, “hello\Dilli.txt”);

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

What is getName() does?

A

getName returns the name of the directory / file denoted by this path

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

What is getAbsolutePath()?

A

returns the absolute pathname String of the file/directory

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

lastModified()

A

returns the no of milliseconds since the epoch when the file was last modified

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

delete()

A

deletes the file/directory

If the pathname denotes a directory, then the directory must be empty in order to be deleted

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

mkdirs()

A

creates the directory named by this paths including any non-existent parent directories

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

listFiles()

A

returns the File[] Array returning the files on the directory

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

How can the contents of the file accessed/written?

A

Streams, which is a list of data elements presented sequentially

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

How to conceptually remember a Stream?

A

Think Stream as a long, never ending Stream of water with data presented one “wave” at a time

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

What are the three input streams java provides?

A

System.in, System.err, System.out

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

Difference b/w Streams and Reader/Writer classes?

A
  1. Stream classes are used for inputting and outputting all types of binary or byte data
  2. Reader/Writer classes are used for inputting and outputting only character and String data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Are Readers/Writers are streams?

A

Yes, even though their names doesnt contain Streams in it, they are streams

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

Why we use character Streams?

A
  1. There are advantages of using reader/writer clsses as they are specifically focussed on managing character and string data
  2. WE DONT WANT TO WORRY ABOUT THE UNDERLYING BYTE ENCODING OF THE FILE
25
Q

How can we differentiate streams as levels?

A

Low-Level Stream and High-Level Streams.

26
Q

What is Low-level Streams?

A

Low level Streams connects directly with the source of the data such as files, arrays or Strings.
They process the raw data or resources and are accessed in a direct or unfiltered manner.

27
Q

What are high level Sreams?

A

High level Streams is built on anoter Stream using wrapping. (An instance is passed as constructor to another instance)

28
Q

What are the four abstract classes that are parent for all streams?

A
  1. InputStream
  2. OutputStream
  3. Reader
  4. Writer
29
Q

Which is the parent class of PrintStream?

A

OutputStream

30
Q
new BufferedInputStream(new FileReader("C:\\hello.txt"));
		new BufferedWriter(new FileOutputStream("C:\\hello.txt"));
		new ObjectInputStream(new FileOutputStream("C:\\hello.txt"));
		new BufferedInputStream(new InputStream("hello.txt"));

will these comile?

A

No. First two mix Reader/Writer classes with InputStream/OutputStream classes.

Third does not compile bcause it mixes with Input Stream with Output Steam

4th doesnt compile because, InputStream is a abstract class, no constructor is allowed

31
Q

What happens if we do not close() a file operation?

A
  1. Failing to close may leave the OS lock the file, so that no other processes could read/write until program is terminated.
32
Q

Why do we need to use flush() methods?

A

When the data is written to the OutputStream, the underlying operations system doesnt necessarily guarantee that the data will be written to File system.

Sometimes, the data will be cached and written after sometime. INI the meantime, if application termintates then the data is lost

33
Q

What are the two methods InputStream and Reader classes have?

A

mark(int) and reset to move the stream back to earlier position

34
Q

Which method should we call before calling the MARK / reset method? Why?

A
We should call markSupported() method. 
Because, not all Stream classes support these operations and calling mark(int) or reset on a class that does not support these operations will throw runtime Exceptions.

So, markSupported() returns true only if mark is supported.

35
Q

method to skip over some data?

A

skip(long)

36
Q

What are FileInputStream and FileOutPutStream?

A

They are used to read bytes from files / write bytes to the files

37
Q

What data can the constructor can take as argument for FileInputStream/FileOutputStream?

A

They take file objects or String, representing the path to the file

38
Q

What does the read method in FileInputStream method returns?

A

It returns int. So that when the EOF is reached, it returns ‘-1’, from which we can predict it is the end of file.

39
Q

What does the overloaded read() method do?

A
  1. It takes a pointer to a byte array where the data is written. iT returns the integer representing how many data can be read into byte array
40
Q

which method does FileOutputStream takes to write to a file and its overloaded version?

A
  1. write(int) method - writes successive bytes to file

2. Overloaded write methodmethod, that allow byte array to be passed and can be used by biffered class

41
Q

What are PrintStream and printWriter classes?

A
  1. High level stream classes

2. Writes formatted representation of java objects to a text-based output stream

42
Q

Which instances does the PrintStream and PrintWriter classes operates?

A
  1. PrintStream classes operates on OutputStream instances and writes data as bytes
  2. PrintWriter operates on Writer instances and writes data as characters
43
Q

What is special about PrintStream and PrintWriter?

A
  1. They can open and write to files directly.

PrintWriter class can even take an OutPutStream as input, allowing us to wrap the PrintWriter class

44
Q

Which object does the System.out and System.err classes belongs to?

A

PrintStream classes

45
Q

Does PrintBased methods throw any exceptions? Cool way to remember this?

A
  1. They do not throw any exceptions
  2. Whenever, we used System.out.println() we have not checked for any exceptions, which indicates that they dont throw any exceptions
46
Q

What is the method used to detect any error in Print based methods?

A

checkError(), used to detect te presence of a problem after attempting to write data to stream

47
Q

print() method is overloaded with what?

A

with all java primitives and String and Objects. It also internally calls String.valueOf() method of arguments passed

48
Q

Key point to remember for PrintStream on methods used in PrintWriter

A

We can use all te methods used in PrintWriter with printStream

49
Q

Whar are all the arguments that teh format method takes?

A
  1. It takes a String, an Optional locale and set of arguments
50
Q

What are the two methods that could be used interchangebly in java formatting Strings?

A

printf() and format() methods

51
Q

Which is the recommended methods for interacting with the user and displaying information in the test-based environment?

A

java.io.console

52
Q

Which pattern does the Console class follows?

A

Console follows SingletonPattern.

It is created automatically by JVM and accessd by calling System.console()

53
Q

What are the methods provided by the Console for the reading and writing operations?

A

reader() and writer() to access an istance of Reader and PrintWriter

54
Q

How does the format() method differs from the Stream format() method?

A

It does not take an argument of locale variable

55
Q

How does the basic readLine mthod works?

A
  1. It works by retreiving the single line of text from the user and the user presses the Ener button to terminate it
56
Q

What about the overloaded readLine() method?

A

signature is: readLine(String format, Object… args), which displays the formatted prompt to the user

57
Q

What is different in readPassword() method when compared to readLine() method?

A
  1. Echoing is disabled in readPassword()

2. readPassword() returns an arrayof characters instead of String

58
Q

How can we wipe an arrays data?

A

Arrays.fill(array_variable, ‘x’);

59
Q

What happens if we call reset() after we have pases the mark position?

A

An exception may be thrown at run time.

may- because some implementations may use buffer to allow extra data to be read