1.Write a (very short) program that creates a serial text file 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 platform) to display the file's contents. For example:
type names.txt
2. Using a text editor or word processor, create a text file containing a series of
five surnames and examination marks, each item on a separate line. For
example:
Smith
47
Jones
63
…
…
By extending the code given below, create a random access file called
results.dat, accepting input from the standard input stream (via a Scanner
object) and redirecting input from the above text file. Each record should
comprise a student surname and examination mark. When all records have been written, reposition the file pointer to the start of the file and then read each record in turn, displaying its contents on the screen.
import java.io.*;
import java.util.*;
public class FileResults
{
private static final long REC_SIZE = 34;
private static final 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 file, String text,
int fixedSize) throws IOException
{
int size = text.length();
if (size<>
{
file.writeChars(text);
for (int i=size; i
file.writeChar(' ');
}
else
file.writeChars(text.substring(
0,fixedSize));
}
public static String readString(
RandomAccessFile file, int fixedSize)
throws IOException
{
String value = “”;
for (int i=0; i
value+=file.readChar();
return value;
}
}