📄 datahandler.java
字号:
package datamining;
import java.io.*;
/**
* A class for handling a database for datamining purposes.
* Is used for getting data from the database.
*/
public class DataHandler {
String filename; // the filename of the database
BufferedReader reader; // the buffer to read the input to
int samplesize;
int partsize; // the size of a part when using Partition
int counter;
boolean locked;
/**
* The default constructor for creating a DataHandler object.
*/
public DataHandler() {
}
/**
* Constructor for creating a DataHandler object with parameters.
*
* @param filename the name of the database file
*/
public DataHandler(String filename) {
this.filename = filename;
this.samplesize = 1;
this.partsize = -1;
this.counter = 0;
this.locked = false;
}
/**
* Method for opening the connection to the database. Checks
* whether it is allowed to (re)open the connection. Checks also
* if it should reopen a certain part of the database (partition algorithm).
*/
public void open() {
if (!locked) {
try {
this.reader = new BufferedReader(new FileReader(filename));
reader.mark(1000*partsize);
} catch (Exception e) {}
} else if (partsize > 0) {
try {
reader.reset();
} catch (Exception e) {
System.out.println("could not reset to mark");
}
counter = 0;
locked = false;
} else {
locked = false;
}
}
/**
* Method for reading a transaction from the database.
* It returns the transaction as an array of integers.
*
* @return the transaction as an array of integers.
* an array of length 0 is returned if there was no
* transaction or the part was read to the end.
*/
public int[] read() {
int[] items = new int[0];
if (!locked) {
try {
for(int i = this.samplesize; i > 1; i--) {
System.out.println("skipping sample");
reader.readLine();
}
items = toIntArray(reader.readLine().trim().split(" "));
counter++;
} catch (Exception e) {
// eof
items = new int[0];
if (counter < partsize)
locked = true;
}
if (counter == partsize) {
locked = true;
}
}
return items;
}
/**
* Sets now manieth transaction is given from the database.
*/
public void setSample(int size) {
this.samplesize = size;
}
/**
* Sets the size of a part.
*/
public void setPartsize(int size) {
this.partsize = size;
}
/**
* Advances the mark in the database to the current point, which is
* the new beginning of the new part.
*/
public void nextPart() {
try {
reader.mark(1000*partsize);
counter = 0;
} catch (Exception e) {
System.out.println("mark could not be set");
}
}
/**
* Converts a String array to integer array.
*
* @param str the array of Strings to be converted
* @return the array of integers
*/
private int[] toIntArray(String[] str) {
int[] intArray = new int[str.length];
for(int i=0; i < str.length; i++) {
intArray[i] = new Integer(str[i].trim()).intValue();
}
return intArray;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -