comp102 lec 11

24
File Handling

Upload: fraz-bakhsh

Post on 25-May-2015

185 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Comp102   lec 11

File Handling

Page 2: Comp102   lec 11

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

Page 3: Comp102   lec 11

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

Page 4: Comp102   lec 11

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

Page 5: Comp102   lec 11

A file on a disk or tape can be a text file or a binary file.

Page 6: Comp102   lec 11

The standard input and output behave like text files.

Page 7: Comp102   lec 11

The standard error behaves like a text file.

Page 8: Comp102   lec 11

Streams

Page 9: Comp102   lec 11
Page 10: Comp102   lec 11
Page 11: Comp102   lec 11

File streams are created, connected to files, and disconnected from files by

the programmer.

Page 12: Comp102   lec 11

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

Page 13: Comp102   lec 11

One of the most frequently used task in programming is writing to and reading from a

file. To do this in Java there are more possibilities.

Page 14: Comp102   lec 11

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

Page 15: Comp102   lec 11

Reading and writing to text files

FileWriter name = new FileWriter(“Filename”);

PrintWriter printname = new PrintWriter(name);

printname.println(data );

Printname.close();

Filereader name = new FileReader(“Filename”);

BufferedReader buffername = new BufferedReader(name);

String str = buffername.readLine();

If(str == null) => End of File

Buffername.close();

Writing to a File Reading from a File

Page 16: Comp102   lec 11

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 thecomplete file name and path in your fileName variable:□String fileName = "c:\\filedemo\\test.txt";

Page 17: Comp102   lec 11

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 thenyou 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 insidea try-catch block.

Page 18: Comp102   lec 11

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 doesn't 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

Page 19: Comp102   lec 11
Page 20: Comp102   lec 11
Page 21: Comp102   lec 11

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

Page 22: Comp102   lec 11

return fileContent.toString();

Page 23: Comp102   lec 11

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

Page 24: Comp102   lec 11

Reading and writing to Binary files

FileOutputStream out = new FileOutputStream("c:\\test.txt",true);

DataOutputStream dataOut = new DataOutputStream(out);

String str ="Saira Anwar";

dataOut.writeBytes(str);dataOut.close();

Writing to a File Reading from a File