Streams File InputOutput IO IO Streams n A
- Slides: 19
Streams & File Input/Output (I/O)
I/O Streams n A java stream is an object consisting of a sequence of bytes that flow from a source to a destination Stream Source Destination n n Source: A program, a file Destination: Can be a file, the console (i. e. output) window…. . We read information from an input stream and write information to an output stream
I/O Streams n Streams are a bit of an elusive concept in Java -- it can take a while to get used to them ¨ You sort of need to mentally try to picture them over and over again, and the idea of them begins to become clear n The java. io. * (input output or I/O for short) package contains many classes that allow us to define various kinds of streams, each with specific characteristics n A program can manage multiple streams at a time n The I/O package is quite complex and a detailed analysis of all the classes in this package requires practice and experience
I/O Streams categories n n n There are several categories of I/O streams Today we will limit ourselves to two I/O streams. The first kind of stream deals with reading information from text files. The second deals with writing information to text files. Here are the classes that we will use for each: ¨ File. Reader: To read lines of text from files ¨ Print. Writer: To write lines of text to files
Standard I/O n We have already dealt with 3 standard I/O streams ¨A standard input stream– defined by System. in ¨ A standard output stream – defined by System. out ¨ A standard error stream– defined by System. err n Yet we never had to create these streams. This is because these streams are very common and are therefore automatically created for us as soon as we start executing a program
Streams are objects: n System. out ¨ ¨ out is a field of the System class. The data type of ‘out’ is a class called Print. Stream n ¨ n You could also say that ‘out’ is an object of type Print. Stream println is a method of the Print. Stream class. System. in in is an object of type Input. Stream ¨ read is a method that read only raw byte data ¨
Scanner to the rescue n Instead of having to deal with the System. in “rawness” we were able to use a Scanner object Scanner console = new Scanner (System. in); n n And the handy methods like next(), next. Int(), next. Double() etc. to read from the console Today we will see how to use Scanner to read from a text file
Reading from files Instead of using System. in (i. e. the terminal keyboard) as our input source, we need to create an input source linked to a text file n To do this, we need a special stream class, called File. Reader n n The File. Reader class has methods that will connect the Scanner class to a specific text file which can then be read
Reading from files n n The Scanner class has a very convenient method called next. Line() that reads in a line from a text file. So, once we have linked the Scanner class to an input file, we can use the next. Line() method to read in each line of the file The next. Line() method returns a String containing the first line of the file. The method also positions the cursor at the beginning of the next line
import java. util. *; import java. io. *; public static void main(String[] args) throws File. Not. Found. Exception { File. Reader data. File = new File. Reader("data. txt"); ); Scanner file. In = new Scanner (data. File); while (file. In. has. Next. Line()) { String a. Line = file. In. next. Line(); System. out. println(a. Line); } file. In. close(); }
The four steps of file reading 1. 2. Import the necessary packages (java. io. *) Create an input stream to the input source: File. Reader object to the actual file ¨ Scanner associated to the File. Reader ¨ 3. 4. Use the appropriate methods to read the data: next. Line() , next. Int(), etc. Close the stream: file. In. close()
File I/O Can Generate Exceptions n n If the program doesn’t find the file or if the file is somehow protected and denies access, the program will throw an exception and abort (crash) Every time you use File. Reader you need to either handle or throw the exception or it will not compile We can handle by enclosing the I/O code inside a try/catch sequence. Or, we can simply throw (delegate) with: throws File. Not. Found. Exception
import java. util. *; import java. io. *; public static void main(String[] args) throws File. Not. Found. Exception { File. Reader data. File = new File. Reader(“data. txt”); Scanner file. In = new Scanner ( data. File); while (file. In. has. Next. Line()) { String a. Line = file. In. next. Line(); System. out. println(a. Line); } file. In. close(); }
The four+ steps of file reading Import the necessary packages (java. io. *) n Create an input stream to the input source: n ¨ File. Reader object to the actual file ¨ Scanner associated to the File. Reader Use the appropriate methods to read the data: next. Line() , next. Int(), etc. n Close the stream: file. In. close() n Handle or throw the File. Not. Found. Exception n
Writing to a file We need to create an output stream that is connected to a file n For this, we do not use File. Reader and Scanner n We make use of the Print. Writer stream class and its methods n
import java. util. *; import java. io. *; public static void main(String[] args) throws File. Not. Found. Exception { Print. Writer file. Out = new Print. Writer("data. txt"); String a. Line = "A line of text"; file. Out. println(a. Line); file. Out. close(); }
The four+ steps of file writing Import the necessary packages (java. io. *) n Create an output stream to the output source: n ¨ Print. Writer object to the actual file Use the appropriate methods to write the data: println() , print(), etc. n Close the stream: file. Out. close() n
Important: n What happens when a program attempts to open a file that does not yet exist depends on whether it is for input or output For input: If the file does not exist, an exception will be thrown and the program will stop ¨ For output: If the file does not exist Print. Writer will create the file and not throw an exception ¨ n If the output file already exists, then the Print. Writer object will overwrite the file that was previously there!
Printing to file When Print. Writer writes to an existing file, it overrides whatever is already there, e. g. it does not append n So if you do not want to lose what you already have you should: n 1. 2. store the existing data in memory, then write the old and the new using Print. Writer
- Inputoutput devices
- File-file yang dibuat oleh user pada jenis file di linux
- Remote file access in distributed file system
- An html file is a text file containing small markup tags.
- Difference between logical file and physical file
- In a file-oriented information system, a transaction file
- Fungsi dari create file pada operasi-operasi file (cont.)
- Cba streams
- Disappearing streams karst topography
- Finding frequent items in data streams
- Types of bodys of water
- Broken stream fire nozzle
- Perforce virtual streams
- Bill nye rivers and streams
- Poem on growing older
- High gradient stream
- A framework for clustering evolving data streams
- Here i bow down here i bow down
- Female figure holding a fly-whisk
- Ibm business model canvas