?? databackupframe.java
字號:
}
//取得文件路徑
String pathName = jTextField1.getText().trim();
if(pathName.length() == 0){
JOptionPane.showMessageDialog(null, "請選擇備份文件目錄.");
return;
}
//清空信息列表框的內容
listData3.clear();
String tableName = "";
for(int i = 0; i < selectedIndexes.length; i++){
//取得選擇數據表的名字
tableName = (String)listData1.getElementAt(selectedIndexes[i]);
try{
//備份數據表
backupTable(tableName, pathName);
}catch(Exception ex){
ex.printStackTrace();
}
}
//單擊備份所有數據表按鈕的處理代碼
}else if(actionCommand.equals("backupAllTables")){
if(listData1.size() == 0){
JOptionPane.showMessageDialog(null, "請顯示數據表.");
return;
}
//取得文件路徑
String pathName = jTextField1.getText().trim();
if(pathName.length() == 0){
JOptionPane.showMessageDialog(null, "請選擇備份文件目錄.");
return;
}
//清空信息列表框的內容
listData3.clear();
String tableName = "";
for(int i = 0; i < listData1.size(); i++){
//取得選擇數據表的名字
tableName = (String)listData1.getElementAt(i);
try{
//備份數據表
backupTable(tableName, pathName);
}catch(Exception ex){
ex.printStackTrace();
}
}
//單擊恢復選擇文件按鈕的處理代碼
}else if(actionCommand.equals("restoreSelectedFiles")){
//取得文件路徑
String pathName = jTextField1.getText().trim();
if(pathName.length() == 0){
JOptionPane.showMessageDialog(null, "請選擇備份文件目錄.");
return;
}
int[] selectedIndexes = jList2.getSelectedIndices();
if(selectedIndexes.length == 0){
JOptionPane.showMessageDialog(null, "請選擇備份文件.");
return;
}
if(listData1.size() == 0){
JOptionPane.showMessageDialog(null, "請顯示數據表.");
return;
}
//清空信息列表框的內容
listData3.clear();
String fileName = "";
for(int i = 0; i < selectedIndexes.length; i++){
fileName = (String)listData2.getElementAt(selectedIndexes[i]);
//根據文件名取得數據表的名字
String tableName = fileName.substring(0, fileName.indexOf(".dat"));
//檢查數據表名字是否存在
if(listData1.indexOf(tableName) == -1){
JOptionPane.showMessageDialog(null, fileName + "文件的數據表在數據庫不存在,"
+ "不可以進行恢復操作.");
continue;
}
try{
restoreTable(fileName, tableName, pathName);
}catch(Exception ex){
ex.printStackTrace();
}
}
//單擊恢復全部文件按鈕的處理代碼
}else if(actionCommand.equals("restoreAllFiles")){
//取得文件路徑
String pathName = jTextField1.getText().trim();
if(pathName.length() == 0){
JOptionPane.showMessageDialog(null, "請選擇備份文件目錄.");
return;
}
if(listData2.size() == 0){
JOptionPane.showMessageDialog(null, "請顯示備份文件.");
return;
}
if(listData1.size() == 0){
JOptionPane.showMessageDialog(null, "請顯示數據表.");
return;
}
//清空信息列表框的內容
listData3.clear();
String fileName = "";
for(int i = 0; i < listData2.size(); i++){
fileName = (String)listData2.getElementAt(i);
//根據文件名取得數據表的名字
String tableName = fileName.substring(0, fileName.indexOf(".dat"));
//檢查數據表名字是否存在
if(listData1.indexOf(tableName) == -1){
JOptionPane.showMessageDialog(null, fileName + "文件的數據表在數據庫不存在,"
+ "不可以進行恢復操作.");
continue;
}
try{
restoreTable(fileName, tableName, pathName);
}catch(Exception ex){
ex.printStackTrace();
}
}
}else if(actionCommand.equals("exit")){
exit();
}
}
//將數據表數據保存在文件的方法
public void backupTable(String tableName, String pathName) throws Exception{
String[][] data = null;
//創建文件類
File file = new File(pathName + "\\" + tableName + ".dat");
if(file.isFile()){
//取得信息確認框的返回值
int actionInt = JOptionPane.showConfirmDialog(null, tableName + ".dat"
+ "文件已存在,是否覆蓋該文件?", "信息確認框",
JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
//如果單擊否按鈕,不進行覆蓋操作
if(actionInt == JOptionPane.NO_OPTION){
listData3.addElement(tableName + ".dat文件已存在,不進行覆蓋操作.");
return;
}
}
//創建文件寫出類
FileWriter writer = new FileWriter(pathName + "\\" + tableName + ".dat");
//取得數據表的數據
data = stockManagementData.getDataByTableName(tableName);
for (int row = 0; row < data.length; row++) {
for (int col = 0; col < data[0].length; col++) {
//如果字段值為空值,轉換為null字符串,如果字段值的長度為度,加入一個空格
if(data[row][col] == null){
data[row][col] = "null";
}else if(data[row][col].length() == 0){
data[row][col] = " ";
}
if(col == data[0].length -1){
//\n是換行符
writer.write(data[row][col] + "\n");
}else{
//\t是水平制表符
writer.write(data[row][col] + "\t");
}
}
}
//關閉文件寫出類
writer.close();
listData3.addElement(tableName + "數據表成功備份,數據表的保存路徑是" + pathName + ".");
//將用戶備份數據表信息寫入日志數據表
stockManagementData.createUserLog("數據備份窗口", tableName + "數據表備份操作",
user.getUserName());
}
//將文件數據寫入數據表的方法
public void restoreTable(String fileName, String tableName, String pathName) throws
Exception {
//創建數組
String[][] data = null;
File inFile = new File(pathName + "\\" + fileName);
//讀入文件
FileReader reader = new FileReader(inFile);
//創建集合類
Vector vector = new Vector();
BufferedReader bufferedReader = new BufferedReader(reader);
while (bufferedReader.ready()) {
//讀入一行內容
vector.add(bufferedReader.readLine());
}
if (vector.size() > 0) {
//取得行總數
int rowLength = vector.size();
String tempStr = (String) vector.get(0);
StringTokenizer stringToken = new StringTokenizer(tempStr, "\t");
//取得列總數
int colLength = stringToken.countTokens();
//根據行和列的總數創建內容數組
data = new String[rowLength][colLength];
for (int row = 0; row < rowLength; row++) {
stringToken = new StringTokenizer( (String) vector.get(row), "\t");
for (int col = 0; col < colLength; col++) {
tempStr = stringToken.nextToken();
//取代/n字符串
tempStr.replace('\n', ' ');
tempStr = tempStr.trim();
//向數組寫入內容
data[row][col] = tempStr;
}
}
//將數組數據寫入數據表
stockManagementData.setDataByTableName(tableName, data);
listData3.addElement(fileName + "文件的數據已寫入數據表" + tableName + ".");
//將用戶恢復數據表信息寫入日志數據表
stockManagementData.createUserLog("數據備份窗口", tableName + "數據表恢復操作",
user.getUserName());
}
else {
listData3.addElement(fileName + "文件沒有數據.");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -