public class Test {
public static void main(String[] args) throws IOException {
File output = new File("N:/output.txt");
FileWriter out = new FileWriter(output);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("N:/Printer.txt"))));
String line = "";
int tokenid = 0;
int id = 0;
String date = "";
String username = "";
String printer = "";
String document = "";
String pages = "";
PrintingRecord myrecord = new PrintingRecord(id, date, username, printer, document, pages);
myrecord.id = 0;
while((line= reader.readLine()) != null) { //Keep on reading in lines until EOF
System.out.println(line); //reading in all lines ok
StringTokenizer st = new StringTokenizer(line, ",;:");
String token;
tokenid = 0;
myrecord.id++;
while(st.hasMoreTokens()){ //while there are still tokens to read
out.flush();
token = st.nextToken();
out.write(token); //output token to file
out.write('\n');
//assign variables to tokens
if (tokenid == 0){
myrecord.date= token;
}
if (tokenid == 8){
myrecord.username = token;
}
if (tokenid == 11){
myrecord.printer = token;
}
if (tokenid == 13){
myrecord.pages = token;
}
tokenid++;
}
System.out.println("Date " + myrecord.date + "Token ID is " + tokenid);
System.out.println("Username " + myrecord.username + "Token ID is " + tokenid);
System.out.println("Printer " + myrecord.printer + "Token ID is " + tokenid);
System.out.println("RecordID is " + myrecord.id);
HashMap RecordMap = new HashMap();
MyDataHolder mdh = new MyDataHolder();
mdh.setIntVal(myrecord.id);
mdh.setStrVal(myrecord.username);
String myrecordidasString = myrecord.id + "";
RecordMap.put(myrecordidasString, mdh);
// and to retrieve
MyDataHolder mdh2 = (MyDataHolder)RecordMap.get("1");
System.err.println(mdh2.getIntVal() +" " + mdh2.getStrVal());
}
out.close();
}
public static class PrintingRecord {
public PrintingRecord(int id_, String date_, String username_, String printer_, String document_, String pages_){
id = id_;
username = username_;
printer = printer_;
document = document_;
pages = pages_;
}
public int id =0;
public String date;
public String username;
public String printer;
public String document;
public String pages;
}
public static class MyDataHolder {
private int intVal = 0;
private String strVal = "";
public void setIntVal(int intVal) {
this.intVal = intVal;
}
public int getIntVal() {
return intVal;
}
public void setStrVal(String strVal) {
this.strVal = strVal;
}
public String getStrVal() {
return strVal;
}
}
}