Comp102 lec 11


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Comp102 lec 11

  1. 1. File Handling
  2. 2. System class□ System class has three attributes □ in ( □ Object of class InputStream □ out (System.out) □ Object of OutputStream □ err (Error Stream) □ Object of ErrorStream
  3. 3. 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
  4. 4. 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
  5. 5. A file on a disk or tape can be a text file or a binary file.
  6. 6. The standard input and output behave like text files.
  7. 7. The standard error behaves like a text file.
  8. 8. StreamS
  9. 9. File streams are created, connected to files, and disconnected from files by the programmer.
  10. 10. 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
  11. 11. 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.
  12. 12. 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
  13. 13. 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();
  14. 14. 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";
  15. 15. 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.
  16. 16. 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
  17. 17. 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
  18. 18. return fileContent.toString();
  19. 19. 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
  20. 20. 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();