AN IMPROTANT COLLECTION OF JAVA IO RELATED PROGRAMS

Size: px
Start display at page:

Download "AN IMPROTANT COLLECTION OF JAVA IO RELATED PROGRAMS"

Transcription

1 JAVALEARNINGS.COM AN IMPROTANT COLLECTION OF JAVA IO RELATED PROGRAMS Visit for more pdf downloads and interview related questions JAVALEARNINGS.COM

2 /* Write a program to write n number of student records in a data file and search the data file on the basis of roll no.,name,marks */ import java.util.*; import java.io.*; class Student implements Serializable public String name; public int roll; public float marks; Student() //System.out.println("Student Record Created"); public void input() throws IOException BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print(" Name of the Student: "); name = br.readline(); System.out.print(" Roll No. of the Student: "); roll = Integer.parseInt(br.readLine()); System.out.print(" Marks of the Student: "); marks = Float.parseFloat(br.readLine()); public void display() System.out.println("\tName: "+name); System.out.println("\tRoll No.: "+roll); System.out.println("\tMarks: "+marks); class ObjectWrite static File path = new File("objwrite.dat"); public static void main(string args[]) throws IOException

3 int z; Student temp = new Student(); Scanner sc = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("\n\t Serialization/DeSerialization \n"); System.out.println("\n 1.Write \n 2.Read \n 3.Search \n 4.Exit"); System.out.print(" Your Choice: "); z = sc.nextint(); switch(z) case 1: ObjectWrite.write(); break; case 2: ObjectWrite.read(); break; case 3: System.out.println("\n\t\t Searching from file: "+path); int choice; System.out.println("\n\t 1.Using Name \n\t 2.Using Marks \n\t 3.Using RollNo."); System.out.print(" Your Choice: "); choice = sc.nextint(); switch(choice) case 1: System.out.println(" Enter Name to be Searched: "); String tname = br.readline(); searchname(tname); break;

4 case 2: System.out.println(" Enter Marks to be Searched: "); float marks = Float.parseFloat(br.readLine()); searchmarks(marks); break; case 3: System.out.println(" Enter RollNo. to be Searched: "); int roll = Integer.parseInt(br.readLine()); searchroll(roll); break; default: System.out.println("\n Poor Choice "); ObjectWrite.main(new String[] "Poor","Choice","Entered"); Encountered"); case 4: System.exit(0); default: System.out.println("\n Wrong Input ObjectWrite.main(new String[] "Wrong","Input","Encountered"); public static void write() throws IOException System.out.println(" Writing Objects to the File: "+path+"\n"); int i;

5 Scanner sc = new Scanner(System.in); System.out.println("\n Enter the Number of Students: "); int n = sc.nextint(); Student s[] = new Student[n+1]; //writing to the file FileOutputStream out = new FileOutputStream(path); ObjectOutputStream oos = new ObjectOutputStream(out); System.out.println("\n Enter the Details: "); for(i=0;i<n;i++) s[i] = new Student(); System.out.print("\n"); s[i].input(); oos.writeobject(s[i]); oos.flush(); s[i] = new Student(); s[i].name = "EOF"; s[i].roll = -1; s[i].marks = -1; oos.close(); out.close(); System.out.println("\n Writing Complete \n"); public static void read() throws IOException //reading from the file FileInputStream in = new FileInputStream(path); ObjectInputStream ois = new ObjectInputStream(in); System.out.println("\n Reading Objects from the File: "+path+"\n"); try Student temp=(student)ois.readobject(); while(true) if(temp.roll==-1) break; else

6 Complete"); temp.display(); System.out.print("\n"); temp=(student)ois.readobject(); ois.close(); catch(throwable e) System.out.println("End of File Reached\n Reading public static void searchname(string tname ) throws IOException //reading from the file FileInputStream in = new FileInputStream(path); ObjectInputStream ois = new ObjectInputStream(in); try Student temp=(student)ois.readobject(); while(true) if(temp.roll==-1) break; else if(temp.name.startswith(tname) temp.name.endswith(tname)) System.out.println("\t Record Found \t"); temp.display(); System.out.print("\n"); temp=(student)ois.readobject(); ois.close();

7 catch(throwable e) System.out.println("End of File Reached\n Searching Complete"); public static void searchmarks(float marks ) throws IOException //reading from the file FileInputStream in = new FileInputStream(path); ObjectInputStream ois = new ObjectInputStream(in); try Student temp=(student)ois.readobject(); while(true) if(temp.roll==-1) break; else if(temp.marks==marks) System.out.println("\t Record Found \t"); temp.display(); System.out.print("\n"); temp=(student)ois.readobject(); ois.close(); catch(throwable e) System.out.println("End of File Reached\n Searching Complete"); public static void searchroll(int roll ) throws IOException //reading from the file FileInputStream in = new FileInputStream(path);

8 ObjectInputStream ois = new ObjectInputStream(in); try Student temp=(student)ois.readobject(); while(true) if(temp.roll==-1) break; else if(temp.roll==roll) System.out.println("\t Record Found \t"); temp.display(); System.out.print("\n"); temp=(student)ois.readobject(); ois.close(); catch(throwable e) System.out.println("End of File Reached\n Searching Complete");

