0
点赞
收藏
分享

微信扫一扫

java maven 读取 dbf 文件 生成 表结构 示例代码


介绍

      通过读取一个文件夹下所有dbf文件 然后生成sql

 

依赖

<dependency>
<groupId>com.github.albfernandez</groupId>
<artifactId>javadbf</artifactId>
<version>1.9.4</version>
</dependency>

 

 

代码

package com.superman.uitl;

import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* 读取dbf文件 生成 创建表的sql
*
* @author yushen
*
*/
public class JavaDBFReader {

public static void main(String args[]) {
String path = "E:\\xxx";
getFile(path);

}

public static void dbfr(String url,String table) {
System.out.println(" CREATE TABLE "+table.substring(0,table.length()-4)+" ( ");
try {
InputStream inputStream = new FileInputStream(new File(url));
DBFReader reader = new DBFReader(inputStream);
int numberOfFields = reader.getFieldCount();
String sz = "";
for (int i = 0; i < numberOfFields; i++) {
DBFField field = reader.getField(i);
if(i==0) {
System.out.println(" "+field.getName()+" varchar("+field.getLength() +") ");
}else {
System.out.println(" "+","+field.getName()+" varchar("+field.getLength() +") ");
}
// System.out.println(field.getName() + "___" + field.getLength() + "___" + field.getType());
}
System.out.println();
Object[] rowObjects;
// while ((rowObjects = reader.nextRecord()) != null) {
// for (int i = 0; i < rowObjects.length; i++) {
// System.out.println(rowObjects[i]);
// }
// }
inputStream.close();
System.out.println(" ); ");
} catch (DBFException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}

private static void getFile(String path) {
// get file list where the path has
File file = new File(path);
// get the folder list
File[] array = file.listFiles();

for (int i = 0; i < array.length; i++) {
if (array[i].isFile()) {
// System.out.println(array[i].getPath() + array[i].getName());
dbfr(array[i].getPath(),array[i].getName());
// System.out.println(array[i].getPath());
} else if (array[i].isDirectory()) {
getFile(array[i].getPath());
}
}
}

}

 

日志

 CREATE TABLE yhcs ( 
DAIHAO varchar(12)
,KOULING varchar(80)
,JIBIE varchar(1)
,LEIBIE varchar(10)
,MINGCHENG varchar(40)

);


 

ok


 

持续更新

 

 

 

 

 

举报

相关推荐

0 条评论