java解析dbf之經由過程javadbf包生成和讀取dbf文件。本站提示廣大學習愛好者:(java解析dbf之經由過程javadbf包生成和讀取dbf文件)文章只能為提供參考,不一定能成為您想要的結果。以下是java解析dbf之經由過程javadbf包生成和讀取dbf文件正文
靜態計劃處理矩陣連乘成績,隨機發生矩陣序列,輸入形如((A1(A2A3))(A4A5))的成果。
代碼:
#encoding: utf-8
=begin
author: xu jin, 4100213
date: Oct 28, 2012
MatrixChain
to find an optimum order by using MatrixChain algorithm
example output:
The given array is:[30, 35, 15, 5, 10, 20, 25]
The optimum order is:((A1(A2A3))((A4A5)A6))
The total number of multiplications is: 15125
The random array is:[5, 8, 8, 2, 5, 9]
The optimum order is:((A1(A2A3))(A4A5))
The total number of multiplications is: 388
=end
INFINTIY = 1 / 0.0
p = [30, 35, 15, 5, 10, 20, 25]
m, s = Array.new(p.size){Array.new(p.size)}, Array.new(p.size){Array.new(p.size)}
def matrix_chain_order(p, m, s)
n = p.size - 1
(1..n).each{|i| m[i][i] = 0}
for r in (2..n) do
for i in (1..n - r + 1) do
j = r + i - 1
m[i][j] = INFINTIY
for k in (i...j) do
q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]
m[i][j], s[i][j] = q, k if(q < m[i][j])
end
end
end
end
def print_optimal_parens(s, i, j)
if(i == j) then
print "A" + i.to_s
else
print "("
print_optimal_parens(s, i, s[i][j])
print_optimal_parens(s, s[i][j] + 1, j)
print ")"
end
end
def process(p, m, s)
matrix_chain_order(p, m, s)
print "The optimum order is:"
print_optimal_parens(s, 1, p.size - 1)
printf("\nThe total number of multiplications is: %d\n\n", m[1][p.size - 1])
end
puts "The given array is:" + p.to_s
process(p, m, s)
#produce a random array
p = Array.new
x = rand(10)
(0..x).each{|index| p[index] = rand(10) + 1}
puts "The random array is:" + p.to_s
m, s = Array.new(p.size){Array.new(p.size)}, Array.new(p.size){Array.new(p.size)}
process(p, m, s)
nbsp; {
DBFField field = reader.getField(i);
System.out.println(field.getName());
}
Object[] rowValues;
//一條條掏出path文件中記載
while((rowValues = reader.nextRecord()) != null)
{
for( int i=0; i<rowValues.length; i++)
{
System.out.println(rowValues[i]);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{
fis.close();
}catch(Exception e){}
}
}
public static void writeDBF(String path)
{
OutputStream fos = null;
try
{
//界說DBF文件字段
DBFField[] fields = new DBFField[3];
//分離界說各個字段信息,setFieldName和setName感化雷同,
//只是setFieldName曾經不建議應用
fields[0] = new DBFField();
//fields[0].setFieldName("emp_code");
fields[0].setName("semp_code");
fields[0].setDataType(DBFField.FIELD_TYPE_C);
fields[0].setFieldLength(10);
fields[1] = new DBFField();
//fields[1].setFieldName("emp_name");
fields[1].setName("emp_name");
fields[1].setDataType(DBFField.FIELD_TYPE_C);
fields[1].setFieldLength(20);
fields[2] = new DBFField();
//fields[2].setFieldName("salary");
fields[2].setName("salary");
fields[2].setDataType(DBFField.FIELD_TYPE_N);
fields[2].setFieldLength(12);
fields[2].setDecimalCount(2);
//DBFWriter writer = new DBFWriter(new File(path));
//界說DBFWriter實例用來寫DBF文件
DBFWriter writer = new DBFWriter();
//把字段信息寫入DBFWriter實例,即界說表構造
writer.setFields(fields);
//一條條的寫入記載
Object[] rowData = new Object[3];
rowData[0] = "1000";
rowData[1] = "John";
rowData[2] = new Double(5000.00);
writer.addRecord(rowData);
rowData = new Object[3];
rowData[0] = "1001";
rowData[1] = "Lalit";
rowData[2] = new Double(3400.00);
writer.addRecord(rowData);
rowData = new Object[3];
rowData[0] = "1002";
rowData[1] = "Rohit";
rowData[2] = new Double(7350.00);
writer.addRecord(rowData);
//界說輸入流,並聯系關系的一個文件
fos = new FileOutputStream(path);
//寫入數據
writer.write(fos);
//writer.write();
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{
fos.close();
}catch(Exception e){}
}
}
public static void main(String[] args){
String path ="E:\\tmp\\2\\xx.dbf";
try {
InputStream fis = new FileInputStream(path);
DBFReader reader = new DBFReader(fis);
int fieldsCount = reader.getFieldCount();
System.out.println("字段數:"+fieldsCount);
DBFField[] df = new DBFField[fieldsCount+2];
for( int i=0; i<fieldsCount; i++)
{
df[i] = reader.getField(i);
System.out.println("field"+i+":"+df[i].getName());
}
df[fieldsCount] = new DBFField();
df[fieldsCount].setName("add1");
df[fieldsCount].setDataType(DBFField.FIELD_TYPE_C);
df[fieldsCount].setFieldLength(10);
df[fieldsCount+1] = new DBFField();
df[fieldsCount+1].setName("add2");
df[fieldsCount+1].setDataType(DBFField.FIELD_TYPE_C);
df[fieldsCount+1].setFieldLength(10);
DBFWriter writer = new DBFWriter();
writer.setFields(df);
Object[] rowValues;
Object[] rowValues1 = new Object[fieldsCount+2];
//一條條掏出path文件中記載
while((rowValues = reader.nextRecord()) != null)
{
for(int i=0;i<fieldsCount;i++){
rowValues1[i] = rowValues[i];
}
rowValues1[fieldsCount]="x";
rowValues1[fieldsCount+1]="xx";
writer.addRecord(rowValues1);
}
path ="E:\\tmp\\2\\test2.dbf";
OutputStream fos = new FileOutputStream(path);
//寫入數據
writer.write(fos);
System.out.println("OVER");
} catch (FileNotFoundException | DBFException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}