Selection sort and Merge sort File input/output. HW 2 – Section 02 Avg = 96/100.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 12 File Input and Output Animated Version.
Lecture 15: I/O and Parsing
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
1 Various Methods of Populating Arrays Randomly generated integers.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
File input and output. Interactive vs. file I/O All of the programs we have seen or written thus far have assumed interaction with a user, who types in.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 11 File Input and Output.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
File Input and Output Recitation 04/03/2009 CS 180 Department of Computer Science, Purdue University.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Chapter 91 Streams and File I/O Chapter 9. 2 Announcements Project 5 due last night Project 6 assigned Exam 2 –Wed., March 21, 7:00 – 8:00 pm, LILY 1105.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
J.43 ARRAYS  A Java array is an Object that holds an ordered collection of elements.  Components of an array can be primitive types or may reference.
Files and Streams CS 21a Chapter 11 of Horstmann.
Files and Streams CS 21a. 10/02/05 L18: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
CSC – Java Programming II Lecture 9 January 30, 2002.
Question Hot Seat public MyConstructor (int a, int b) { index = a+b; } A.Parameters B.Class Name C.Body D.Modifier Please put match these choices to these.
Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.
1 Chapter 11 – Files and Streams 1 Introduction What are files? –Long-term storage of large amounts of data –Persistent data exists after termination of.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different input.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Files and Streams. Midterm exam Time: Wednesday, October 31, 2007 Format: Multiple choices (about 15 to 20 questions) Determine the results of the code.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
Programs & Data Files Notes Data information processed by word processing, spreadsheet or other application programs are stored as data files. Java programs,
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Even-Driven Programming and basic Graphical User Interface.
Java Chapter 9 File Input and Output. Objectives In this chapter you will: Learn how bits, characters, records, and files fit into a data hierarchy Explore.
Documentation Array and Searching. Documentation rules Easy rules: –Naming convention for variables, constants and methods Difficult rules: –Professional.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
CSC 298 Streams and files.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
File Input & Output1 Streams & File I/O. Introduction (1) The Java platform includes a number of packages that are concerned with the movement of data.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Recitation File I/O. File I/O File inFile = new File("data.txt"); File inFile = new File (“/Users/sunil/test.txt");
Introduction to OOP with Java 4th Ed, C. Thomas Wu
CMSC 202 Text File I/O.
CS1101: Programming Methodology
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Accessing Files in Java
Chapter 12 File Input and Output
Presentation transcript:

Selection sort and Merge sort File input/output

HW 2 – Section 02 Avg = 96/100

HW 2 – Section 01 Avg = 89/100

HW2 Answers 1. The difference between the two String methods: equals and equalsIgnoreCase is A. equals compares two string objects, and ignores case considerations b. equalsIgnoreCase compares two string objects, and ignores case considerations c. both equals and equalsIgnoreCase compare two string objects and ignore case considerations. D. both equals and equalsIgnoreCase compare two string objects and consider cases

HW2 Answers 2. In order to determine how many characters contained in a String object myString, we use: a. myString.size() b. myString.size c. myString.length d. myString.length()

HW2 Answers 3. Which of the followings is the constructor of the class Faculty? a public int Faculty (long SSN ) {…..} b public Faculty(long SSN) { ….} c public void Faculty(long SSN) {….} d. public Boolean Faculty(long SSN) {….}

HW2 Answers 4. Fixed-size array declaration has two limitations: inability to handle an overflow situation and possible under utilization of allocated space a. True b. False

HW2 Answers 5. Consider the following method: public int compute(int amt) { int temp = amt * 4; return temp; } How would you characterize amt? a. A data member/ an attribute b. A parameter c. A return value d. A return data type

HW2 Answers 6. A code fragment that declare an array of type double named gpa is: a. double gpa; b. double gpa{ }; c. double gpa[ ]; d. double gpa( );

HW2 Answers 7. Which of the following is incorrect: a. double testArray[] = new double[12]; b. double[] testArray = new double[12]; c. double[] testArray; d. double[] testArray[12];

HW2 Answers 8. Suppose an array contains 256 elements. What are the least and the greatest number of comparisons for a successful search using linear search? a. 1 and 256 b. 0 and 256 c. 1 and 8 d. 0 and 8

HW2 Answers 9. Suppose an array (SORTED) contains 256 elements. What are the least and the greatest number of comparisons for a successful search using binary search? a. 1 and 256 b. 0 and 256 c. 1 and 8 d. 0 and 8

HW2 Answers 10. Given a one dimensional array myArray, what is the correct way of getting the number of elements in this array. a. myArray.length b. myArray.length - 1 c. myArray.size d. myArray.size – 1

HW2 Answers Matching 11. public void setExchangeRate (double rate) { exchangeRate = rate; } CBE A D

HW2 Answers Matching 12. B DF AG CE

HW2 Answers Matching 12. B DF A C E G

HW2 Answers Matching –13. A code fragment that declares an array named profession and allocate the memory to store 20 values of type String:=; –new –[20] –profession –String[ ] –String = ; C D AEB

HW2 Answers Looks for a value in a linear sequence Looks for a value by checking the element in The middle of an array 14. B A A.Binary search B.Linear search

HW2 Answers 15. The following method is invalid. Why? public void myMethod(int myParams) { return (myParams*2); } 1.Void method should not return an int. It should return nothing.

HW2 Answers 16. public String ClassB( ) { description = “Unknown”; return description; } 1.String should be removed because Constructors don’t need return data type 2. Return description should also be removed because constructors should not return anything.

HW2 Answers 17. CurrencyConverter converter = new CurrencyConverter(); double amount = converter.feeRate; 1.feeRate is a private attribute and therefore is not assessible outside of CurrencyConverter class.

HW2 Answers 18. int number[] = new int[10]; for (int i=0; i<number.length; i++) { number[i] = i; } for (int i=0; i<number.length; i++) { if (i%2 == 0) System.out.println(" "+ number[i]); }

HW2 Answers 19 obj = new QuestionOne(); // => amount =10 Obj.setAmount(0.15); // =>amount = 10*(0.15+1) = 10*1.15 = Updated amount is 11.50

HW2 Answers 20 resultStr=“Spring Break" lengthStr =12

Selection Sort public void selectionSort(int[] number) { int minIndex, size, temp; size = number.length;

Selection Sort for (int i=0; i<=size-2; i++) { minIndex = i; for (int j=i+1; j<=size-1; j++) { if (number[j] < number[minIndex]) minIndex=j; } temp= number[i]; number[i]=number[minIndex]; number[minIndex] = temp; }

Selection Sort i=0: minIndex=0 j= 1 number[1]=20 > number[0]=18 j=2 number[2]=17 < number[0]=18 minIndex = 2 j=3 number[3]=40 > number[2]=17 j=4 number[4]=22 > number[2]=17 j=5 number[5]=27 > number[2]=17 j=6 number[6]=30 > number[2]=17 Number

Selection Sort i=0: minIndex=2 temp= number[0] = 18 number[0]=number[2] = 17 number[2] = temp = 18; Number

Selection Sort i=0: minIndex=2 temp= number[0] = 18 number[0]=number[2] = 17 number[2] = temp = 18; Number

Selection Sort i=1: minIndex=1 j=2 number[2]=18 < number[1]=20 minIndex = 2 j=3 number[3]=40 > number[2] = 18 j=4 number[4]=22 > number[2]=18 j=5 number[5]=27 > number[2]=18 j=6 number[6]=30 > number[2]=18 Number

Selection Sort i=1: minIndex=2 temp= number[1] = 20 number[1]=number[2] = 18 number[2] = temp = 20; Number

Selection Sort i=1: minIndex=2 temp= number[1] = 20 number[1]=number[2] = 18 number[2] = temp = 20; Number

Selection Sort i=2: minIndex=2 j=3 number[3]=40 > number[2] =20 j=4 number[4]=22 > number[2]=20 j=5 number[5]=27 > number[2]=20 j=6 number[6]=30 > number[2]=20 Number

Selection Sort i=2: minIndex=2 j=3 number[3]=40 > number[2] =20 j=4 number[4]=22 > number[2]=20 j=5 number[5]=27 > number[2]=20 j=6 number[6]=30 > number[2]=20 Number

Selection Sort i=3: minIndex=3 j=4 number[4]=22 < number[3]=40 minIndex = 4 j=5 number[5]=27 > number[4]=22 j=6 number[6]=30 > number[4]=22 Number

Selection Sort i=3: minIndex=4 temp= number[3] = 40 number[3]=number[4] = 22 number[4] = temp = 40; Number

Selection Sort i=4: minIndex=4 j=5 number[5]=27 < number[4]=40 minIndex=5 j=6 number[6]=30 > number[5]=27 Number

Selection Sort i=4: minIndex=5 temp= number[4] = 40 number[4]=number[5] = 27 number[5] = temp = 40; Number

Selection Sort i=5: minIndex=5 j=6 number[6]=30 < number[5]=40 minIndex=6 Number

Selection Sort i=5: minIndex=6 temp= number[5] = 40 number[5]=number[6] = 30 number[6] = temp = 40; Number

Selection Sort i=5: minIndex=6 temp= number[5] = 40 number[5]=number[6] = 30 number[6] = temp = 40; Number

Concepts 1.String class, methods for String class (length, substring,compareTo,indexOf…) Week 5 slides 2. Instantiable classes: Private/public attributes/methods Different between attribute and methods Constructors Parameters passing for methods Week 7-8 slides

Concepts 3. Array: Definition, how to declare an array, how to allocate memory, how to initialize values How much memory is needed for an array How to determine how many elements an array has Passing an array to a method (passing by reference) Week 9 slides

Concepts 4. Search and Sorting Linear Search Binary Search (Week 10-11)

Merge Sort Start=0, end = 6, mid=(6+0)/2 = 3 Number

Merge Sort. Start=0, end = 3, mid=(3+0)/2 = 1. Start=4, end =6, mid=(4+6)/2=

Passing Array to Methods When an array is passed to a method, only its reference is passed. A copy of the array is not created in the method. That means: we pass the identifier for that array which in fact is a reference to a start address of the array.

Passing Array to Methods Assuming changeArrayValue is one method of a class named ArrayClass public void changeArrayValue(int[] arrayChanged) { for (int i=0; i< arrayChanged.length-1; i++) arrayChanged[i] += 1; } We call this method as:

Passing Array to Methods ArrayClass anObj = new ArrayClass(); int number[5] = new int[5]; for(int i=0; i<number.length; i++) number[0] = i; anObj.changeArrayValue(number);

Passing Array to Methods for(int i=0; i<number.length; i++) number[i] = i; anObj.changeArrayValue(number); number

Final Exam Section 01: MWF begins 12:05- 12:55pm Monday, May 08, :15-5:15pm Section 02: MWF begins 1:10-2pm Wednesday, May 10, 2006: 1-3pm

File Input output File input: the action of reading data from a file File output: the action of writing/saving data to a file Why is it important?. Limited memory. Some data is needed immediately, others are needed monts/weeks/.. from now

File Input output File input: the action of reading data from a file File output: the action of writing/saving data to a file Why is it important?. Limited memory. Some data is needed immediately, others are needed monts/weeks/.. from now

File objects To operate on a file, we must first create a File object (from java.io) and associate this file object to a specific file. How to create a file object: File = new File( ); OR File =new File(, );

File Class Go to pi/java/io/File.html pi/java/io/File.html See the constructors of File class FileFile(String pathname) Creates a new File instance by converting the given pathnameString string into an abstract pathname

Examples File inFile = new File(“sample.dat”); File inFile = new File (“C:/SamplePrograms/test.dat”); Opens the file sample.dat in the current directory. Opens the file test.dat in the directory C:\SamplePrograms using the generic file separator / and providing the full pathname.

File myFile = new File(“C:\\mydocuments”,”sample.dat”); (Windows) File myFile = new File(“C:/mydocuments”,”sample.dat”); (Unix) Example (continue)

File I/O Low level I/OHigh level I/OText file I/O Treat a file As a set of bytes Treat A file As a set of data of primitive Data type Treat A file As a set of text (or String)

Low-Level File I/O To read data from or write data to a file, we must create one of the Java stream objects and attach it to the file. A stream is a sequence of data items, usually 8- bit bytes. Java has two types of streams: an input stream and an output stream. An input stream has a source form which the data items come, and an output stream has a destination to which the data items are going.

Streams for Low-Level File I/O FileOutputStream and FileInputStream are two stream objects that facilitate file access. FileOutputStream allows us to output a sequence of bytes; values of data type byte. FileInputStream allows us to read in an array of bytes.

Low level data input Step 1: Create a File object Step 2: Create a FileInputStream objectFileInputStream Step 3: Declare an array to keep input data, allocate memory for this array Step 4: Read data and process data if neededRead data Step 5: Close the file

Sample: Low-Level File Input //set up file and stream File inFile = new File("sample1.data"); FileInputStream inStream = new FileInputStream(inFile); //set up an array to read data in int fileSize = (int)inFile.length(); byte[] byteArray = new byte[fileSize]; //read data in and display them inStream.read(byteArray); for (int i = 0; i < fileSize; i++) { System.out.println(byteArray[i]); } //input done, so close the stream inStream.close();

Low level data output Step 1: Create a File object Step 2: Create a FileOutputStream objectFileOutputStrea Step 3:Get data ready Step 4:Write data to output streamWrite data Step 5: Close the file

Sample: Low-Level File Output //set up file and stream File outFile = new File("sample1.data"); FileOutputStream outStream = new FileOutputStream( outFile ); //data to save byte[] byteArray = {10, 20, 30, 40, 50, 60, 70, 80}; //write data to the stream outStream.write( byteArray ); //output done, so close the stream outStream.close();

Streams for High-Level File I/O FileOutputStream and DataOutputStream are used to output primitive data values FileInputStream and DataInputStream are used to input primitive data values To read the data back correctly, we must know the order of the data stored and their data types

Setting up DataOutputStream A standard sequence to set up a DataOutputStream object:

Sample Output import java.io.*; class Ch12TestDataOutputStream { public static void main (String[] args) throws IOException {... //set up outDataStream //write values of primitive data types to the stream outDataStream.writeInt( ); outDataStream.writeLong( L); outDataStream.writeFloat( F); outDataStream.writeDouble( D); outDataStream.writeChar('A'); outDataStream.writeBoolean(true); //output done, so close the stream outDataStream.close(); }

Setting up DataInputStream A standard sequence to set up a DataInputStream object:

Sample Input import java.io.*; class Ch12TestDataInputStream { public static void main (String[] args) throws IOException {... //set up inDataStream //read values back from the stream and display them System.out.println(inDataStream.readInt()); System.out.println(inDataStream.readLong()); System.out.println(inDataStream.readFloat()); System.out.println(inDataStream.readDouble()); System.out.println(inDataStream.readChar()); System.out.println(inDataStream.readBoolean()); //input done, so close the stream inDataStream.close(); }

Reading Data Back in Right Order The order of write and read operations must match in order to read the stored primitive data back correctly.

Textfile Input and Output Instead of storing primitive data values as binary data in a file, we can convert and store them as a string data. –This allows us to view the file content using any text editor To output data as a string to file, we use a PrintWriter object To input data from a textfile, we use FileReader and BufferedReader classes –From Java 5.0 (SDK 1.5), we can also use the Scanner class for inputting textfiles

Read data from a text file Step 1: Create a File object Step 2: Create a FileReader objectFileReader Step 3: Create a BufferedReader objectBufferedReader Step 4: Read line by line Step 5: Convert String object to primitive data type as necessary Step 6: Close the file

Create FileReader and BufferedReader objects How to create a FileReader ojbect: FileReader = new FileReader( ); How to create a BufferedReaderobject: BufferedReader = new BufferedReader(<name of a FileReader object); How to read a line.readLine();

Example – Reading a text file E:\Temp\data.dat It's been 84 years, and I can still smell the fresh paint. The china had never been used. The sheets had never been slept in. Titanic was called the Ship of Dreams, and it was. It really was.

Example – Reading a text file import java.io.*; public class FileExample{ public static void main (String[] args) throws IOException { File inFile = new File("e:\\Temp\\data.dat"); FileReader fileReader = new FileReader(inFile); BufferedReader bufferReader = new BufferedReader(fileReader); String inputStr; inputStr = bufferReader.readLine(); while (inputStr != null) { System.out.println(inputStr); inputStr = bufferReader.readLine(); } // end of while bufferReader.close(); System.exit(0); } Package that contains all classes for File I/O

Example – Reading a text file import java.io.*; public class FileExample{ public static void main (String[] args) throws IOException { File inFile = new File("e:\\Temp\\data.dat"); FileReader fileReader = new FileReader(inFile); BufferedReader bufferReader = new BufferedReader(fileReader); String inputStr; inputStr = bufferReader.readLine(); while (inputStr != null) { System.out.println(inputStr); inputStr = bufferReader.readLine(); } // end of while bufferReader.close(); System.exit(0); } IOException (error) must be thrown from main or handle in side this method

Example – Reading a text file import java.io.*; public class FileExample{ public static void main (String[] args) throws IOException { File inFile = new File("e:\\Temp\\data.dat"); FileReader fileReader = new FileReader(inFile); BufferedReader bufferReader = new BufferedReader(fileReader); String inputStr; inputStr = bufferReader.readLine(); while (inputStr != null) { System.out.println(inputStr); inputStr = bufferReader.readLine(); } // end of while bufferReader.close(); System.exit(0); } Steps 1-3

Example – Reading a text file import java.io.*; public class FileExample{ public static void main (String[] args) throws IOException { File inFile = new File("e:\\Temp\\data.dat"); FileReader fileReader = new FileReader(inFile); BufferedReader bufferReader = new BufferedReader(fileReader); String inputStr; inputStr = bufferReader.readLine(); while (inputStr != null) { System.out.println(inputStr); inputStr = bufferReader.readLine(); } // end of while bufferReader.close(); System.exit(0); } Step 4 in a while loop

Example – Reading a text file import java.io.*; public class FileExample{ public static void main (String[] args) throws IOException { File inFile = new File("e:\\Temp\\data.dat"); FileReader fileReader = new FileReader(inFile); BufferedReader bufferReader = new BufferedReader(fileReader); String inputStr; inputStr = bufferReader.readLine(); while (inputStr != null) { System.out.println(inputStr); inputStr = bufferReader.readLine(); } // end of while bufferReader.close(); System.exit(0); } Step 6

Result

Write data to a text file Step 1: Create a File object Step 2: Create a FileOutputStream objectFileOutputStream Step 3: Create a PrinterWriter objectPrinterWrite Step 4: Write line(s) Step 5: Close the file

Create a FileOutputSTream and PrintWriter objects How to create a FileOutputStream objects: FileOutputStream = new FileOutputStream( ); How to create a PrintWriter object PrintWriter = new PrintWriter( ); How to write a string to a file.println( ); More details on this link

Example import java.io.*; public class OutputExample{ public static void main (String[] args) throws IOException { File inFile = new File("e:\\Temp\\output.dat"); FileOutputStream fileStream = new FileOutputStream(inFile); PrintWriter printWriter = new PrintWriter(fileStream); String inputStr; int number[] = new int[10]; for (int i=0;i<number.length; i++) { number[i] = i+1; printWriter.println(number[i]); } printWriter.close(); System.exit(0); }

Example import java.io.*; public class OutputExample{ public static void main (String[] args) throws IOException { File inFile = new File("e:\\Temp\\output.dat"); FileOutputStream fileStream = new FileOutputStream(inFile); PrintWriter printWriter = new PrintWriter(fileStream); String inputStr; int number[] = new int[10]; for (int i=0;i<number.length; i++) { number[i] = i+1; printWriter.println(number[i]); } printWriter.close(); System.exit(0); } Step 1-3

Example import java.io.*; public class OutputExample{ public static void main (String[] args) throws IOException { File inFile = new File("e:\\Temp\\output.dat"); FileOutputStream fileStream = new FileOutputStream(inFile); PrintWriter printWriter = new PrintWriter(fileStream); String inputStr; int number[] = new int[10]; for (int i=0;i<number.length; i++) { number[i] = i+1; printWriter.println(number[i]); } printWriter.close(); System.exit(0); } Step 4 (in for loop)

Example import java.io.*; public class OutputExample{ public static void main (String[] args) throws IOException { File inFile = new File("e:\\Temp\\output.dat"); FileOutputStream fileStream = new FileOutputStream(inFile); PrintWriter printWriter = new PrintWriter(fileStream); String inputStr; int number[] = new int[10]; for (int i=0;i<number.length; i++) { number[i] = i+1; printWriter.println(number[i]); } printWriter.close(); System.exit(0); } Step 5 (close file)

Just in time review 1. Two steps must occur before we can read data from a file: and A.Associate it to a file B.Create a File object

Just in time review 2. The following two statements will associate file 1 and file2 to the same file: File file1 = new File(“c:\\one”,”two.dat”); File file2 = new File (“C:\\one\\two.dat”) A. True B. False