File Output Writing data out to a file 1. Output to files  StreamWriter : An object in the System.IO namespace that lets you print output to a destination.

Slides:



Advertisements
Similar presentations
Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Advertisements

Computer and Programming
BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT. 22 PrintStream : An object in the java.io package that lets you print output to a destination such as.
Files & Streams. Files Introduction Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
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:
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Computer and Programming File I/O File Input/Output Author: Chaiporn Jaikaeo, Jittat Fakcharoenphol Edited by Supaporn Erjongmanee Lecture 13.
Copyright 2008 by Pearson Education C# File I/O Line by line and token-based file input.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
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.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
1 Hours question Given a file hours.txt with the following contents: 123 Kim Eric Stef
BUILDING JAVA PROGRAMS CHAPTER 6 File Processing.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Building java programs chapter 6
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
Topic 19 file input, line based Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Advanced File Processing NOVEMBER 21 ST, Objectives Write text output to a file. Ensure that a file can be read before reading it in your program.
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:
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
BIM313 – Advanced Programming File Operations 1. Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2.
FILE PROCESSING. Reading files To read a file, pass a File when constructing a Scanner. Scanner name = new Scanner(new File(" file name ")); Example:
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
CSC 298 Streams and files.
1 Reminder: String Scanners A Scanner can be constructed to tokenize a particular String, such as one line of an input file. Scanner = new Scanner( );
File Processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from "We can only.
1 Line-based file processing suggested reading:6.3.
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 Lecture 21 Arrays – Part 1.
Files and Streams Lec Introduction  Files are used for long-term retention of large amounts of data, even after the program that created the.
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
Building Java Programs
Hours question Given a file hours.txt with the following contents:
COMP 2710 Software Construction File I/O
Building Java Programs
Today’s Lecture I/O Streams Tools for File I/O
Files & Streams.
CSc 110, Autumn 2016 Lecture 17: Line-Based File Input
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 21: Line-Based File Input
Suggested self-checks: Section 7.11 #1-11
Building Java Programs
CSC 111 Exam 3 Review Fall 2005.
Building Java Programs
Hours question Given a file hours.txt with the following contents:
Building Java Programs
Building Java Programs
Building Java Programs
Topic 19 file input, line based
File Input and Output.
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Building Java Programs
Lecture 18 File I/O CSE /5/2019.
Week 7 - Monday CS 121.
Building Java Programs
Presentation transcript:

File Output Writing data out to a file 1

Output to files  StreamWriter : An object in the System.IO namespace that lets you print output to a destination such as a file.  Any methods you have used on Console.out (such as Write, WriteLine ) will work on a StreamWriter.  Syntax: StreamWriter name = new StreamWriter( " file name " );  Example: StreamWriter output = new StreamWriter( "out.txt" ); output.WriteLine("Hello, file!"); output.WriteLine("This is a second line of output."); output.Dispose();  If you don’t call.Dispose() then C# will not write the data to the file 2

Output to files  Example: StreamWriter output = new StreamWriter( "out.txt" ); output.WriteLine("Hello, file!"); output.WriteLine("This is a second line of output."); output.Dispose();  If you don’t call.Dispose() then C# will not write the data to the file  You can use the ‘using’ construct, similar to the input slides: using (StreamWriter output = new StreamWriter("out.txt")) { output.WriteLine("Hello, file!"); output.WriteLine("This is a second line of output."); } // output.Dispose() called here automatically // This is C#-specific  We’ll use the.Dipose() pattern in these slides (so you’re used to it) 3

Details about StreamWriter StreamWriter name = new StreamWriter( " file name " );  If the given file does not exist, it is created.  If the given file already exists, it is overwritten.  The output you print appears in a file, not on the console. You will have to open the file with an editor to see it.  Do not open the same file for both reading ( StreamReader ) and writing ( StreamWriter ) at the same time.  You will overwrite your input file with an empty file (0 bytes). 4

Console.Out and StreamWriter  The console output object, Console.Out, is a TextWriter.  The StreamWriter class is a subclass of the TextWriter class. TextWriter out1 = Console.Out; TextWriter out2 = new StreamWriter( "data.txt" ); out1.WriteLine("Hello, console!"); // goes to console out2.WriteLine("Hello, file!"); // goes to file out2.Dispose();  A reference to Console.Out can be stored in a TextWriter variable.  Printing to that variable causes console output to appear.  You can pass Console.out to a method as a TextWriter.  Allows a method to send output to the console or a file. 5

Hours question  Given a file hours.txt with the following contents: 123 Kim Eric Stef  Task: compute hours worked by each person  Send the output to the file hours_out.txt.  The program will produce no console output.  But the file hours_out.txt will be created with the text: Kim (ID#123) worked 31.4 hours (7.85 hours/day) Eric (ID#456) worked 36.8 hours (7.36 hours/day) Stef (ID#789) worked 39.5 hours (7.9 hours/day)

Hours answer public void Slide_8() { char[] delimiters = { ' ', '\t' }; TextReader input = new StreamReader("Files/hours.txt"); StreamWriter output = new StreamWriter("Files/hours_out.txt"); String sLine; while ((sLine = input.ReadLine()) != null) { string[] tokensFromLine = sLine.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); if (tokensFromLine.Length < 3) { Console.WriteLine("Line contained fewer than 3 tokens! {0}", sLine); continue; // get next line } 7

Hours answer int ID; if (false == Int32.TryParse(tokensFromLine[0], out ID)) { Console.WriteLine("Line did not start with ID number! {0}", sLine); continue; // get next line } string name = tokensFromLine[1]; double sum = 0.0; int count = 0; for (int i = 2; i < tokensFromLine.Length; i++) { double dNum; if (Double.TryParse(tokensFromLine[i], out dNum)) { sum += dNum; count++; } 8

Hours answer if( count == 0 ) // didn't find any hours worked { Console.WriteLine("Line did not find any hours worked! {0}", sLine); continue; // get next line } double average = sum / count; output.WriteLine("{0} (ID#{1}) worked {2} hours ({3} hours/day)", name, ID, sum, average); // If you change output.WriteLine to Console.WriteLine // you’d see the output on the screen } output.Dispose(); } 9

Credits  The Chapter 6 slides from Reges and Stepp’s excellent “Building Java Programs” textbook was used as the basis of these slides.  The instructor was given permission by the publisher to use those slides in this course.  Those slides were modified to work in C#.  Java has two methods of interacting with files: token-based and line-based.  C#’s implementation does not have this distinction (which avoids some problems at the cost of requiring more complicated code) 10