Cheng, Wei COMP110-001 June 10, 2014
Title
File Operations
Today
• Files, Directories, Path
• Character set
• Reading from a file
• Writing to a file
Persistence
• RAM is not persistent
• To make data persistent, you have to store data to:
• Or maybe in the old days:
Working with Files
• The data stored in these persistent storage are normally in the form of files
• Have you tried to open a movie DVD in your computer using file explorer?
• You will probably see some folders and files like this:
Working with Files
• In short:
– To store our data, we often need to write to files.
– To read data, we often need to read from files.
• We will cover some basics about files and directories in Windows / Linux & Mac OS first
Files and Directories
• Files are stored in directories or folders in a tree structure:
• A directory can contain one or more files and/or directories.
• The root directory in Windows is the drive name ( C: or D: , don’t miss the : )
• The root directory in Unix/Linux/MacOS is /
Files and Directories – Path to file
• A file is identified by its path through the file system, beginning from the root node.
• For example, the statusReport file in the previous figure is described by the following notation in the Linux/Unix/MacOS:
/home/sally/statusReport
• In Microsoft Windows, statusReport is described by the following notation:
C:\home\sally\statusReport
The character used to separate the directory names (also called the delimiter) is forward slash (/) in Linux/Unix/MacOS, and backslash slash (\) in Windows.
Relative and Absolute Path • A path is either relative or absolute. An absolute path always contains the root
element and the complete directory list required to locate the file.
• For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string.
• A relative path needs to be combined with another path in order to access a file.
• For example, joe/foo is a relative path.
• Without more information, a program cannot reliably locate the joe/foo directory in the file system.
• In java, when you write a relative path, it’s relative to the working directory.
Path Class in Java (only in Java SE 7)
• Create an instance of “Path” to represent the location information of a file:
import java.nio.file.*;
….
Linux/Unix/MacOS:
Path p = Paths.get("/home/sally/statusReport");
Windos:
Path p = Paths.get("C:\\home\\sally\\statusReport");
Or
Path p = Paths.get("C:/home/sally/statusReport");
Java’s Input/Output Mechanism • Very complicated design based on “streams”
• Here I will give you some short-cuts to avoid the details at stream level.
• I am not following textbook
Text Files vs Binary Files • Text file: a sequence of characters
• Binary file: pack values into binary representation
• We only cover text file I/O in this course
Character Set • A set of characters and a mapping from this set of characters to binary
representation in computers
• When you are working with plain English text, most of the time you probably need ASCII (American Standard Code for Information Interchange):
• Other character sets: Unicode, GBK, JIS…..
Before Any File Operation
import java.nio.charset.Charset;
…
Charset charset = Charset.forName("US-ASCII");
Or
Charset charset = Charset.defaultCharset();
This creates an instance of charset to represent the character set in reading/writing files. (This tells Java which “language” we are speaking)
Reading All Lines From a Text File Class Files
public static List<String> readAllLines(Path path, Charset cs) throws IOException
Read all lines from a file.
Parameters:
path - the path to the file
cs - the charset to use for decoding
Returns:
the lines from the file as a List;
Throws:
IOException - if an I/O error occurs reading from the file or a malformed or unmappable byte sequence is read
SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the file.
Reading All Lines From a Text File import java.io.IOException; import java.nio.charset.Charset;
import java.nio.file.*; import java.util.List;
public class ReadFileAsListDemo {
public static void main(String[] args) {
Path path = Paths.get("students_name_list.txt");
try {
List<String> lines = Files.readAllLines(path, Charset.defaultCharset());
} catch (IOException e) {
System.out.println(e.getMessage());
}
for (String line : lines) { // you can also use lines.size() and lines.get(i) to do the loop
System.out.println(line);
// do sth meaningful about this line
}
}
}
Reading All Lines From a Text File
Caution:
• This method reads all file content into memory (as a list of Strings)
Pros?
Cons?
Reading a File: Line by Line
Charset charset = Charset.defaultCharset();
Path path = Paths.get("C:/data/students_name_list.txt");
try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
String line = null;
while ( (line = reader.readLine() ) != null) {
System.out.println(line);
// do sth meaningful about this line
}
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
Calculate the Sum of Numbers in File Charset charset = Charset.defaultCharset();
Path path = Paths.get("C:/data/students_score_list.txt");
int sum = 0;
try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
String line = null;
while ( (line = reader.readLine() ) != null) {
sum += Integer.parseInt( line ); // each line is an integer number
}
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
System.out.println(sum);
Writing to a File
Charset charset = Charset.defaultCharset();
Path path = Paths.get("C:/data/students_score_list.txt");
String s = 100 + “\n” + 99 + “\n” …..;
try (BufferedWriter writer = Files.newBufferedWriter(path, charset)) {
writer.write(s, 0, s.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
Writing to a File
Instead of doing:
Files.newBufferedWriter(path, charset);
We can do:
Files.newBufferedWriter(path, charset, openOption);
• Where openOption is a static member of StandardOpenOption class
Writing to a File
Open Options:
APPENDIf the file is opened for WRITE access then bytes will be written to the end of the file rather than the beginning.
CREATECreate a new file if it does not exist.
CREATE_NEWCreate a new file, failing if the file already exists.
DELETE_ON_CLOSEDelete on close.
DSYNCRequires that every update to the file's content be written synchronously to the underlying storage device.
READOpen for read access.
SPARSESparse file.
SYNCRequires that every update to the file's content or metadata be written synchronously to the underlying storage device.
TRUNCATE_EXISTINGIf the file already exists and it is opened for WRITE access, then its length is truncated to 0.
WRITEOpen for write access.
Writing to a File
Charset charset = Charset.defaultCharset();
Path path = Paths.get("C:/data/students_score_list.txt");
String s = 100 + “\n” + 99 + “\n” …..;
try (BufferedWriter writer = Files.newBufferedWriter(path, charset, StandardOpenOption.APPEND
)) {
writer.write(s, 0, s.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
File I/O
• There many other ways to do file I/O
• Here we covered some quick ways to read/write files in Java 7 (using the NIO package)
• Extra reading:
http://docs.oracle.com/javase/tutorial/essential/io/index.html
Top Related