Using Files as Input/Output in Java 5.0 Applications



Similar documents
CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Object-Oriented Programming in Java

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

13 File Output and Input

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Reading Input From A File

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Introduction to Java

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Introduction to Java. CS 3: Computer Programming in Java

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Chapter 2: Elements of Java

Building a Multi-Threaded Web Server

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Topic 11 Scanner object, conditional execution

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Building Java Programs

Chapter 3. Input and output. 3.1 The System class

Event-Driven Programming

Course Intro Instructor Intro Java Intro, Continued

Some Scanner Class Methods

AP Computer Science Java Mr. Clausen Program 9A, 9B

Comp 248 Introduction to Programming

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

6.1. Example: A Tip Calculator 6-1

CS 106 Introduction to Computer Science I

Chapter 2 Introduction to Java programming

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Homework/Program #5 Solutions

AP Computer Science Static Methods, Strings, User Input

Introduction to Programming

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

public static void main(string[] args) { System.out.println("hello, world"); } }

Building Java Programs

Basics of Java Programming Input and the Scanner class

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Building Java Programs

Install Java Development Kit (JDK) 1.8

AP Computer Science File Input with Scanner

J a v a Quiz (Unit 3, Test 0 Practice)

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Line-based file processing

Files and input/output streams

D06 PROGRAMMING with JAVA

Chapter 10. A stream is an object that enables the flow of data between a program and some I/O device or file. File I/O

Moving from CS 61A Scheme to CS 61B Java

Sample CSE8A midterm Multiple Choice (circle one)

CS506 Web Design and Development Solved Online Quiz No. 01

Explain the relationship between a class and an object. Which is general and which is specific?

CS 1302 Ch 19, Binary I/O

CS 111 Classes I 1. Software Organization View to this point:

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

JAVA ARRAY EXAMPLE PDF

Using Two-Dimensional Arrays

Programmierpraktikum

INPUT AND OUTPUT STREAMS

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Arrays. Introduction. Chapter 7

Part I. Multiple Choice Questions (2 points each):

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

Visit us at

D06 PROGRAMMING with JAVA

In this Chapter you ll learn:

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Copy the.jar file into the plugins/ subfolder of your Eclipse installation. (e.g., C:\Program Files\Eclipse\plugins)

Data Structures Lecture 1

Programming Fundamentals I CS 110, Central Washington University. November 2015

WRITING DATA TO A BINARY FILE

Iteration CHAPTER 6. Topic Summary

Lecture 5: Java Fundamentals III

java Features Version April 19, 2013 by Thorsten Kracht

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Programming and Data Structures with Java and JUnit. Rick Mercer

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

IMDB Data Set Topics: Parsing Input using Scanner class. Atul Prakash

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Step 4: Configure a new Hadoop server This perspective will add a new snap-in to your bottom pane (along with Problems and Tasks), like so:

Creating a Simple, Multithreaded Chat System with Java

3 Improving the Crab more sophisticated programming

Java CPD (I) Frans Coenen Department of Computer Science

CS170 Lab 11 Abstract Data Types & Objects

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Building Java Programs

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Word Count Code using MR2 Classes and API

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Java Application Developer Certificate Program Competencies

Arrays in Java. Working with Arrays

Transcription:

Using Files as Input/Output in Java 5.0 Applications The goal of this module is to present enough information about files to allow you to write applications in Java that fetch their input from a file instead of from the keyboard. Files are useful in testing programs because they save the programmer from re-entering (typing) data into the program every time the program is run. Writing a program to read from a file instead of from the keyboard simply involves adding a few additional pieces to your program. In order to use file as input or output, you need to import the pre-defined Java io (input output) package and the Scanner Class. Example: import java.io.* ; import java.util.scanner; You will need also an object from the class Scanner and another object from the class FileInputStream when reading from a file. Example: Scanner infile = new Scanner(new FileInputStream("data.txt")); Before you can read from a file, you must first open the file. Opening a file sets up a communication conduit between your program and the file. Opening a file does not actually read the data in the file, it just sets up a conduit so that the program may read data from the file by using that conduit. For example, Scanner infile = new Scanner(new FileInputStream("data.txt")); where data.txt is the name of the file that we are using as input. Picture the class FileInputStream making a pipe (one-way going from the file towards the program) between the program and the file. In the example above, the end of the pipe anchored in the program would have a label infile, and at the other end the label data.txt. Checking the results of the open request There are some conditions under which the statement above can fail (such as the file does not exist, or the file is present, but your program does not have read-access to it, or the file contains the wrong type of data). In Java, it is very important to make sure that the input file has been successfully opened. If the file cannot be opened then Java will automatically generate an error (this is called throwing an exception ). The class Scanner generates this exception. Fortunately for us, this is done very nicely by using a try-catch block. Example: File Input and Output in Java 1