9 /* Write a program to copy the contents of one file to another */ import java.util.*; import java.io.*; class FileCopy public static void main(string args[]) throws IOException File f1 = new File(args[0]); File f2 = new File(args[1]); FileInputStream in = null; FileOutputStream out = null; System.out.println(" Writing the Contents to: "+f1+"\n"); BufferedInputStream br = new BufferedInputStream(System.in); BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); char c; int i; String temp; out = new FileOutputStream(f1); //writing to file f1 System.out.println("Enter Strings(@ to Exit Writing): \n"); i = br.read(); c = (char)i; while(c!='@') out.write(i); i = br.read(); c = (char)i; out.close(); //copying to file f2 System.out.println(" Copying the Contents of "+f1+" to "+f2+" \n"); in = new FileInputStream(f1); out = new FileOutputStream(f2); i = in.read(); while(i!=-1)

10 c = (char)i; out.write(i); i = in.read(); out.close(); "+f2); System.out.println("\n Write 'view' to view the contents of file: String v = b.readline(); if(v.equals("view") v.equals("view")) System.out.println("Done"); else System.exit(0); //displaying the contents of f2 in = new FileInputStream(f2); i = in.read(); while(i!=-1) c = (char)i; System.out.print(c); i = in.read();

11 /* Write a program to count the number of words in a text file */ import java.util.*; import java.io.*; class WordCount public static void main(string args[]) throws IOException String name; String text; int count=1; char c; int i; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(" Enter the Name for the File: "); name = br.readline(); File f = new File(name); System.out.println("\n We got a file: "+f); System.out.println("\n Does it extisted before: "+f.exists()); System.out.println("\n Is it a Directory: "+f.isdirectory()); FileWriter out = new FileWriter(f); System.out.println("\n Enter the text to the File(Write 'qq' to end writing): \n"); text = br.readline(); while(!(text.equals("qq"))) char temp[] = new char[text.length()]; text.getchars(0,text.length(),temp,0); int n = temp.length; for(i=0;i<n;i++) out.write(temp[i]); text = br.readline(); out.close(); System.out.println("\n Writng Done on File: "+f);

12 System.out.println("\n Counting the Number of Words in the File: "+f); FileReader in = new FileReader(f); i = in.read(); while(i!=-1) c = (char)i; if(c==32 c=='\n') count++; System.out.print(c); i = in.read(); System.out.println("\n Number of Words in the File "+f+" are: "+(count));

13 /* Write a program to create encrypt and decrypt the contents of a text file */ import java.util.*; import java.io.*; class EncryptData static int er=0,pass=0; public static void main(string args[]) throws IOException int er =0,pass =0; int c; char a; File path = new File(args[0]); //creating a FileObject FileReader in = null; //For Reading from File FileWriter out = null; //For Writing to File Scanner sc = new Scanner(System.in); String test; System.out.println("\n We got a File: "+path); //File Details System.out.println("\n Does it Exists: "+path.exists()); //File Details System.out.println("\n Is Directory: "+path.isdirectory()); //File Details encryption out = new FileWriter(path); //writng to File System.out.println("\n Encryption Process: \n\n"); EncryptData.encrypt(out); //calling function for out.close(); er = EncryptData.er; pass = EncryptData.pass; System.out.println("er = "+er+" pass"+pass);

14 "); decryption in = new FileReader(path); System.out.println("\n Displaying Encrypted Contents of the File: c = in.read(); while(c!=-1) a = (char)c; System.out.print(a); c = in.read(); in = new FileReader(path); System.out.println("\n\n Decryption Process: \n\n"); System.out.println("Write 'view' to view the Decrypted File Data"); test = sc.nextline(); if(test.equals("view") test.equals("view")) EncryptData.decrypt(in,pass); //calling function for public static void encrypt(filewriter out) throws IOException String l; int n,c,er=0,pass=0; char a; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("\n Enter the Data to be written on the File:(qq to stop wirting) \n\n"); l = br.readline(); while(!(l.equals("qq"))) char[] arr = new char[l.length()]; n = arr.length; //System.out.println("Char Arr length: l.getchars(0,l.length(),arr,0); for(int i=0;i<n;i++,er++) "+n);

15 c = arr[i]-er; a = (char)c; //System.out.print(" err : "+er); out.write(a); if(er==20) er=-1; pass++; l = br.readline(); out.close(); EncryptData.er = er-2; EncryptData.pass = pass; public static void decrypt(filereader in,int pass) throws IOException //function to decrypt data from file int c,n,er=0; char a; c = in.read(); do if(er>20) er=0; if(c==10) er = 0; System.out.println("\n"); n = c+er; a = (char)n; System.out.print(a); er++; c = in.read(); while(c!=-1);

