CreateData02.java
Input file:
popdata.txt
Select all
/* CreateData02.java David Klick 2005-01-26 Create population binary data file for assignment 2 from text data file. */ import java.io.*; import java.util.StringTokenizer; class CreateData02 { public static void main(String[] args) throws IOException { String filename = "popdata"; // open input file (plain text, field separator = ' ') BufferedReader ifile = new BufferedReader( new FileReader(filename + ".txt")); // open output file DataOutputStream ofile = new DataOutputStream( new FileOutputStream(filename + ".dat")); String linein = ifile.readLine(); while (linein != null) { // readLine returns null at end of file // each record is a country name followed by 16 ints byte[] bytCountry; int[] pop = new int[16]; // grab country name, replace '_' chars with ' ' // (spaces within names were replaced with '_' since // spaces are being used as field separators) StringTokenizer tok = new StringTokenizer(linein); bytCountry = tok.nextToken().replace('_', ' ').getBytes(); // grab 16 int fields // should add error-checking, but this will only be run once for (int i=0; i