try FileInputStream textfile = new FileInputStream ( data.txt ); Scanner infile = new Scanner (textfile); System.out.println( File data.txt has been opened. ); catch (FileNotFoundException fnfe) System.out.println( File data.txt was not found! ); Reading from a file (File Input) When your program reads from a file, it always keeps track of what it has read previously so when you ask it to read again, it always reads the next un-read item. This is a very important concept to remember. There are many ways to read data from a file. Reading from a file mostly depends on how the data has been stored in the file (one item per line, more than one item per line) and the type of that data. For simplicity, we are going to restrict our discussion to files where each line contains only one piece of data. Examples: File A 20 33 124-5 -10 File B Hello Goodbye How What Where Reading from files shaped like the ones above is very easy in Java. All we have to do is to read one line at the time from the file and then do the necessary type conversions using the Wrapper Classes (if they are needed). Reading only one line at the time is done by the nextline() method of the class Scanner. To do the type conversions we use the Wrapper classes (Integer, Double, etc.) as we have done in the past. Example: String oneline = infile.nextline(); This statement will read the first line of the file associated with the object infile and store its contents into the String object oneline. File Input and Output in Java 2

Then we need to extract the value from the String object and store it into a local variable of the proper type. So, let s assume that the file data.txt looks like File A (a file of integers) above. int number = Integer.parseInt(oneLine); This statement will extract the integer value stored in the String oneline and it will assign it to the integer variable number. As you can see, it is very important to know in advance the type of data that the file has. Otherwise, we could end up using the wrong wrapper class. Because of this, it is important that we catch this type of error (throw an exception). The class FileReader takes car of this as follows: try FileInputStream textfile = new FileInputStream ( data.txt ); Scanner infile = new Scanner (textfile); System.out.println( File data.txt has been opened. ); String oneline = infile.nextline(); int number = Integer.parseInt(oneLine); catch (FileNotFoundException fnfe) System.out.println( File data.txt was not found! ); catch (IOException ioe) System.out.println( Error while reading the file. Possible wrong type! ); Obviously, the file has more than one line. So, how do we read more than one line? How do we know how many lines to read? These are very important questions! One solution is to physically count the number of lines in the file. This is not a good approach because we couldn t write an application generic enough to handle file of integers (independent of the number of lines). The correct approach is to use a loop with the correct stopping conditions. So, let s use a while loop to read one line at the time until there are no more lines left in the file. Example: #1 int number; #2 while(infile.hasnextline()) File Input and Output in Java 3

#3 String oneline = infile.nextline(); #4 number = Integer.parseInt(oneLine); #5 System.out.println( Number read is + number); Line #1 declares a local variable called number. This variable is used to staore one integer value at the time. Line #2 is the while loop we use to read all the lines in the file. The loop guard (infile.hasnextline()) checks to see if there is line in the file to be read. The method hasnextline() returns either true (there is a line) or false (there are no lines). If there is no data to be read from the file then the condition is false and the loop will not iterate. Otherwise, there is some data stored in the file and the condition is true. Once inside the loop (meaning that there is data to be read from the file) we do the following. Line #3 reads the first line from the input file. Line #4 extracts the integer value read from the file and stores it in the variable number. Line #5 prints the value read and stored. Line #6 attempts to read the next line in the file. After this, the loop guard in Line #2 is checked again. So, when does the loop stop? Assume that the last line has been read by Line #3. Line #4 and Line #5 get executed as expected. After this, the loop guard in Line #2 is checked again, but this time there is no line to be read (we are at the end of the file). Thus, the guard condition becomes false (there are no more lines to be read). Hence, the loop stops! Sample application The application below reads integer numbers from a file, sums them, and displays the total at the end. import java.util.scanner; import java.io.*; File Input and Output in Java 4

public class FileInput public static void main (String[] args) try Scanner infile = new Scanner(new FileInputStream("numbers.txt")); int sum = 0; int number = 0; while(infile.hasnextline()) String oneline = infile.nextline(); number = Integer.parseInt(oneline); System.out.println(number); sum = sum + number; infile.close(); System.out.println("The sum of the numbers is " + sum); catch(filenotfoundexception fnfe) System.out.println("File not found!!"); //end of main //end of class Using a string for the filename In the previous example, in order to change what file the program reads from, you must edit the program and then re-compile. That's pretty clumsy. Is there an easier way? The answer is yes. We will use an object of the class String to hold the file name that the user wants the program to process. The code below will ask the user what file to read, and then proceed to process that file. Scanner keyboard = new Scanner(System.in); System.out.print("Enter the name of the file: "); String filename = keyboard.nextline(); Scanner infile = new Scanner(new FileInputStream(fileName)); File Input and Output in Java 5

First, we instantiate keyboard, an object of the class Scanner, which is going to accept input from the console (i.e. typed by the user). Then, we prompt the user for the name of the file to be read. Finally, we use the method nextline() to read the sequence of characters and instantiate infile, an object of the class Scanner, which is going to fetch the input data from the file chosen be the user. Other type of input files So far, we have discussed files, which contain only one item per line. How about files which contain several pieces of data in a line? In this case, we would still read one line at the time from the file. Then we would use the StringTokenizer class to fetch each item (or token) from the line read. This technique and similar ones are discussed in a more advanced course. Caution If you are using Eclipse to compile and execute your application you must physically place the input file in the same folder where your project is located. Otherwise, Eclipse will not be able to locate your input file. Similarly, output files can be found in the same folder where your project is located. File Input and Output in Java 6