16 /* Write a program to search the files starting with a perticular string in directory */ import java.util.*; import java.io.*; class FileSearch public static void main(string args[]) throws IOException int i; Scanner sc = new Scanner(System.in); System.out.println(" Enter the name of the Directory(e.g: C:\\Himanshu)"); String aa = sc.nextline(); File path = new File(aa); String[] s =path.list(); System.out.println("Enter part of Name of File To Be Searched:(e.g: First Name or Extension)"); String l = sc.nextline(); int loc=-1; int a; if(path.isdirectory()) System.out.println("Is directory: "+path.isdirectory()); for(i=0,a=0;i<s.length;i++,a++) //System.out.println("S.NO.: "+a+1+" : "+s[i]); if(s[i].startswith(l) s[i].endswith(l)) loc = i; System.out.println(s[i]+" --> Found at Loc:\t"+loc+"\n"); if(loc==-1) System.out.println("Not found");

17

18 /* Write a program to pass the contents of a student from a html file to a java program for finding the result */ import java.util.*; import java.applet.*; import java.awt.*; // <applet code="applettest.class" height=500 width=500 /> public class AppletTest extends Applet String s; int m1,m2,m3,m4; public void init() //setforeground(color.black); //setbackground(color.red); public void start() s = getparameter("name"); m1 = Integer.parseInt(getParameter("sub1")); m2 = Integer.parseInt(getParameter("sub2")); m3 = Integer.parseInt(getParameter("sub3")); m4 = Integer.parseInt(getParameter("sub4")); public void paint(graphics g) g.drawstring(" Applet Testing ",100,100); g.drawstring("name: "+s,100,140); g.drawstring("subject 1 Marks: "+m1,110,180); g.drawstring("subject 2 Marks: "+m2,110,220); g.drawstring("subject 3 Marks: "+m3,110,260); g.drawstring("subject 4 Marks: "+m4,110,300); g.drawstring("total Marks: "+(m1+m2+m3+m4),110,340); g.drawstring("total Marks: "+((m1+m2+m3+m4)/4),110,380);

Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*;

Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*; Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*; /* */ public

More information

CN208 Introduction to Computer Programming

CN208 Introduction to Computer Programming CN208 Introduction to Computer Programming Lecture #11 Streams (Continued) Pimarn Apipattanamontre Email: pimarn@pimarn.com 1 The Object Class The Object class is the direct or indirect superclass of every

More information

Darshan Institute of Engineering & Technology for Diploma Studies

Darshan Institute of Engineering & Technology for Diploma Studies Streams A stream is a sequence of data. In Java a stream is composed of bytes. In java, 3 streams are created for us automatically. 1. System.out : standard output stream 2. System.in : standard input

More information

Chapter 10. IO Streams

Chapter 10. IO Streams Chapter 10 IO Streams Java I/O The Basics Java I/O is based around the concept of a stream Ordered sequence of information (bytes) coming from a source, or going to a sink Simplest stream reads/writes

More information

AP COMPUTER SCIENCE A

AP COMPUTER SCIENCE A AP COMPUTER SCIENCE A CONTROL FLOW Aug 28 2017 Week 2 http://apcs.cold.rocks 1 More operators! not!= not equals to % remainder! Goes ahead of boolean!= is used just like == % is used just like / http://apcs.cold.rocks

More information

Object-Oriented Programming Design. Topic : Streams and Files

Object-Oriented Programming Design. Topic : Streams and Files Electrical and Computer Engineering Object-Oriented Topic : Streams and Files Maj Joel Young Joel Young@afit.edu. 18-Sep-03 Maj Joel Young Java Input/Output Java implements input/output in terms of streams

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: www: materials: e-learning environment: office: alt. office: jkizito@cis.mak.ac.ug http://serval.ug/~jona http://serval.ug/~jona/materials/csc1214

More information

Here is a hierarchy of classes to deal with Input and Output streams.

Here is a hierarchy of classes to deal with Input and Output streams. PART 25 25. Files and I/O 25.1 Reading and Writing Files A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data

More information

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת A Typical Program Most applications need to process some input and produce some output based on that input The Java IO package (java.io) is to make that possible

More information

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 2.A. Design a superclass called Staff with details as StaffId, Name, Phone, Salary. Extend this class by writing three subclasses namely Teaching (domain, publications), Technical (skills), and Contract

More information

1.00 Lecture 30. Sending information to a Java program

1.00 Lecture 30. Sending information to a Java program 1.00 Lecture 30 Input/Output Introduction to Streams Reading for next time: Big Java 15.5-15.7 Sending information to a Java program So far: use a GUI limited to specific interaction with user sometimes

More information

CS Week 11. Jim Williams, PhD

CS Week 11. Jim Williams, PhD CS 200 - Week 11 Jim Williams, PhD This Week 1. Exam 2 - Thursday 2. Team Lab: Exceptions, Paths, Command Line 3. Review: Muddiest Point 4. Lecture: File Input and Output Objectives 1. Describe a text

More information

STRUKTUR PROGRAM JAVA: //Daftar paket yang digunakan dalam program import namapaket;

STRUKTUR PROGRAM JAVA: //Daftar paket yang digunakan dalam program import namapaket; STRUKTUR PROGRAM JAVA: //Daftar paket yang digunakan dalam program import namapaket; //Membuat Kelas public class namakelas //Metode Utama public static void main(string[] args) perintah-perintah;... LATIHAN

More information

