System class□ System class has three attributes □ in (System.in) □ Object of class InputStream □ out (System.out) □ Object of OutputStream □ err (Error Stream) □ Object of ErrorStream
Introduction□ We need to have some method of storing data permanently – even when the computer is switched off and program has been terminated□ We need to store multiple records, each consist of multiple fields in a file
File Handling□ Java provide File streams – Input and Output streams that handle communication between main memory and named file on a disk□ We can write data to a file in the form of Strings, lines of text or basic types such as integers or characters.□ Java even allows us to store and retrieve whole objects
A file on a disk or tape can be a text file or a binary file.
The standard input and output behave like text files.
The standard error behaves like a text file.
File streams are created, connected to files, and disconnected from files by the programmer.
Input and Output□ Input □ Allowing information to come in from outside world □ Transfer of data from some external device to main memory□ Output □ Display or storage of processed information □ Transfer of data from main memory to an external device□ In order to have Input and Output , a channel of communication is required referred to as a stream□ We have standard input and output stream which is keyboard and screen□ We have standard error stream which is also set to screen
One of the most frequently usedtask in programming is writing to and reading from a file. To do this in Java there are more possibilities.
Encoding□ Java supports three different ways of encoding data □ Text □ Data on disk is stored as characters in the form used by external system □ ASCII normally but as java uses UNICODE so internally some transformation do happen □ Readable by text editor □ Binary □ Data is stored in same format as the internal representation of the data used by the program to store data so number 107 will be stored as 1101011. □ Cannot be properly read by text editor □ Object □ Whole object can be written
Reading and writing to text files Writing to a File Reading from a FileFileWriter name = new Filereader name = new FileWriter(“Filename”); FileReader(“Filename”);PrintWriter printname = new BufferedReader buffername PrintWriter(name); = new BufferedReader(name);printname.println(data ); String str = buffername.readLine();Printname.close(); If(str == null) => End of File Buffername.close();
Filename handling□ To write anything to a file first of all we need a file name we want to use.□ The file name is a simple string like: □ String fileName = "test.txt";□ If you want to write in a file which is located elsewhere you need to define the complete file name and path in your fileName variable: □ String fileName = "c:filedemotest.txt";
Open a file□ To open a file for writing use the FileWriter class and create an instance from it. The file name is passed in the constructor like this: □ FileWriter writer = new FileWriter(fileName);□ This code opens the file in overwrite mode. If you want to append to the file then you need to use an other constructor like this: □ FileWriter writer = new FileWriter(fileName,true);□ Besides this the constructor can throw an IOException so we put all of the code inside a try-catch block.
Write to a File□ At this point we have a writer object and we can send real content to the file.□ Do this using the write() method, which has more variant but the most commonly used requires a string as input parameter.□ Calling the write() method doesnt mean that it immediately writes the data into the file. □ The output is maybe cached so if you want to send your data immediately to the file you need to call the flush() method.□ As last step you should close the file with the close() method and you are done
Reading from a File□ reading from a file is very similar to writing.□ We only need to use *Reader objects instead of *Writer objects.□ It means that you can use FileReader or BufferedReader. □ A simple FileReader can handle only a single character or a character array it is more convenient to use the BufferedReader which can read a complete line from a file as a string. □ So using a BufferedReader we can read a text file line by line with the readln() method
Reading and Writing to Binary Files□ FileOutputStream and DataOutputStream□ FileInputStream and DataInputStream□ If will try to access something after end of file, it will throw and EOFException
Reading and writing to Binary files Writing to a File Reading from a FileFileOutputStream out = new FileOutputStream("c:test. txt",true);DataOutputStream dataOut = new DataOutputStream(out);String str ="Saira Anwar";dataOut.writeBytes(str);dataOut.close();