Flashcards in JAVA IO Deck (59):
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
What is a Path
String representation of a file/directory within the file system
What is the package of a File Class?
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
What does the instance of the File class represents?
Path name of a particular file/directory
What can a File class cannot do?
* very important
Cannot read/write data within the File system.
Can a File class used to represent directories?
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
Which slashdoes Unix and windows use
What are the two options to retrieve the local seperator character?
1. System property
2. Static variable
Method to find whether the file exists or not?
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");
What is getName() does?
getName returns the name of the directory / file denoted by this path
What is getAbsolutePath()?
returns the absolute pathname String of the file/directory
returns the no of milliseconds since the epoch when the file was last modified
deletes the file/directory
If the pathname denotes a directory, then the directory must be empty in order to be deleted
creates the directory named by this paths including any non-existent parent directories
returns the File Array returning the files on the directory
How can the contents of the file accessed/written?
Streams, which is a list of data elements presented sequentially
How to conceptually remember a Stream?
Think Stream as a long, never ending Stream of water with data presented one "wave" at a time
What are the three input streams java provides?
System.in, System.err, System.out
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
Are Readers/Writers are streams?
Yes, even though their names doesnt contain Streams in it, they are streams
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
How can we differentiate streams as levels?
Low-Level Stream and High-Level Streams.
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.
What are high level Sreams?
High level Streams is built on anoter Stream using wrapping. (An instance is passed as constructor to another instance)
What are the four abstract classes that are parent for all streams?
Which is the parent class of PrintStream?
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
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.
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
What are the two methods InputStream and Reader classes have?
mark(int) and reset to move the stream back to earlier position
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.
method to skip over some data?
What are FileInputStream and FileOutPutStream?
They are used to read bytes from files / write bytes to the files
What data can the constructor can take as argument for FileInputStream/FileOutputStream?
They take file objects or String, representing the path to the file
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.
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
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
What are PrintStream and printWriter classes?
1. High level stream classes
2. Writes formatted representation of java objects to a text-based output stream
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
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
Which object does the System.out and System.err classes belongs to?
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
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
print() method is overloaded with what?
with all java primitives and String and Objects. It also internally calls String.valueOf() method of arguments passed
Key point to remember for PrintStream on methods used in PrintWriter
We can use all te methods used in PrintWriter with printStream
Whar are all the arguments that teh format method takes?
1. It takes a String, an Optional locale and set of arguments
What are the two methods that could be used interchangebly in java formatting Strings?
printf() and format() methods
Which is the recommended methods for interacting with the user and displaying information in the test-based environment?
Which pattern does the Console class follows?
Console follows SingletonPattern.
It is created automatically by JVM and accessd by calling System.console()
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
How does the format() method differs from the Stream format() method?
It does not take an argument of locale variable
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
What about the overloaded readLine() method?
signature is: readLine(String format, Object... args), which displays the formatted prompt to the user
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
How can we wipe an arrays data?