JAVA IO Flashcards Preview

Java8 > JAVA IO > Flashcards

Flashcards in JAVA IO Deck (59):
1

Some key points on the File System

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

2

What is a Path

String representation of a file/directory within the file system

3

What is the package of a File Class?

java.io.File

4

What is the File class used for?

1. Read information about existing files and directories
2. list the content of a directory
3. create/delete files and directories

5

What does the instance of the File class represents?

Path name of a particular file/directory

6

What can a File class cannot do?
* very important

Cannot read/write data within the File system.

7

Can a File class used to represent directories?

Yes

8

What is a relative path and what is absolute path?

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

9

Which slashdoes Unix and windows use

Unix: /
Windows: \

10

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

1. System property
System.getProperty("file.separator");
2. Static variable
java.io.File.separator

11

Method to find whether the file exists or not?

file.exists()

12

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

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");

13

What is getName() does?

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

14

What is getAbsolutePath()?

returns the absolute pathname String of the file/directory

15

lastModified()

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

16

delete()

deletes the file/directory

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

17

mkdirs()

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

18

listFiles()

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

19

How can the contents of the file accessed/written?

Streams, which is a list of data elements presented sequentially

20

How to conceptually remember a Stream?

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

21

What are the three input streams java provides?

System.in, System.err, System.out

22

Difference b/w Streams and Reader/Writer classes?

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

23

Are Readers/Writers are streams?

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

24

Why we use character Streams?

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

How can we differentiate streams as levels?

Low-Level Stream and High-Level Streams.

26

What is Low-level Streams?

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

What are high level Sreams?

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

28

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

1. InputStream
2. OutputStream
3. Reader
4. Writer

29

Which is the parent class of PrintStream?

OutputStream

30

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?

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

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

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

32

Why do we need to use flush() methods?

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

What are the two methods InputStream and Reader classes have?

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

34

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

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

method to skip over some data?

skip(long)

36

What are FileInputStream and FileOutPutStream?

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

37

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

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

38

What does the read method in FileInputStream method returns?

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

What does the overloaded read() method do?

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

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

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

What are PrintStream and printWriter classes?

1. High level stream classes
2. Writes formatted representation of java objects to a text-based output stream

42

Which instances does the PrintStream and PrintWriter classes operates?

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

43

What is special about PrintStream and PrintWriter?

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

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

PrintStream classes

45

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

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

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

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

47

print() method is overloaded with what?

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

48

Key point to remember for PrintStream on methods used in PrintWriter

We can use all te methods used in PrintWriter with printStream

49

Whar are all the arguments that teh format method takes?

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

50

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

printf() and format() methods

51

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

java.io.console

52

Which pattern does the Console class follows?

Console follows SingletonPattern.

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

53

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

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

54

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

It does not take an argument of locale variable

55

How does the basic readLine mthod works?

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

56

What about the overloaded readLine() method?

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

57

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

1. Echoing is disabled in readPassword()
2. readPassword() returns an arrayof characters instead of String

58

How can we wipe an arrays data?

Arrays.fill(array_variable, 'x');

59

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

An exception may be thrown at run time.

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