前几天看的逐步回归算法:
https://blog.csdn.net/lindayu0408/article/details/51172996
它写的非常详细,只是少个类,于是我自己瞎编编,把那个类给补上去
- 源代码:(CsvUtil类)
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackInputStream;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.ArrayList;
//规定:表格没有行头标和列头标。如果有需要修改参数
public class CsvUtil {
private BufferedReader br = null;
ArrayList<String> list = new ArrayList<String>();
private int maxLen =0;
public CsvUtil(String filename) throws IOException{
br = new BufferedReader(new UnicodeReader(new FileInputStream(filename),Charset.defaultCharset().name()));
String line = "";
while((line = br.readLine())!=null){
list.add(line);
if(line.split(",").length>maxLen)
maxLen= line.split(",").length;
}
}
//获取行数:
public int getRowNum(){
return list.size()+1;
}
//获取列数:
public int getColNum(){
return maxLen+1;
}
//获取矩阵中某个数值:参数row,col
public String getString(int row, int col) { //row 合法是 0~rowNum之间。
row--;
col--;
String temp = "";
int colNum = getColNum();
int rowNum = getRowNum();
if(row>rowNum-1 || col >colNum|| row<0 ||col<0) {
return null;
}else if(colNum == 1){
return list.get(row).toString();
}else if(colNum >1){
temp = list.get(row).toString().split(",")[col];
}else{
return null;
}
return temp;
}
}
class UnicodeReader extends Reader {
PushbackInputStream internalIn;
InputStreamReader internalIn2 = null;
String defaultEnc;
private static final int BOM_SIZE = 4;
UnicodeReader(InputStream in, String defaultEnc) {
internalIn = new PushbackInputStream(in, BOM_SIZE);
this.defaultEnc = defaultEnc;
}
public String getDefaultEncoding() {
return defaultEnc;
}
public String getEncoding() {
if (internalIn2 == null) return null;
return internalIn2.getEncoding();
}
protected void init() throws IOException {
if (internalIn2 != null) return;
String encoding;
byte bom[] = new byte[BOM_SIZE];
int n, unread;
n = internalIn.read(bom, 0, bom.length);
if ( (bom[0] == (byte)0x00) && (bom[1] == (byte)0x00) &&
(bom[2] == (byte)0xFE) && (bom[3] == (byte)0xFF) ) {
encoding = "UTF-32BE";
unread = n - 4;
} else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) &&
(bom[2] == (byte)0x00) && (bom[3] == (byte)0x00) ) {
encoding = "UTF-32LE";
unread = n - 4;
} else if ( (bom[0] == (byte)0xEF) && (bom[1] == (byte)0xBB) &&
(bom[2] == (byte)0xBF) ) {
encoding = "UTF-8";
unread = n - 3;
} else if ( (bom[0] == (byte)0xFE) && (bom[1] == (byte)0xFF) ) {
encoding = "UTF-16BE";
unread = n - 2;
} else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) ) {
encoding = "UTF-16LE";
unread = n - 2;
} else {
// Unicode BOM mark not found, unread all bytes
encoding = defaultEnc;
unread = n;
}
//System.out.println("read=" + n + ", unread=" + unread);
if (unread > 0) internalIn.unread(bom, (n - unread), unread);
// Use given encoding
if (encoding == null) {
internalIn2 = new InputStreamReader(internalIn);
} else {
internalIn2 = new InputStreamReader(internalIn, encoding);
}
}
public void close() throws IOException {
init();
internalIn2.close();
}
public int read(char[] cbuf, int off, int len) throws IOException {
init();
return internalIn2.read(cbuf, off, len);
}
}
还好,运行结果跟博客上一样!
- 测试数据:
7 26 6 60 78.5
1 29 15 52 74.3
11 56 8 20 104.3
11 31 8 47 87.6
7 52 6 33 95.9
11 55 9 22 109.2
3 71 17 6 102.7
1 31 22 44 72.5
2 54 18 22 93.1
21 47 4 26 115.9
1 40 23 34 83.8
11 66 9 12 113.3
10 68 8 12 109.4
- 运行结果:
妥妥滴!