当前位置:网站首页>DBF menu driver: librarydatabaseproject
DBF menu driver: librarydatabaseproject
2022-07-24 16:46:00 【SakamataZ】
This program has been uploaded to GitHub:https://github.com/ztreble/LibraryDatabaseProject
explain
Menu driver , The implementation effect is as follows , That is, for DBF Check the addition, deletion and modification of documents . First, you can practice JAVA File operations , Second, you can practice the use of collection classes , Third, you can learn JAVA DBF.
DBF It can be used EXCEL Open an ancient relational database , Maybe it can be used for fast data processing ? On the whole, it's still useless , More than three hours is a waste of time .


demand
Put the demand on , Basically achieved , But that # Operation No . It seems that we need JAVA Robot Define mouse events . Specific reference StackOveflow
Code
because javadbf Package not provided DBF Deleted actions , So use file.delete+createTable(String tablename,DBFField[] fields,List<String> fieldsList,List<String> outList,int del) To complete the modification .
It is worth noting that , Do not execute on one file at the same time OutputStream And InputStream Open operation of . This will cause file corruption .
package domain;
import com.linuxense.javadbf.*;
import java.io.*;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import com.linuxense.javadbf.DBFWriter;
public class TableOperation {
public static void createTable(){
String tablename;
LinkedHashMap<String,Integer> map = new LinkedHashMap<String,Integer>();
Scanner scanner = new Scanner(System.in);
System.out.print("\tenter table name:");
tablename = scanner.nextLine();
DBFWriter writer=null;
while(true){
String name;
int length;
System.out.print("\tenter column name and its length(quit by type -1):");
name = scanner.next();
if(name.equals("-1")) break;
length = scanner.nextInt();
map.put(name,length);
}
if(!tablename.equals("")){
String path = "D:\\dbFiles\\"+tablename+".dbf";
File file = new File(path);
try{
if(!file.exists())
file.createNewFile();
DBFField[] fields = new DBFField[map.size()];
Iterator iterator = map.entrySet().iterator();
int i=0;
while(iterator.hasNext()){
Map.Entry<String,Integer> entry = (Map.Entry<String,Integer>)iterator.next();
fields[i] = new DBFField();
fields[i].setName(entry.getKey());
int tmp = entry.getValue();
fields[i].setType(DBFDataType.CHARACTER);
fields[i].setLength(tmp);
i++;
}
writer = new DBFWriter(file);
writer.setFields(fields);
System.out.println("The dbf file product success!");
}catch (IOException e) {
System.out.println("The dbf file product failed!");
e.printStackTrace();
}finally {
DBFUtils.close(writer);
}
}
}
public static void insert(){
File f;
Scanner scanner = new Scanner(System.in);
String filename;
OutputStream fos=null;
InputStream fis =null;
DBFReader reader =null;
DBFWriter writer =null;
while(true){
System.out.print("\tenter file name:");
filename = scanner.next();
filename = "d:\\dbFiles\\"+filename+".dbf";
f = new File(filename);
if(f.exists()) break;
else System.out.println("\tPlease type right filename!");
}
try {
boolean mark = true;
List<String> fieldsList = new ArrayList<>();
fis = new FileInputStream(filename);
reader = new DBFReader(fis);
int fieldsCount = reader.getFieldCount();
for(int i=0;i<fieldsCount;i++){
fieldsList.add(reader.getField(i).getName());
}
DBFUtils.close(reader);
fis.close();
writer = new DBFWriter(new File(filename));
do{
Object[] rowData = new Object[fieldsCount];
String tmpString = null;
for(int i=0;i<fieldsCount;i++){
System.out.printf("\t"+fieldsList.get(i)+":");
tmpString = scanner.next();
rowData[i] = tmpString;
}
writer.addRecord(rowData);
while(true){
System.out.printf("\tMore entries (Y/n):");
tmpString = scanner.next();
if(tmpString.equals("Y")||tmpString.equals("y")){
break;
}
else if(tmpString.equals("n")||tmpString.equals("N")){
mark = false;
break;
}
else{
System.out.println("\tYour Type wrong!");
}
}
}while(mark);
writer.write();
DBFUtils.close(writer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
public static void createTable(String tablename,DBFField[] fields,List<String> fieldsList,List<String> outList,int del){
File file = new File(tablename);
DBFWriter writer=null;
writer = new DBFWriter(new File(tablename));
writer.setFields(fields);
DBFUtils.close(writer);
writer = new DBFWriter(new File(tablename));
Object[] rowData = new Object[fieldsList.size()];
for(int i=0;i<outList.size()/fieldsList.size();i++){
if(i==del) continue;
for(int j=0;j<fieldsList.size();j++){
rowData[j]=outList.get(i*fieldsList.size()+j);
}
writer.addRecord(rowData);
}
writer.write();
}
public static void remove() {
File f;
Scanner scanner = new Scanner(System.in);
String filename;
OutputStream fos=null;
InputStream fis =null;
DBFReader reader =null;
DBFWriter writer =null;
while(true){
System.out.print("\tenter file name:");
filename = scanner.next();
filename = "d:\\dbFiles\\"+filename+".dbf";
f = new File(filename);
if(f.exists()) break;
else System.out.println("\tPlease type right filename!");
}
while(true){
print(filename);
System.out.print("Please enter the number of lines to delete(quit by type -1):");
int num = scanner.nextInt();
if(num==-1) break;
else try {
List<String> fieldsList = new ArrayList<>();
List<String> outList = new ArrayList<>();
fis = new FileInputStream(filename);
reader = new DBFReader(fis);
int fieldsCount = reader.getFieldCount();
DBFField[] fields = new DBFField[fieldsCount];
for(int i=0;i<fieldsCount;i++){
fields[i]=reader.getField(i);
fieldsList.add(reader.getField(i).getName());
}
Object[] rowValues;
while ((rowValues = reader.nextRecord()) != null) {
for (int i = 0; i < rowValues.length; i++) {
outList.add((String) rowValues[i]);
}
}
DBFUtils.close(reader);
fis.close();
f.delete();
createTable(filename,fields,fieldsList,outList,num-1);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
public static void print(String filename) {
InputStream fis = null;
try {
fis = new FileInputStream(filename);
DBFReader reader = new DBFReader(fis);
int fieldsCount = reader.getFieldCount();
System.out.printf("#\t");
for (int i = 0; i < fieldsCount; i++) {
DBFField field = reader.getField(i);
System.out.print(" "+field.getName());
}
System.out.println("");
Object[] rowValues;
int j=1;
while ((rowValues = reader.nextRecord()) != null) {
System.out.printf("#"+j+"\t");
j++;
for (int i = 0; i < rowValues.length; i++) {
System.out.print(" "+rowValues[i]);
}
System.out.println("");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (Exception e) {
}
}
}
}
边栏推荐
- 会议OA项目进度(一)
- Long awaited full platform support - Open Source im project uniapp update of openim
- Envi SHP to ROI and mask the grid
- QT keyboard event (I) -- detect key input
- What does Baidu promote "delete and recall" mean?
- Custom types: Enumeration
- Jenkins cli command details
- MySQL basic commands
- [leetcode]75. color classification - problem solving (execution time beat 90%, memory consumption beat 78%)
- Unity camera free movement control
猜你喜欢

Problems encountered in upgrading chrome to version 80 - solutions to system login failure

VSCode如何鼠标滚轮放大界面

MySQL basic commands

Jing Wei PS tutorial: basic part a

TCP protocol debugging tool tcpengine v1.3.0 tutorial

Axi protocol (1): introduction to AMBA bus, introduction to Axi concept and background, characteristics and functions of Axi protocol

AXI协议(3):AXI架构的握手机制和实现细节

Long awaited full platform support - Open Source im project uniapp update of openim

会议OA项目进度(二)

期盼已久全平台支持-开源IM项目OpenIM之uniapp更新
随机推荐
C font usage effect
剑指 Offer 22. 链表中倒数第k个节点
AMD锐龙7000预计9月15日上市 3D缓存版还要再等等
QT design robot simulation controller -- key control robot joint rotation
Analysis of double pointer sliding window method and solution of leetcode related problems
Getting started with ARP
QT design simulation robot controller
OpenMP入门
简单使用 MySQL 索引
The third edition of New Horizon College English reading and Writing Tutorial 4 graduation examination site (units 1,2,3,5,6)
EventLoop event loop mechanism
Axi protocol (1): introduction to AMBA bus, introduction to Axi concept and background, characteristics and functions of Axi protocol
hping3安装使用
ARP 入门
MySQL basic commands
QT QML virtual keyboard
JVM class loading subsystem
Envi SHP to ROI and mask the grid
荣耀CEO赵明:单一厂商很难实现全场景产品覆盖
Why should we launch getaverse?