File Input and Output
When you need a way to store data even after the program has stopped running you use files. When I use databases, these are just text files. I just don't know the details of how it handles the reading and writing.
Text files are used in contests because it is makes for easy grading and testing of multiple test cases.
Reading Files
There are two main ways to read from files. The Scanner and the BufferedReader. I like using these two methods because they provide methods for reading until end of file. Thus I don't have to worry about reading past the end of file.
For these cases, you need these imports:
When you use all of these methods, you need to either catch IOException or declare main to throw IOException.Code:import java.util.*; import java.io.*;
The IOException is thrown when there is an error with input or output.Code:public static void main(String[] args) throws IOException { }
Scanner
This method is used as a tokenizer to read things token at a time. This method is great when you know what the format of the data is that you are going to be reading. With methods like hasNext and hasNextInt I can check what the type of the next token is. The hasNext method allows me to read until end of file. When I am at the end of the file hasNext will return false and the loop will terminate.
Declaring a Scanner
Looping until end of fileCode:Scanner fin = new Scanner(new FileReader("file.txt"));
This loop runs until you reach the end of the file. In the loop body, you can read and store the values from the file. Alternatively, you can read the values and then process the values. There is not always a need to store values.Code:while (fin.hasNext()) { }
Reading
The fin.next method reads the next token as a String until a delimeter is found. The default delimter is a space. Alternatively, you can choose the delimeter using setDelimeter.Code:String s = fin.next(); int x = fin.nextInt();
There is a read method for pretty much any data type you can think of. The nextLine method reads an entire line.Code:fin.setDelimeter("\\s");
When you are done, don't forget to close the Scanner.
BufferedReaderCode:fin.close();
Alternatively, the BufferedReader always reads the entire line. Then you can split the string into an array at whatever delimeter you specify.
Declaring a Buffered Reader
Reading with this method of input is as simple as:Code:BufferedReader fin = new BufferedReader(new FileInputReader("data.txt"));
To process individual tokens, you can either use a StringTokenizer or split it into an array.Code:String sLine = fin.readLine();
Writing FilesCode:String[] arsWords = sLine.split(" ");
Writing to the file is as simple as using a PrintWriter. Then calling the print or println functions.
Example:
Always make sure, you close your input and output streams. This is just a functional introduction to input and output streams. There is a lot of details that I did not go into here.Code:PrintWriter fout = new PrintWriter(new FileWriter("OUT.txt")); Scanner fin = new Scanner(new FileReader("IN.txt")); String sLine; while (fin.hasNext()) { sLine = fin.next(); fout.println(sLine.toUpperCase()); } fout.close(); fin.close();
Nice tutorial! +rep
I had big problems with Java when I first tried it. I think your "Main method throwing IOException" is the solution to what I experienced.+rep
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
Nice one. +rep
Nice TuT, much appreciated, would rep if I knew how lol.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks