- import java.io.File;
- import java.util.Scanner;
- import java.util.Formatter;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- public class Munge
- {
- private String inFileName, outFileName;
- private Scanner inFile;
- private Formatter outFile;
- private int line = 0;
- private String[] data = new String[100];
- public Munge(String inFileName, String outFileName)
- {
- this.inFileName = inFileName;
- this.outFileName = outFileName;
- }
- public void openFiles()
- {
- try
- {
- inFile = new Scanner(new File(inFileName));
- }
- catch(FileNotFoundException exception)
- {
- System.err.println("File not found.");
- System.exit(1);
- }
- catch(SecurityException exception)
- {
- System.err.println("You do not have access to this file.");
- System.exit(1);
- }
- try
- {
- outFile = new Formatter(outFileName);
- }
- catch(FileNotFoundException exception)
- {
- System.err.println("File not found.");
- System.exit(1);
- }
- catch(SecurityException exception)
- {
- System.err.println("You do not have access to this file.");
- System.exit(1);
- }
- }
- public void readRecords()
- {
- while(inFile.hasNext())
- {
- data[line] = inFile.nextLine();
- System.out.println(data[line]);
- line++;
- }
- }
- public void writeRecords()
- {
- for(int i = 0; i < line; i++)
- {
- String tokens[] = data[i].split(", ");
- String city = tokens[i];
- String province = tokens[i + 1];
- ArrayList<ArrayList<String>> provinces = new ArrayList<ArrayList<String>>();
- if(!provinces.contains(province))
- provinces.add(province);
- provinces(indexOf(province)).add(new ArrayList(cities));
- }
- }
- public void closeFiles()
- {
- if(inFile != null)
- inFile.close();
- if(outFile != null)
- outFile.close();
- }
- }