0% found this document useful (0 votes)
4 views39 pages

Chapter 11

Chapter 11 of CS 200 covers the use of printf for string formatting in Java, file input and output, and the organization of file paths. It explains the difference between relative and absolute paths, how to read from and write to files using various classes, and the importance of buffering in file operations. The chapter also includes examples and error handling related to file operations.

Uploaded by

kenzhao0621
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views39 pages

Chapter 11

Chapter 11 of CS 200 covers the use of printf for string formatting in Java, file input and output, and the organization of file paths. It explains the difference between relative and absolute paths, how to read from and write to files using various classes, and the importance of buffering in file operations. The chapter also includes examples and error handling related to file operations.

Uploaded by

kenzhao0621
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 11

CS 200 - Programming I - Summer 2020


Mark Powers
printf - Replace certain parts of string with values
● String name = [Link]();
[Link](“Hello, “ + name + “!”);
● [Link](“Hello, %s!\n”, name);
printf - Replace certain parts of string with values
● String name = [Link]();
[Link](“Hello, “ + name + “!”);
● [Link](“Hello, %s!\n”, name);

%s is replaced with
the value given
after the string
printf - Replace certain parts of string with values
● String name = [Link]();
[Link](“Hello, “ + name + “!”);
● [Link](“Hello, %s!\n”, name);
● [Link](“%s %s”, str1, str2)
printf - Replace certain parts of string with values
● String name = [Link]();
[Link](“Hello, “ + name + “!”);
● [Link](“Hello, %s!\n”, name);
● [Link](“%s %s”, str1, str2)
printf - subspecifiers
● Between % and type char, we can specify how it should be printed

● Number - width
○ If number starts with a 0, width is padded with 0s, otherwise with spaces
● .Number - how many decimal places
● -
○ Left align
● +
○ Show sign
Examples
What is printed out at each line?

String name = “Sally”;


int num = 100;
double x = 3.14159;
[Link](“Hello ‘%9s’, how are you?\n”, name);
[Link](“%05d, %.2f”, num, x);
Example: Grocery List with printf

the Farmer's Market


5
Going to the Farmer's Market to get:
apples
5 apples
3
3 gallons of milk
milk
2.00 pounds of cheese
2
cheese
“Root” directory
Paths - Where are files stored
/
● Files are organized in a “tree”
○ /home/mark/eclipse-workspace/Ch11/src
home boot

eclipse-workspace documents downloads

ch10 ch11

[Link]
● Programs keep track of “current working directory”
○ Where they are being run from usually (depends on computer)
● Slash (/) separates directories in a given path
● Windows uses \ (in Java you must escape \\ ) Mac and Linux use /
Paths - Relative vs Absolute
● Relative:
○ Path starts are current working directory
○ Eclipse uses project folder as CWD
● Absolute
○ Path starts from root directory
○ Start with /
● . (dot) refers to CWD
● .. (dot dot) refers to directory above CWD
● On Windows, paths may optionally include drive letters
○ C:, L:, D:
Paths - Examples
/
● Current directory: /home
Path: documents/[Link] home boot

eclipse-workspace documents downloads

ch10 ch11 [Link]

● Current directory: /home [Link]


Path: /boot
● Current directory: /home
Path: /eclipse-workspace/ch11/[Link]
Paths - Examples
/
● Current directory: /home/mark
Path: documents/[Link] home boot

eclipse-workspace documents downloads

ch10 ch11 [Link]

● Current directory: /home/mark [Link]


Path: /boot
File does not exist: since
● Current directory: /home/mark there is no eclipse-workspace
Path: /eclipse-workspace/ch11/[Link] directory in root
Paths - Examples
/
● Current directory: /home
Path: documents/[Link] home boot

eclipse-workspace documents downloads

ch10 ch11 [Link]

● Current directory: /home [Link]


Path: /boot
● Current directory: /home
Path: eclipse-workspace/ch11/[Link]
Examples (instead of tree, we show indentation)
/
Which is absolute path to [Link]?
eclipse
p1
[Link]
p2
[Link]
files
[Link]
Examples (instead of tree, we show indentation)
/
Which is relative path from
eclipse
CWD=/eclipse/p1/
p1
to [Link]?
[Link]
p2 ../p2/files/[Link]
[Link] ./p2/files/[Link]
files ../files/[Link]
[Link] /eclipse/p2/files/[Link]
Examples (instead of tree, we show indentation)
/
Which is relative path from
eclipse
CWD=/eclipse/p2/files
p1
to [Link]?
[Link]
p2 ../[Link]
[Link] ./p1/[Link]
files ../../p1/[Link]
[Link] /eclipse/p1/[Link]
Examples: Relative or absolute
● C:\users\j\Documents\[Link]
● downloads/[Link]
● /usr/bin/java/javac
● C:users\j\Documents\[Link]

