File Handling
4.10 Vectors Versus ArrayLists
Exercises
4.1 Using a text editor or wordprocessor, create a text fi le holding a series of sur- names and payroll numbers (at least fi ve of each). For example:
Smith Jones Jenkins 123456 987654 555555
……
Now write a Java program that uses a while loop to read values from the above text fi le and displays then in a table under the headings ‘Surname’ and ‘Payroll No.’. (Don’t be too concerned about precise alignment of the columns.) 4.2 Take a copy of the above program, rename the class and modify the appropriate
line so that the program accepts input from the standard input stream (i.e., using a Scanner around the standard input stream, System.in ). Then use redirection to feed the values from your payroll text fi le into your program (displaying the contents as before).
4.3 Write a (very short) program that creates a serial text fi le holding just two or three names of your own choosing. After compiling and running the program, use the MS-DOS command type (or the equivalent command for your plat- form) to display the fi le’s contents. For example:
type names.txt
4.4 Using a text editor or word processor, create a text fi le containing a series of fi ve surnames and examination marks, each item on a separate line. For example:
Smith Jones 47
…63
…
By extending the code given below, create a random access fi le called results.
dat , accepting input from the standard input stream (via a Scanner object) and redirecting input from the above text fi le. Each record should comprise a student surname and examination mark. When all records have been written, reposition the fi le pointer to the start of the fi le and then read each record in turn, displaying its contents on the screen.
import java.io.*;
import java.util.*;
public class FileResults {
private static fi nal long REC_SIZE = 34;
private static fi nal int SURNAME_SIZE = 15;
private static String surname;
private static int mark;
public static void main(String[] args)
throws IOException {
/*********************************************
*** SUPPLY CODE FOR main! ***
*********************************************/
}
public static void writeString(
RandomAccessFile fi le, String text, int fi xedSize) throws IOException {
int size = text.length();
if (size<=fi xedSize) {
fi le.writeChars(text);
for (int i=size; i<fi xedSize; i++) fi le.writeChar(' ');
} else
fi le.writeChars(text.substring(
0,fi xedSize));
}
public static String readString(
RandomAccessFile fi le, int fi xedSize) throws IOException {
String value = "";
for (int i=0; i<fi xedSize; i++) value+=fi le.readChar();
return value;
} }
4.5 Making use of class Results shown below, repeat the above program, this time writing/reading objects of class Result . (When displaying the names and marks that have been read, of course, you must make use of the methods of class Result .) Once again, redirect initial input to come from your text fi le.
class Result implements java.io.Serializable {
private String surname;
private int mark;
public Result(String name, int score) {
surname = name;
mark = score;
}
public String getName() {
return surname;
}
public void setName(String name) {
surname = name;
}
public int getMark() {
return mark;
}
public void setMark(int score) {
if ((score>=0) && (score<=100)) mark = score;
} }
4.6 Using class Personnel from Sect. 4.9 , create a simple GUI-based program called ChooseSaveFile.java that has no components, but creates an instance of itself within main and has the usual window-closing code (also within main ).
Within the constructor for the class, declare and initialise an array of three Personnel objects (as in program ArrayListSerialise.java from Sect. 4.9 ) and write the objects from the array to a fi le (using an ObjectOutputStream ). The name and location of the fi le should be chosen by the user via a JFileChooser object. Note that you will need to close down the (empty) application window by clicking on the window close box.
4.7 Take a copy of the above program, rename it ReadFile.java and modify the code to make use of a JFileChooser object that allows a fi le to be selected for reading.
Use the JFileChooser object to read from the fi le created above and get your program to use method getSurname of class Personnel to display the surnames of all the staff whose details were saved.
129 J. Graba, An Introduction to Network Programming with Java: Java 7 Compatible,
DOI 10.1007/978-1-4471-5254-5_5, © Springer-Verlag London 2013
Learning Objectives
After reading this chapter, you should:
• understand the fundamental purpose of RMI;
• understand how RMI works;
• be able to implement an RMI client/server application involving .class fi les that are available locally;
• appreciate the potential danger presented by .class fi les downloaded from remote locations;
• know how security managers may be used to overcome the above danger.
With all our method calls so far, the objects upon which such methods have been invoked have been local . However, in a distributed environment, it is often desirable to be able to invoke methods on remote objects (i.e., on objects located on other systems). RMI ( Remote Method Invocation ) provides a platform-independent means of doing just this.
Under RMI, the networking details required by explicit programming of streams and sockets disappear and the fact that an object is located remotely is almost transparent to the Java programmer. Once a reference to the remote object has been obtained, the methods of that object may be invoked in exactly the same way as those of local objects.
Behind the scenes, of course, RMI will be making use of byte streams to transfer data and method invocations, but all of this is handled automatically by the RMI infrastructure.
RMI has been a core component of Java from the earliest release of the language, but has undergone some evolutionary changes since its original specifi cation.