4.4 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
File Handling
133
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<=fixedSize)
file.writeChars (text) ;
for (int i=size; i‹fixedsize; it+)
file.writeChar("
else
file.writeChars(text. substring l
0, fixedsize)):
public static String readstring(
RandomAccessFile file, int fixedSize)
throws IOException
String value
for (int 1=0; i‹fixedsize;
i++)
value+=file.readchar () ;
return value;
}
}
4.4 Using a text editor or word processor, create a text file containing a series of five surnames and examination marks
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
4.4 Using a text editor or word processor, create a text file containing a series of five surnames and examination marks
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!