In Eclipse: Store files in project folder


● Use relative paths - Absolute paths wills not work in zylabs
How to set up Ch 11 Zylabs in eclipse
● demo
Java - File Input and Output
File class - File f = new File(“path/[Link]”);

● Creates object to represent the file - Accesses metadata information


○ Name, size, path, attributes

● Reading:
○ Scanner, InputStream, FileInputStream, FileReader, BufferedReader
● Writing:
○ OutputStream, FileOutputStream, PrintStream, PrintWriter
File Meta data
● File file = new File(“”);
if([Link](){
[Link]([Link]());
[Link]([Link]());
if([Link]()){
[Link]([Link]([Link]()));
}
}
File input - Using Scanner
File file = new File(“[Link]”);
Scanner fileScnr = new Scanner(file);
// use fileScnr like any normal scanner
// Treat file content as input to Scanner
[Link](); // allow other processes to use file
Print out file contents
File file = new File(“[Link]”);
Scanner fileScnr = new Scanner(file);
while([Link]()){
[Link]([Link]());
}
[Link](); // allow other processes to use file
Example: What is printed out?
A1
Scanner scnr = new Scanner(new File(“[Link]”)); 2 B
[Link](); C3
int n = 0; 4D
if([Link]()){ E5
n = [Link]();
[Link]();
} else {
[Link]();
}
[Link]([Link]());
Errors
● We cannot know that a file exists or that we can read it
● So we have checked exception:
○ FileNotFoundException
○ IOException
● Finally block: after catch
○ Always runs when finishing either the try or a catch block
○ Use finally block to close Scanner
File Output - Use PrintWriter
PrintWriter outFS;
try {
outFS = new PrintWriter(new File(“[Link]”));
[Link]("hello world");
} catch (FileNotFoundException e) {
[Link]();
} finally {
if(outFS != null) [Link]();
}
Try with Resources - Java 7+
try (PrintWriter outFS = new PrintWriter(new File(“[Link]”))) {
[Link]("hello world");
} catch (FileNotFoundException e) {
[Link]();
}

Will automatically close outFS


Example - Lakes with files
Copy the data about county lakes and save it into a file [Link]:
[Link]

Write a program that outputs a file lakes_output.txt that converts each acre
measurement to sq miles: 1 acre = 0.0015625 sq miles

In eclipse, you may have to refresh the project for files to show up.
Buffers
● Doing input and output are expensive

File IO ->

[Link]
Buffers
● Before it is written, data is placed in a buffer
● When the buffer gets full, then it is sent to the file system
● flush() pushes this buffer out to the file system before it is full
Buffers
● PrinterWriter pw = new PrintWriter(new File(“[Link]”));
for(int i = 0; i < 10; i++){
[Link](i);
}
[Link]();
Buffers
● PrinterWriter pw = new PrintWriter(new File(“[Link]”));
for(int i = 0; i < 10; i++){
[Link](i);
}
[Link]();

0
Buffers
● PrinterWriter pw = new PrintWriter(new File(“[Link]”));
for(int i = 0; i < 10; i++){
[Link](i);
}
[Link]();

0 1
Buffers
● PrinterWriter pw = new PrintWriter(new File(“[Link]”));
for(int i = 0; i < 10; i++){
[Link](i);
}
[Link]();

0 1 2 3 4 5 6 7 8 9
Buffers
● PrinterWriter pw = new PrintWriter(new File(“[Link]”));
for(int i = 0; i < 10; i++){
[Link](i);
}
[Link]();
Buffers
● PrinterWriter pw = new PrintWriter(new File(“[Link]”));
for(int i = 0; i < 10; i++){
[Link](i);
[Link]();
}
[Link]();
Buffers
● PrinterWriter pw = new PrintWriter(new File(“[Link]”));
for(int i = 0; i < 10; i++){
[Link](i);
[Link]();
}
[Link]();

0
Buffers
● PrinterWriter pw = new PrintWriter(new File(“[Link]”));
for(int i = 0; i < 10; i++){
[Link](i);
[Link]();
}
[Link]();
Buffers
● PrinterWriter pw = new PrintWriter(new File(“[Link]”));
for(int i = 0; i < 10; i++){
[Link](i);
[Link]();
}
[Link]();

1
Buffers
● PrinterWriter pw = new PrintWriter(new File(“[Link]”));
for(int i = 0; i < 10; i++){
[Link](i);
[Link]();
}
[Link]();

You might also like