JAVA IO Flashcards
Some key points on the File System
- A file is a record within the file system that stores data.
- Files are organized into directories.
- 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?
java.io.File
What is the File class used for?
- Read information about existing files and directories
- list the content of a directory
- 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?
Yes
What is a relative path and what is absolute path?
- Relative path is the path from the current working directory to file or directory
- Absolute Path is the path of file/directory from root directory
Which slashdoes Unix and windows use
Unix: /
Windows: \
What are the two options to retrieve the local seperator character?
- System property
System.getProperty(“file.separator”); - Static variable
java.io.File.separator
Method to find whether the file exists or not?
file.exists()
What are the two constructors generally used for creating a file Instance
- One is taking String as argument representing relative/absolute path
- 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
lastModified()
returns the no of milliseconds since the epoch when the file was last modified
delete()
deletes the file/directory
If the pathname denotes a directory, then the directory must be empty in order to be deleted
mkdirs()
creates the directory named by this paths including any non-existent parent directories
listFiles()
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?
- Stream classes are used for inputting and outputting all types of binary or byte data
- 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?
- There are advantages of using reader/writer clsses as they are specifically focussed on managing character and string data
- 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?
- InputStream
- OutputStream
- Reader
- Writer
Which is the parent class of PrintStream?
OutputStream
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?
- 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?
skip(long)
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?
- 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?
- 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?
- 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?
- PrintStream classes operates on OutputStream instances and writes data as bytes
- PrintWriter operates on Writer instances and writes data as characters
What is special about PrintStream and PrintWriter?
- 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?
PrintStream classes
Does PrintBased methods throw any exceptions? Cool way to remember this?
- They do not throw any exceptions
- 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?
- 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?
java.io.console
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?
- 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?
- Echoing is disabled in readPassword()
2. readPassword() returns an arrayof characters instead of String
How can we wipe an arrays data?
Arrays.fill(array_variable, ‘x’);
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