Byte and Character Streams. Reading and Writing Console input and output

Byte and Character Streams. Reading and Writing Console input and output Byte and Character Streams Reading and Writing Console input and output 1 I/O basics The io package supports Java s basic I/O (input/output) Java does provide strong, flexible support for I/O as it relates

More information

Assignment2013 Please use this document only for verifying if your programs are right. Do not blindly copy paste and waste your time.

Assignment2013 Please use this document only for verifying if your programs are right. Do not blindly copy paste and waste your time. Please use this document only for verifying if your programs are right. Do not blindly copy paste and waste your time. 11/3/2013 TechSparx Computer Training Center Saravanan.G Please use this document

More information

MYcsvtu Notes. Experiment-1. AIM: Write a program to implement fiestal cipher structure PROGRAM: import java.io.*; class functions

MYcsvtu Notes. Experiment-1. AIM: Write a program to implement fiestal cipher structure PROGRAM: import java.io.*; class functions Experiment-1 AIM: Write a program to implement fiestal cipher structure PROGRAM: import java.io.*; class functions String Xor(String s1,string s2) char s1array[]=s1.tochararray(); char s2array[]=s2.tochararray();

More information

Java Programs. System.out.println("Dollar to rupee converion,enter dollar:");

Java Programs. System.out.println(Dollar to rupee converion,enter dollar:); Java Programs 1.)Write a program to convert rupees to dollar. 60 rupees=1 dollar. class Demo public static void main(string[] args) System.out.println("Dollar to rupee converion,enter dollar:"); int rs

More information

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371 Class IX HY 2013 Revision Guidelines Page 1 Section A (Power Point) Q1.What is PowerPoint? How are PowerPoint files named? Q2. Describe the 4 different ways of creating a presentation? (2 lines each) Q3.

More information

Recitation: Loop Jul 7, 2008

Recitation: Loop Jul 7, 2008 Nested Loop Recitation: Loop Jul 7, 2008 1. What is the output of the following program? Use pen and paper only. The output is: ****** ***** **** *** ** * 2. Test this program in your computer 3. Use "for

More information

6 COMPUTER PROGRAMMING

6 COMPUTER PROGRAMMING 6 COMPUTER PROGRAMMING ITERATION STATEMENT CONTENTS WHILE DO~WHILE FOR statement 2 Iteration Statement provides While / do-while / For statements for supporting an iteration logic function that the logic

More information

FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO {

FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO { FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO public static void main(string[] args) throws IOException BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int frames,

More information

The Java I/O System. Binary I/O streams (ASCII, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

The Java I/O System. Binary I/O streams (ASCII, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits) The Java I/O System Binary I/O streams (ASCII, 8 bits) InputStream OutputStream The decorator design pattern Character I/O streams (Unicode, 16 bits) Reader Writer Comparing binary I/O to character I/O

More information

copy.dept_change( CSE ); // Original Objects also changed

copy.dept_change( CSE ); // Original Objects also changed UNIT - III Topics Covered The Object class Reflection Interfaces Object cloning Inner classes Proxies I/O Streams Graphics programming Frame Components Working with 2D shapes. Object Clone Object Cloning

More information

CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012

CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012 CMPSCI 187: Programming With Data Structures Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012 Files and a Case Study Volatile and Non-Volatile Storage Storing and Retrieving Objects

More information

Streams and File I/O

Streams and File I/O Chapter 9 Streams and File I/O Overview of Streams and File I/O Text File I/O Binary File I/O File Objects and File Names Chapter 9 Java: an Introduction to Computer Science & Programming - Walter Savitch

More information

Programmierpraktikum

Programmierpraktikum Programmierpraktikum Claudius Gros, SS2012 Institut für theoretische Physik Goethe-University Frankfurt a.m. 1 of 21 05/07/2012 10:31 AM Input / Output Streams 2 of 21 05/07/2012 10:31 AM Java I/O streams

More information

CS 200 File Input and Output Jim Williams, PhD

CS 200 File Input and Output Jim Williams, PhD CS 200 File Input and Output Jim Williams, PhD This Week 1. WaTor Change Log 2. Monday Appts - may be interrupted. 3. Optional Lab: Create a Personal Webpage a. demonstrate to TA for same credit as other

More information

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

Software 1 with Java. Recitation No. 7 (Java IO) May 29, Software 1 with Java Recitation No. 7 (Java IO) May 29, 2007 1 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating files Classes

More information

12% of course grade. CSCI 201L Final - Written Fall /7

12% of course grade. CSCI 201L Final - Written Fall /7 12% of course grade 1. Interfaces and Inheritance Does the following code compile? If so, what is the output? If not, why not? Explain your answer. (1.5%) interface I2 { public void meth1(); interface

More information

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes.  Debugging tools Today Book-keeping File I/O Subscribe to sipb-iap-java-students Inner classes http://sipb.mit.edu/iap/java/ Debugging tools Problem set 1 questions? Problem set 2 released tomorrow 1 2 So far... Reading

More information

Good Earth School Naduveerapattu Date: Marks: 70

Good Earth School Naduveerapattu Date: Marks: 70 Good Earth School Naduveerapattu Date:.2.207 Marks: 70 Class: XI Second Term Examination Computer Science Answer Key Time: 3 hrs. Candidates are allowed additional 5 minutes for only reading the paper.

More information

Special error return Constructors do not have a return value What if method uses the full range of the return type?

Special error return Constructors do not have a return value What if method uses the full range of the return type? 23 Error Handling Exit program (System.exit()) usually a bad idea Output an error message does not help to recover from the error Special error return Constructors do not have a return value What if method

More information

Java Input/Output Streams

Java Input/Output Streams Java Input/Output Streams Rui Moreira Some useful links: http://java.sun.com/docs/books/tutorial/essential/toc.html#io Input Stream Output Stream Rui Moreira 2 1 JVM creates the streams n System.in (type

More information

Example: Copying the contents of a file

Example: Copying the contents of a file Administrivia Assignment #4 is due imminently Due Thursday April 8, 10:00pm no late assignments will be accepted Sign up in the front office for a demo time Dining Philosophers code is online www.cs.ubc.ca/~norm/211/2009w2/index.html

More information

Files and IO, Streams. JAVA Standard Edition

Files and IO, Streams. JAVA Standard Edition Files and IO, Streams JAVA Standard Edition Java - Files and I/O The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent

More information

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20 File IO Computer Science and Engineering College of Engineering The Ohio State University Lecture 20 I/O Package Overview Package java.io Core concept: streams Ordered sequences of data that have a source

More information

Input from Files. Buffered Reader

Input from Files. Buffered Reader Input from Files Buffered Reader Input from files is always text. You can convert it to ints using Integer.parseInt() We use BufferedReaders to minimize the number of reads to the file. The Buffer reads

More information

CPSC 319. Week 2 Java Basics. Xiaoyang Liu & Sorting Algorithms

CPSC 319. Week 2 Java Basics. Xiaoyang Liu & Sorting Algorithms CPSC 319 Week 2 Java Basics Xiaoyang Liu xiaoyali@ucalgary.ca & Sorting Algorithms Java Basics Variable Declarations Type Size Range boolean 1 bit true, false char 16 bits Unicode characters byte 8 bits

More information

Exceptions and Working with Files

Exceptions and Working with Files Exceptions and Working with Files Creating your own Exceptions. You have a Party class that creates parties. It contains two fields, the name of the host and the number of guests. But you don t want to

More information

FILE I/O IN JAVA. Prof. Chris Jermaine

FILE I/O IN JAVA. Prof. Chris Jermaine FILE I/O IN JAVA Prof. Chris Jermaine cmj4@cs.rice.edu 1 Our Simple Java Programs So Far Aside from screen I/O......when they are done, they are gone They have no lasting effect on the world When the program

More information

Shardayatan (EM) Subject: Computer Studies STD: 12

Shardayatan (EM) Subject: Computer Studies STD: 12 Shardayatan (EM) Subject: Computer Studies STD: 12 Chapter 7 (Java Basics) (1) Java Program to compute Call Cost and update prepaid balance amount. public class CallCost double b; double r; double d; double

More information

Objec&ves. Review. Standard Error Streams

Objec&ves. Review. Standard Error Streams Objec&ves Standard Error Streams Ø Byte Streams Ø Text Streams Oct 5, 2016 Sprenkle - CSCI209 1 Review What are benefits of excep&ons What principle of Java do files break if we re not careful? What class

More information

Software 1. Java I/O

Software 1. Java I/O Software 1 Java I/O 1 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating files Classes for serializing objects 2 Streams A stream

More information

CS 251 Intermediate Programming Java I/O Streams

CS 251 Intermediate Programming Java I/O Streams CS 251 Intermediate Programming Java I/O Streams Brooke Chenoweth University of New Mexico Spring 2018 Basic Input/Output I/O Streams mostly in java.io package File I/O mostly in java.nio.file package

More information

1.00 Lecture 31. Streams 2. Reading for next time: Big Java , The 3 Flavors of Streams

1.00 Lecture 31. Streams 2. Reading for next time: Big Java , The 3 Flavors of Streams 1.00 Lecture 31 Streams 2 Reading for next time: Big Java 18.6-18.8, 20.1-20.4 The 3 Flavors of Streams In Java, you can read and write data to a file: as text using FileReader and FileWriter as binary

More information

pre-emptive non pre-emptive

pre-emptive non pre-emptive start() run() class SumThread extends Thread { int end; int sum; SumThread( int end ) { this.end = end; } public void run() { // sum integers 1, 2,..., end // and set the sum } } SumThread t = new SumThread(

More information

IT101. File Input and Output

IT101. File Input and Output IT101 File Input and Output IO Streams A stream is a communication channel that a program has with the outside world. It is used to transfer data items in succession. An Input/Output (I/O) Stream represents

More information

DataInputStream dis=new DataInputStream(System.in);

DataInputStream dis=new DataInputStream(System.in); SET 2 Answer Key 1.a. import java.util.*; import java.io.*; class sorting public static void main(string as[])throws Exception DataInputStream dis=new DataInputStream(System.in); System.out.println("Enter

More information

STREAMS. (fluxos) Objetivos

STREAMS. (fluxos) Objetivos STREAMS (fluxos) Objetivos To be able to read and write files To become familiar with the concepts of text and binary files To be able to read and write objects using serialization To be able to process

More information

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O I/O 10-7-2013 I/O in Java I/O streams vs. Reader/Writer HW#3 due today Reading Assignment: Java tutorial on Basic I/O public class Swimmer implements Cloneable { public Date geteventdate() { return (Date)

More information

Chapter 12. File Input and Output. CS180-Recitation

Chapter 12. File Input and Output. CS180-Recitation Chapter 12 File Input and Output CS180-Recitation Reminders Exam2 Wed Nov 5th. 6:30 pm. Project6 Wed Nov 5th. 10:00 pm. Multitasking: The concurrent operation by one central processing unit of two or more

More information

INDEX SL.NO NAME OF PROGRAMS PAGE NO REMARKS PROGRAM TO FIND FACTORIAL OF THREE

INDEX SL.NO NAME OF PROGRAMS PAGE NO REMARKS PROGRAM TO FIND FACTORIAL OF THREE INDEX SL.NO NAME OF PROGRAMS PAGE NO REMARKS PROGRAM TO FIND FACTORIAL OF THREE 1 NUMBERS PROGRAM FOR SUM OF SERIES USING 2 MATHPOWER METHOD 3 PROGRAM ON COMMAND LINE ARGUMENT 4 PROGRAM TO PRINT FIBONACI

More information

Java file manipulations

Java file manipulations Java file manipulations 1 Categories of Java errors We learn that there are three categories of Java errors : Syntax error Runtime error Logic error. A Syntax error (compiler error) arises because a rule

More information

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction COMPSCI 230 S2 2016 Software Construction File Input/Output 2 Agenda & Reading Agenda: Introduction Byte Streams FileInputStream & FileOutputStream BufferedInputStream & BufferedOutputStream Character

More information

输 入输出相关类图. DataInput. DataOutput. java.lang.object. FileInputStream. FilterInputStream. FilterInputStream. FileOutputStream

输 入输出相关类图. DataInput. DataOutput. java.lang.object. FileInputStream. FilterInputStream. FilterInputStream. FileOutputStream 输 入 / 输出 杨亮 流的分类 输 入输出相关类图 OutputStream FileOutputStream DataInputStream ObjectOutputStream FilterInputStream PipedOutputStream DataOutput InputStream DataInputStream PrintStream ObjectInputStream PipedInputStream

More information

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output Overview 7 Streams and files import java.io.*; Binary data vs textual data Simple file processing - examples The stream model Bytes and characters Buffering Byte streams Character streams Binary streams

More information

F I N A L E X A M I N A T I O N

F I N A L E X A M I N A T I O N Faculty Of Computer Studies M257 Putting Java to Work F I N A L E X A M I N A T I O N Number of Exam Pages: (including this cover sheet( Spring 2011 April 4, 2011 ( 5 ) Time Allowed: ( 1.5 ) Hours Student

More information

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015 COMP-202: Foundations of Programming Lecture 22: File I/O Jackie Cheung, Winter 2015 Announcements Assignment 5 due Tue Mar 31 at 11:59pm Quiz 6 due Tue Apr 7 at 11:59pm 2 Review 1. What is a graph? How

More information

Assignment-1 Final Code. Student.java

Assignment-1 Final Code. Student.java package com.ds.assgn1a; public class Student { public int rollno; public String name; public double marks; Assignment-1 Final Code Student.java public Student(int rollno, String name, double marks){ this.rollno

More information

class Ideone { public static void main (String[] args) throws java.lang.exception {

class Ideone { public static void main (String[] args) throws java.lang.exception { 1. wap to enter a string then convert it into upper case Input:- san deep output : SaN DEEp import java.util.*; import java.lang.*; class Ideone public static void main (String[] args) throws java.lang.exception

More information

Project #1 Computer Science 2334 Fall 2008

Project #1 Computer Science 2334 Fall 2008 Project #1 Computer Science 2334 Fall 2008 User Request: Create a Word Verification System. Milestones: 1. Use program arguments to specify a file name. 10 points 2. Use simple File I/O to read a file.

More information

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use: CSE 20 SAMPLE FINAL Version A Time: 180 minutes Name The following precedence table is provided for your use: Precedence of Operators ( ) - (unary),!, ++, -- *, /, % +, - (binary) = = =,!= &&

More information

What is Serialization?

What is Serialization? Serialization 1 Topics What is Serialization? What is preserved when an object is serialized? Transient keyword Process of serialization Process of deserialization Version control Changing the default

More information

PIC 20A Streams and I/O

PIC 20A Streams and I/O PIC 20A Streams and I/O Ernest Ryu UCLA Mathematics Last edited: December 7, 2017 Why streams? Often, you want to do I/O without paying attention to where you are reading from or writing to. You can read

More information

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive Solutions Exercises Unit 19 1 General Algorithm for Recursion

More information

Here is a hierarchy of classes to deal with Input and Output streams.

Here is a hierarchy of classes to deal with Input and Output streams. PART 15 15. Files and I/O 15.1 Reading and Writing Files A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data

More information

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. Name: Write all of your responses on these exam pages. 1 Short Answer (5 Points Each) 1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. 2. Java is a platform-independent

More information

Job Migration. Job Migration

Job Migration. Job Migration Job Migration The Job Migration subsystem must provide a mechanism for executable programs and data to be serialized and sent through the network to a remote node. At the remote node, the executable programs

More information

Chapter 7: Iterations

Chapter 7: Iterations Chapter 7: Iterations Objectives Students should Be able to use Java iterative constructs, including do-while, while, and for, as well as the nested version of those constructs correctly. Be able to design

More information

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

Software 1 with Java. Recitation No. 9 (Java IO) December 10, Software 1 with Java Recitation No. 9 (Java IO) December 10, 2006 1 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating files

More information

Full file at

Full file at Chapter 1 Primitive Java Weiss 4 th Edition Solutions to Exercises (US Version) 1.1 Key Concepts and How To Teach Them This chapter introduces primitive features of Java found in all languages such as

More information

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO) I/O and Scannar Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad Website: www.sisoft.in Email:info@sisoft.in Phone: +91-9999-283-283 I/O operations Three steps:

More information

The FacebookPerson Example. Dr. Xiaolin Hu CSC 2310, Spring 2015

The FacebookPerson Example. Dr. Xiaolin Hu CSC 2310, Spring 2015 The FacebookPerson Example Dr. Xiaolin Hu CSC 2310, Spring 2015 Motivation Whenever you are happy, update your facebook! Start from Simple A Facebook Person Class A test class public class FacebookPerson{

More information

ICSE Class 10 Computer Applications ( Java ) 2012 Solved Question...

ICSE Class 10 Computer Applications ( Java ) 2012 Solved Question... 1 of 1 05-11-015 16: ICSE J Java for Class X Computer Applications ICSE Class 10 Computer Applications ( Java ) 01 Solved Question Paper COMPUTER APPLICATIONS (Theory) (Two Hours) Answers to this Paper

More information

Internet and Intranet Applications and Protocols Examples of Bad SMTP Code Prof. Arthur P. Goldberg Spring, 2004

Internet and Intranet Applications and Protocols Examples of Bad SMTP Code Prof. Arthur P. Goldberg Spring, 2004 Internet and Intranet Applications and Protocols Examples of Bad SMTP Code Prof. Arthur P. Goldberg Spring, 00 Summary I show some examples of bad code and discuss how they fail to meet the Software Quality

More information

CCHS Math Recursion Worksheets M Heinen CS-A 12/5/2013. Recursion Worksheets Plus Page 1 of 6

CCHS Math Recursion Worksheets M Heinen CS-A 12/5/2013. Recursion Worksheets Plus Page 1 of 6 CS-A // arraysol[][] = r; import java.util.scanner; public class RecursionApp { static int r; // return value static int[][] arraysol = new int[][7]; // create a solution array public static void main(string[]

More information

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O Week 12 Streams and File I/O Overview of Streams and File I/O Text File I/O 1 I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or a file

More information

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output. CSE 143 Java Streams Reading: 19.1, Appendix A.2 GREAT IDEAS IN COMPUTER SCIENCE REPRESENTATION VS. RENDERING 4/28/2002 (c) University of Washington 09-1 4/28/2002 (c) University of Washington 09-2 Topics

More information

Chapter 10 Input Output Streams

Chapter 10 Input Output Streams Chapter 10 Input Output Streams ICT Academy of Tamil Nadu ELCOT Complex, 2-7 Developed Plots, Industrial Estate, Perungudi, Chennai 600 096. Website : www.ictact.in, Email : contact@ictact.in, Phone :

More information

LARA TECHNOLOGIES EXAM_SPECIALSIX SECTION-1

LARA TECHNOLOGIES EXAM_SPECIALSIX SECTION-1 Q: 01. SECTION-1 Q: 02 Given: 12. import java.io.*; 13. public class Forest implements Serializable { 14. private Tree tree = new Tree(); 15. public static void main(string [] args) { 16. Forest f = new

More information

Compile error. 2. A run time error occurs when the program runs and is caused by a number of reasons, such as dividing by zero.

Compile error. 2. A run time error occurs when the program runs and is caused by a number of reasons, such as dividing by zero. (טיפול בשגיאות) Exception handling We learn that there are three categories of errors : Syntax error, Runtime error and Logic error. A Syntax error (compiler error) arises because a rule of the language

More information

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course JAC444 - Lecture 4 Segment 1 - Exception 1 Objectives Upon completion of this lecture, you should be able to: Separate Error-Handling Code from Regular Code Use Exceptions to Handle Exceptional Events

More information

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks: كلية الحاسبات وتقنية المعلوما Exam 2 Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: November 22, 2015 Student Name: Student ID: Total Marks: 40 Obtained Marks: Instructions: Do not open this

More information

Java Input/Output. 11 April 2013 OSU CSE 1

Java Input/Output. 11 April 2013 OSU CSE 1 Java Input/Output 11 April 2013 OSU CSE 1 Overview The Java I/O (Input/Output) package java.io contains a group of interfaces and classes similar to the OSU CSE components SimpleReader and SimpleWriter

More information

Computer Applications Answer Key Class IX December 2017

Computer Applications Answer Key Class IX December 2017 Computer Applications Answer Key Class IX December 2017 Question 1. a) What are the default values of the primitive data type int and float? Ans : int = 0 and float 0.0f; b) Name any two OOP s principle.

More information

Input-Output and Exception Handling

Input-Output and Exception Handling Software and Programming I Input-Output and Exception Handling Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline Reading and writing text files Exceptions The try block catch and finally

More information

I/O Streams. Object-oriented programming

I/O Streams. Object-oriented programming I/O Streams Object-oriented programming Outline Concepts of Data Streams Streams and Files File class Text file Binary file (primitive data, object) Readings: GT, Ch. 12 I/O Streams 2 Data streams Ultimately,

More information

The Java Series IO, Serialization and Persistence. The Java Series. IO, Serialization and Persistence Raul RAMOS / CERN-IT User Support Slide 1

The Java Series IO, Serialization and Persistence. The Java Series. IO, Serialization and Persistence Raul RAMOS / CERN-IT User Support Slide 1 The Java Series IO, Serialization and Persistence Raul RAMOS / CERN-IT User Support Slide 1 Input/Output Often programs need to retrieve information from an external source. send information to an external

More information

Events and Exceptions

Events and Exceptions Events and Exceptions Analysis and Design of Embedded Systems and OO* Object-oriented programming Jan Bendtsen Automation and Control Lecture Outline Exceptions Throwing and catching Exceptions creating

More information

//Initializes the variables that will hold the eventual final new versions of the string String pluralextension; String secondwordextension;

//Initializes the variables that will hold the eventual final new versions of the string String pluralextension; String secondwordextension; /* Jackson Baker Rogers High School Vowels-R-Us Final Project 12/15/14 */ import java.io.*; import java.util.stringtokenizer; public class Vowels //Variables needed for inputting outside file private static

More information

File Operations in Java. File handling in java enables to read data from and write data to files

File Operations in Java. File handling in java enables to read data from and write data to files Description Java Basics File Operations in Java File handling in java enables to read data from and write data to files along with other file manipulation tasks. File operations are present in java.io

More information

Object Oriented Design with UML and Java. PART VIII: Java IO

Object Oriented Design with UML and Java. PART VIII: Java IO Object Oriented Design with UML and Java PART VIII: Java IO Copyright David Leberknight and Ron LeMaster. Version 2011 java.io.* & java.net.* Java provides numerous classes for input/output: java.io.inputstream

More information

MYcsvtu Notes. Java Programming Lab Manual

MYcsvtu Notes. Java Programming Lab Manual Java Programming Lab Manual LIST OF EXPERIMENTS 1. Write a Program to add two numbers using Command Line Arguments. 2. Write a Program to find the factorial of a given number using while statement. 3.

More information

I gave this assignment in my Internet and Intranet Protocols and Applications course:

I gave this assignment in my Internet and Intranet Protocols and Applications course: Producing Production Quality Software Lecture 1b: Examples of Bad Code Prof. Arthur P. Goldberg Fall, 2004 Summary I show some examples of bad code and discuss how they fail to meet the Software Quality

More information

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Tools : The Java Compiler. The Java Interpreter. The Java Debugger Tools : The Java Compiler javac [ options ] filename.java... -depend: Causes recompilation of class files on which the source files given as command line arguments recursively depend. -O: Optimizes code,

More information

Caesar Cipher program in java (SS)

Caesar Cipher program in java (SS) Caesar Cipher program in java (SS) In cryptography Caesar cipher is one of the simple and most widely used Encryption algorithm.caesar cipher is special case of shift cipher.caesar cipher is substitution

More information

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation. Chapter 4 Lab Loops and Files Lab Objectives Be able to convert an algorithm using control structures into Java Be able to write a while loop Be able to write an do-while loop Be able to write a for loop

More information

Lecture 11 (review session for intro to computing) ============================================================== (Big-Oh notation continued)

Lecture 11 (review session for intro to computing) ============================================================== (Big-Oh notation continued) Lecture 11 (review session for intro to computing) 1) (Big-Oh notation continued) Assume that each of the following operations takes 1 time unit: an arithmetic op, a comparison, a logical op, an assignment,

More information

Algorithms. Produced by. Eamonn de Leastar

Algorithms. Produced by. Eamonn de Leastar Algorithms Produced by Eamonn de Leastar (edeleastar@wit.ie) Streams http://www.oracle.com/technetwork/java/javase/tech/index.html Introduction ± An I/O Stream represents an input source or an output destination.

More information

Give one example where you might wish to use a three dimensional array

Give one example where you might wish to use a three dimensional array CS 110: INTRODUCTION TO COMPUTER SCIENCE SAMPLE TEST 3 TIME ALLOWED: 60 MINUTES Student s Name: MAXIMUM MARK 100 NOTE: Unless otherwise stated, the questions are with reference to the Java Programming

More information