When you process a file, there are a couple of new things you need to incorporate into the code:
Import the library that handles the input processing: import java.io.*
Handle the potential errors in case the file is not found: throws IOException
Select the mechanism to handle the file:
Reading a file: Scanner(new File(“nameoffile.txt”));
Writing a file: PrintWriter(new File(“nameOfFile.txt”));
User interaction eventually becomes more "effective" if data is provided through files or different forms of storage (e.g., cloud files, links to websites, database queries). Most of the data that computer programmers will manage are stored in data files such as text files (.txt), Microsoft files (docx, xlsx), spreadsheets, and others.
Reading files in Java requires to use of an additional library to handle input/output processes.
import java.io.*
it also requires a mechanism to handle potential problems (i.e., exceptions) that might happen to files that are an attempt to read or write. For example, if the file is not there, or the file is there but is corrupted, or someone else is using it. There are several potential exceptions that might occur while reading or writing a file. That is why it is important to use the following line of code just after the main declaration:
throws IOException
This way in case something "bad" happens, it can be handled by java properly.
The following piece of code ReadFileExample.java demonstrates a simple program that reads a file named "test.txt" that is in the same working directory as ReadFileExample.java. The program will read the content (line by line) of the file and print each line on the screen.
import java.util.Scanner;
import java.io.*;
public class ReadFileExample{
public static void main(String [] args)throws IOException{
Scanner input = new Scanner(new File("test.txt"));
while(input.hasNext()){
String line = input.nextLine();
System.out.println(line);
}
}
}
*Make sure your file is in the same directory as your program
import java.util.Scanner;
import java.io.*;
public class WriteFileExample{
public static void main(String [] args)throws IOException{
PrintWriter pr = new PrintWriter("output.txt");
pr.println("this is a TEST!");
pr.close();
}
}
Task: Write a program that reads a file called “poetry.txt” and then counts the total number of vowels in the file. The program shall report back into a file called “output.txt” the total number of vowels found in the file poetry.txt as follows:
Total ‘a’ in file: #
Total ‘e’ in file: #
Total ‘i’ in file: #
Total ‘o’ in file: #
Total ‘u’ in file: #
How to generate a random number
Make sure your file is in the same directory as your program
(int)(Math.random()*11)
> e.g., 8
AP CS A
[while Loop]
CON-2.H Compute statement execution counts and informal run-time comparison of iterative statements.
CON-2.H.1 A statement execution count indicates the number of times a statement is executed by the program.
[do-while Loop]
CON-2.C.1 Iteration statements change the flow of control by repeating a set of statements zero or more times until a condition is met.
[Random Numbers]
CON-1.D.4 The values returned from Math.random can be manipulated to produce a random int or double in a defined range
Explain the importance of algorithms in the problem-solving process.
Demonstrate how a problem may be solved by multiple algorithms, each with different properties.
Read a given program, and explain what it does
Trace the flow of control during the execution of a program (both correct and incorrect).
Use appropriate terminology to identity elements of a program (e.g., identifier, operator, operand)
Reading and explaining code
Basic concepts such as variables, primitive data types, expression evaluation, assignment, etc.
Input and output using files, console, and APIs
Design, write, test, and debug a program that uses each of the following fundamental programming constructs: assignment and expressions, simple I/O, conditional and iterative structures, functions with parameter passing.
Write a program that uses file I/O to provide persistence across multiple executions.
Write a program that creates simple classes and instantiates objects of those classes (if supported by the language)
Use appropriate terminology to identity elements of a program (e.g., identifier, operator, operand)