Files Java to C++
C++ uses input and output streams in much the same way that Java does. The key classes for accessing files are ofstream for writting to an output file and ifstream for reading from an input file.

Java

C++

*Something.java[2] means the file "Something.java" at line 2.

Include files

In C++ if we are going to read and write with files we need to include <iostream> at Main.cpp[1] and <fstream> at Main.cpp[2].

Opening a file for output

In Java there are three steps needed to open a file for writing: 1) specifying the file name and output stream, 2) wrapping the output stream with a PrintStream so that the nice methods are available and 3) catching an exception for any errors. In C++ there is only the opening of the output file and an if statement to catch any errors. In Java we specify the file name ("data.txt") by including it in a FileOutputStream constructor (Main.java[8]). In C++ we use the open() method as at Main.cpp[8]. For text files, Java has the additional step of wrapping the output stream myFile in a PrintStream object called myOutputFile as shown at Main.java[9]. The PrintStream class provides the various print() and println() that we use so often. This wrapping is not required in C++ the ofstream class provides all of the << operators needed for writting out text.

If there is an error in opening a file, Java with raise an exception. This must be caught in order to handle the error. This is done with the try at Main.java[7] and the catch at Main.java[15-16]. Java's exception mechanism is more powerful but for cases like this the C++ error mechanism is simpler. An ofstream value can be used as a boolean in an if statement. If there has been an error it will return false. Thus the try/catch in Java is replaced by an if statement as shown at Main.cpp[9, 15-16].

Writing to a file

In Java we use print() and println() to write to an output PrintStream as shown at Main.java[10-12]. In C++ we use the << operator just as with cout to write information to an output stream. This is shown at Main.cpp[10-12];

Closing a file

It is very important to close an output file when we are done to ensure that all characters have been written to the file and not stuck in a buffer. This is the same in Java at Main.java[13] as it is in C++ at Main.cpp[13].

Opening a file for input

Each language works similarly on input as it does on output. In Java we specify the file name in a FileInputStream constructor, shown at Main.java[19]. We then wrap it in a Scanner as shown at Main.java[20]. In C++ we use an ifstream for an input file and specify the file name using the open() method as shown at Main.cpp[18-19]. Java uses a try/catch for its file errors (Main.java[17,31-32]). C++ uses the if statements in the same fashion as output files (Main.cpp[23,32-33]).

Checking for end of file

In Java we check for the end of a Scanner stream using the method hasNext() as shown at Main.java[24]. In C++ this function is performed by the >> operator which returns false if it fails to read an input, as shown at Main.cpp[25]. Reading actual values in Java is performed using the Scanner's various next----() methods as at Main.java[25-26]. In C++ we use the >> operator as shown at Main.cpp[25-26].