In this blog post, you will learn how to read and write files - a very useful skill for processing real world data.
Reading and Writing Text files
Examples of text files include not only files that are created with text editor, such as windows notepad, but also java source files, HTML files, etc.
In Java, the most convenient mechanism for reading text is to use the Scanner class.
To begin, construct a File object with the name of the input file.
File inputFile = new File("input.txt");
Then use the File object to construct a Scanner object.
Scanner in = new Scanner(inputFile);
This Scanner object reads text from the file input.txt. You can use the Scanner methods (nextInt, nextDouble, next, nextLine) to read data from the input file.
To write output to a file, you construct a PrintWriter object with the desired file name.
PrintWriter out = new PrintWriter("output.txt");
out.println("Hello, World!");
out.printf("Total: %8.2f\n", total);
If the output file already exists, the file is emptied before the new data are written into. If the file doesn't exit, an empty file is created.
When you are done processing a file, be sure to close the Scanner and PrintWriter objects.
in.close();
out.close();
When you specify a file name as a string literal, and the name contains backslash characters (as in a Windows file name), you must supply each backslash twice:
You can read the contents of a web page with this sequence of commands:
String address = "http://horstmann.com/index.html";
URL pageLocation = new URL(address);
Reading and Writing Text files
Examples of text files include not only files that are created with text editor, such as windows notepad, but also java source files, HTML files, etc.
In Java, the most convenient mechanism for reading text is to use the Scanner class.
To begin, construct a File object with the name of the input file.
File inputFile = new File("input.txt");
Then use the File object to construct a Scanner object.
Scanner in = new Scanner(inputFile);
This Scanner object reads text from the file input.txt. You can use the Scanner methods (nextInt, nextDouble, next, nextLine) to read data from the input file.
To write output to a file, you construct a PrintWriter object with the desired file name.
PrintWriter out = new PrintWriter("output.txt");
out.println("Hello, World!");
out.printf("Total: %8.2f\n", total);
If the output file already exists, the file is emptied before the new data are written into. If the file doesn't exit, an empty file is created.
When you are done processing a file, be sure to close the Scanner and PrintWriter objects.
in.close();
out.close();
When you specify a file name as a string literal, and the name contains backslash characters (as in a Windows file name), you must supply each backslash twice:
File inputFile = new File("c:\\homework\\input.dat");Reading Web Pages
You can read the contents of a web page with this sequence of commands:
URL pageLocation = new URL(address);
Scanner in = new Scanner(pageLocation.openStream());
Note: The URL class is contained in the java.net package.
Text Input and Output
Reading Words
The next() method of Scanner class reads the next string that is delimited by white space.
White space includes spaces, tab characters, and the new line characters that separate lines.
The words returned by next() method can contain punctuation marks and other symbols.
Sometimes, you want to read just the words and discard anything that isn’t a letter. You achieve this task by calling the useDelimiter method on your Scanner object:
Scanner in = new Scanner(. . .);
in.useDelimiter("[^A-Za-z]+");
Reading characters
Sometimes, you want to read a file one character at a time. You achieve this task by calling the useDelimiter method on your Scanner object with an empty string:
Scanner in = new Scanner(. . .);
in.useDelimiter("");
char ch = in.next().charAt(0);
Classifying Characters
The Character class declares several methods for this purpose.
Character.isDigit(ch); // returns true if ch is a digit '0....9'
isDigit
isLetter
isUpperCase
isLowerCase
isWhiteSpace - space, new line, tab
Converting Strings to Numbers
If a String contains the digits of a number, you use the Integer.parseInt or Double.parseDouble method to obtain the number value.
String population = "123223432";
int populationValue = Integer.parseInt(population);
The argument must be a string containing the digits of an integer, without any additional characters. Not even spaces are allowed.
In case, if there are any spaces at the end of string, use the trim() method.
Note: The URL class is contained in the java.net package.
Text Input and Output
Reading Words
The next() method of Scanner class reads the next string that is delimited by white space.
White space includes spaces, tab characters, and the new line characters that separate lines.
The words returned by next() method can contain punctuation marks and other symbols.
Sometimes, you want to read just the words and discard anything that isn’t a letter. You achieve this task by calling the useDelimiter method on your Scanner object:
Scanner in = new Scanner(. . .);
in.useDelimiter("[^A-Za-z]+");
Reading characters
Sometimes, you want to read a file one character at a time. You achieve this task by calling the useDelimiter method on your Scanner object with an empty string:
Scanner in = new Scanner(. . .);
in.useDelimiter("");
char ch = in.next().charAt(0);
Classifying Characters
The Character class declares several methods for this purpose.
Character.isDigit(ch); // returns true if ch is a digit '0....9'
isDigit
isLetter
isUpperCase
isLowerCase
isWhiteSpace - space, new line, tab
Converting Strings to Numbers
If a String contains the digits of a number, you use the Integer.parseInt or Double.parseDouble method to obtain the number value.
String population = "123223432";
int populationValue = Integer.parseInt(population);
The argument must be a string containing the digits of an integer, without any additional characters. Not even spaces are allowed.
In case, if there are any spaces at the end of string, use the trim() method.
int populationValue = Integer.parseInt(population.trim());
No comments:
Post a Comment