tailieunhanh - Lecture Fundamentals of computing 1: Lecture 13 - Duy Tan University
Lecture Fundamentals of computing 1: Lecture 13 introduce file processing. This chapter presents the following content: Class file concept, some methods of class file, reading files with scanner, file paths, compiler error w/ files,.and another contents. Inviting you refer. | Lecture Title: File processing Fundamentals of Computing 1 Agenda File input File output ask them, how might the computer store "hi" using binary digits? (some kind of mapping; ASCII) Class File File class is in the package . To use class File, we have to import as follow: import .*; Create a File object to get info about a file on your drive. (This doesn't actually create a new file on the hard disk.) File f = new File(""); if (() && () > 1000) { (); } Some methods of class File Method name Description canRead() returns whether file is able to be read delete() removes file from disk exists() whether this file exists on disk getName() returns file's name length() returns number of bytes in file renameTo(file) changes name of file Reading files with Scanner To read a file, pass a File when constructing a Scanner. Syntax: Scanner name = new Scanner(new File("file name")); Example: File file = new File(""); Scanner input = new Scanner(file); or (shorter): Scanner input = new Scanner(new File("")); It is nice that Java uses the same object to read files as it does to read the keyboard. It's simpler and easier to learn. Some languages (C, Python, etc.) don't do this. File paths absolute path: specifies a drive or a top "/" folder C:/Documents/smith/hw6/input/ Windows can also use backslashes to separate folders. relative path: does not specify any top-level folder input/ Assumed to be relative to the current directory: Scanner input = new Scanner(new File("data/")); If our program is in H:/hw6 , Scanner will look for H:/hw6/data/ Compiler error w/ files import .*; // for File import .*; // for Scanner public class ReadFile { public static void main(String[] args) { Scanner input = new Scanner(new File("")); String text = (); (text); } } The program fails to compile with the . | Lecture Title: File processing Fundamentals of Computing 1 Agenda File input File output ask them, how might the computer store "hi" using binary digits? (some kind of mapping; ASCII) Class File File class is in the package . To use class File, we have to import as follow: import .*; Create a File object to get info about a file on your drive. (This doesn't actually create a new file on the hard disk.) File f = new File(""); if (() && () > 1000) { (); } Some methods of class File Method name Description canRead() returns whether file is able to be read delete() removes file from disk exists() whether this file exists on disk getName() returns file's name length() returns number of bytes in file renameTo(file) changes name of file Reading files with Scanner To read a file, pass a File when constructing a Scanner. Syntax: Scanner name = new Scanner(new File("file name")); Example: File file = new File(""); Scanner .
đang nạp các trang xem trước