CSC1401 Input and Output (with Files)

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 14 / 2009 Instructor: Michael Eckmann.
Advertisements

CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: File Output; Reference Semantics reading: , 7.1, 4.3, 3.3 self-checks:
18 File handling1June File handling CE : Fundamental Programming Techniques.
Reading Information From the User Making your Programs Interactive.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Announcements Quiz 2 Grades Posted on blackboard.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
1 BUILDING JAVA PROGRAMS CHAPTER 6 FILE PROCESSING.
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
BHCSI Programming Contests. Three contests Mock Contest #1  Friday, July 16 th  2:15 – 4:45pm UCF High School Online Contest  Thursday, July 22 nd.
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
CS1020 Data Structures and Algorithms I Lecture Note #16 File Processing.
Introduction to programming in java
Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
For Friday Finish reading chapter 9 WebCT quiz 17.
CSC 211 Java I File I/O and more methods. Today’s plan Homework discussion Reading lines from files Writing lines to files All of the above, using methods.
Chapter 2 Clarifications
Building Java Programs
File I/O CLI: File Input CLI: File Output GUI: File Chooser
Streams & File Input/Output (I/O)
Reading from a file and Writing to a file
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Software Development Packages
Strings and File I/O.
File Input and Output TOPICS File Input Exception Handling File Output.
Introduction to Methods in java
Week 14 - Wednesday CS 121.
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
File Input and Output TOPICS File Input Exception Handling File Output.
Building Java Programs
Building Java Programs
Know for Quiz Everything through Last Week and Lab 7
Introduction to Computing Using Java
File Handling in Java January 19
class PrintOnetoTen { public static void main(String args[]) {
slides created by Ethan Apter
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
F II 6. Using Libraries Objectives
Building Java Programs
Building Java Programs
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Building Java Programs
Building Java Programs
Building Java Programs
slides created by Ethan Apter
Building Java Programs
Building Java Programs
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Validation We have covered one way to get user input, using Scanner to get it from the console We will soon cover at least one additional way to get.
Presentation transcript:

CSC1401 Input and Output (with Files)

Learning Goals Computing concepts Input and output with files in Java Using the Scanner class for file input, and PrintWriter for output

Recall … import java.util.*; // needed for scanner class IOTesting { public static void main(String [] args) { Scanner scan = new Scanner(System.in); int value = scan.nextInt(); …

Using Files for input Instead of System.in, we can specify a file Two differences We need to import java.io.*; Doing file input can lead to an exception if the type of data we are requesting doesn’t match what’s in the file

Note the changes import java.util.*; // needed for scanner import java.io.*; // needed for file class FileIO { public static void main(String [] args) throws Exception // throws exception is for file i/o { Scanner sc = new Scanner(System.in); Scanner sc2 = new Scanner(new File("data.txt")); int i = sc2.nextInt(); …

Let’s create a file and try Can use Notepad, or just about anything else besides MS Word Let’s read in a bunch of scores into an array

Reading several numbers from a file Scanner sc2 = new Scanner(new File("data.txt")); int [] list = new int[10]; int count = 0; while (sc2.hasNextInt() && count < 10) { list[count] = sc2.nextInt(); count = count +1; } for (int i = 0; i< count;i++) { System.out.println("list[“ + i + "] = “ + list[i]); } sc2.close(); // when you open a file, you // should close it

File Output Again, we’ll change from System.out to something else

An example with file output import java.io.*; // needed for file class IO { public static void main(String [] args) throws Exception // throws exception is for file i/o { int i = 5; PrintWriter steveout = new PrintWriter( new FileWriter("silly2.txt")); steveout.println(i+ " is the value"); steveout.close(); …

Summary We use Printwriter for output to a file (the Media Computation book has an alternate approach) We use a Scanner object for getting input from a file

Reading Assignment Media Computation Chapter 12, Section 3 (but not section 12.3.3)