* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download View File - UET Taxila
Survey
Document related concepts
Go (programming language) wikipedia , lookup
Java syntax wikipedia , lookup
Class (computer programming) wikipedia , lookup
String literal wikipedia , lookup
Name mangling wikipedia , lookup
Abstraction (computer science) wikipedia , lookup
Dynamic-link library wikipedia , lookup
C Sharp syntax wikipedia , lookup
Library (computing) wikipedia , lookup
Scala (programming language) wikipedia , lookup
Object-oriented programming wikipedia , lookup
Java (programming language) wikipedia , lookup
Computer file wikipedia , lookup
Transcript
Object-Oriented Programming Session 09 Student Name: Roll/No: Group: I/O Streams Java does provide support for I/O as it relates to files and networks. I/O in java is performed through streams. A stream is nothing but an abstraction that either reads data from a device or displays it on some output device. I/O classes and methods are applied to any type of input, output device. We have to import java.io package while dealing with I/O in java. Two types of streams are defined in Java: byte and character streams. Let’s practice a few sample codes which can help us to improve our understanding of the topic under discussion. Experiment 1.0: Reading Console Input import java.io.*; class Read{ public static void main(String args[]) throws IOException { charc; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter characters and press q to quit”); //reading characters do { c=(char)br.read(); System.out.println(c); } while(c!=’q’); } } Object-Oriented Programming Experiment 1.1: Writing Console Output import java.io.*; class Write_Console { public static void main(String args[]) { int b; b=’A’; System.out.write(b); System.out.write(‘\n’); } } Experiment 1.2: Reading and Writing file import java.io.*; import java.io.*; class File_Display { public static void main(String args[]) throws IOException { int i; FileInputStream fin; try { fin=new FileInputStream(args[0]); } catch(“File Not Found”); return; } catch(ArrayIndexOutOfBoundsException e) { System.out.println(“Display a file”); return; } //read characters until End of File is found Object-Oriented Programming do { i=fin.read(); if(i!= -1) System.out.println((char)i); } while(i!=-1) Experiment 1.3: Task Q1) By using the above techniques, copy a text file or java file to another already created file. You have to mention the names of the source and destination files at runtime. Also count number of words in that file. Q2) While compiling the above code, do mention such a name of destination file that does not exist. And see what happens. If there is some error then correct it. Q3) Write a code which reads string from the keyboard, and displays that on standard output device. (This can set basis for a tiny editor design) Q4) Write a code, which reads input from the console and displays it in a file.