📄 filemanager.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package fileManager;import java.io.*;import database.*;import bufferedManager.*;import catalogManager.*;import java.util.ArrayList;import indexManager.*;import java.util.Date;import javax.swing.JOptionPane;/** * * @author outlaw */public class FileManager { public static final String WORKPATH="d:/workspace/"; public static final String DATABASE_INFO="d:/workspace/System.ini"; public static final String LOG=WORKPATH+"db.log"; public FileManager(){ } public static void writeOutObject(Object obj,String fullName) throws Exception{ try{ //System.out.println(fullName); File parentDir=new File(new File(fullName).getParent()); if(!parentDir.exists()) parentDir.mkdirs(); ObjectOutputStream oos=new ObjectOutputStream( new FileOutputStream(fullName)); oos.writeObject(obj); oos.close(); }catch(Exception ex){ throw new OperationInfo("File process error!:\nIn FileManager.writeOutObject."); } } public static TableData readInTable(String fullName) throws Exception { ObjectInputStream ois=new ObjectInputStream(new FileInputStream(fullName)); TableData tb=(TableData) ois.readObject(); ois.close(); return tb; } public static DataBaseData readInDB(String fullName) throws Exception { ObjectInputStream ois=new ObjectInputStream(new FileInputStream(fullName)); DataBaseData db=(DataBaseData)ois.readObject(); ois.close(); return db; } public static DataBaseData readInDBByName(String name) throws Exception { return readInDB(WORKPATH+name+"/"+name+".db"); } public static void createFile(String fullName) throws Exception { File file=new File(fullName); File parent=new File(file.getParent()); if(!parent.exists()) parent.mkdirs(); file.createNewFile(); } public static void writeBlockBack(Block block) throws Exception { int blockID=block.getBlockID(); String fullName=null; TableData tb=CatalogManager.getTableByName(block.getTBName()); if(tb==null) throw new OperationInfo("Insert tuple error!Table not found!"); fullName=tb.getFullName()+".tbl"; try { RandomAccessFile raf=new RandomAccessFile(fullName,"rw"); raf.seek(Block.BLOCKSIZE*block.getBlockID()); /*System.out.println(block.getBlockID()); Tuple[] tuples=block.getTuples(); for(int i=0;i<tuples.length;i++) { if(tuples[i]!=null) { System.out.println("--------------------------------"); String[] values=tuples[i].getValues(); for(int j=0;j<values.length;j++) System.out.println(values[j]); } }*/ //byte[] array=block.toBytes(); //System.out.println("length: "+array.length); raf.write(block.toBytes()); raf.close(); tb.setBlockInFile(blockID); }catch(Exception ex) { ex.printStackTrace(); throw new OperationInfo("Write block back error!"); } } public static void writeOutIndex(Index index) throws Exception { String fullName=index.getFullName(); FileOutputStream fos=new FileOutputStream(fullName); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(index); oos.close(); } public static void makeDir(String path) { File file=new File(path); if(!file.exists()) { file.mkdir(); } } public static Index readInIndex(String fullName) throws Exception { FileInputStream fis=new FileInputStream(fullName); ObjectInputStream ois=new ObjectInputStream(fis); Index index=(Index) ois.readObject(); return index; } public static void writeOutIndices() throws Exception { ArrayList<indexManager.Index> indices=IndexManager.indices; int len=indices.size(); for(int i=0;i<len;i++) { if(indices.get(i).isDirty()) { Index index=indices.get(i); index.clearDirty(); writeOutIndex(index); } } } public static void writeOutTables() throws Exception { DataBaseData db=CatalogManager.getCurDB(); ArrayList<TableData> tables=db.getTables(); int len=tables.size(); for(int i=0;i<len;i++) { writeOutTable(tables.get(i)); } } public static void writeOutTable(TableData tb) throws Exception { String fullName=tb.getPath()+tb.getName()+".tbl"; FileOutputStream fos=new FileOutputStream(fullName); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(tb); oos.close(); } public static void writeOutDataBaseInfo() throws Exception { ArrayList<String> names=CatalogManager.getAllDBName(); DataBaseInfo dbi=new DataBaseInfo(names); writeOutObject(dbi,DATABASE_INFO); } public static DataBaseInfo getDataBaseInfo() throws Exception { ObjectInputStream ois=new ObjectInputStream(new FileInputStream(DATABASE_INFO)); DataBaseInfo dbi=(DataBaseInfo)ois.readObject(); ois.close(); return dbi; } public static void writeOutDataBase() throws Exception { DataBaseData db=CatalogManager.getCurDB(); String fullName=db.getFullName()+".db"; FileOutputStream fos=new FileOutputStream(fullName); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(db); oos.close(); } public static void deleteFile(String fullName) throws Exception { File file=new File(fullName); if(file.exists()) if(!file.delete()) JOptionPane.showMessageDialog(null, "delete error!"); } public static Index readIndexIntoMemory(String fullName) throws Exception { FileInputStream fis=new FileInputStream(fullName); ObjectInputStream ois=new ObjectInputStream(fis); Index index=(Index)ois.readObject(); ois.close(); return index; } public static void writeOutLog(String log) throws Exception { RandomAccessFile raf=new RandomAccessFile(LOG,"rw"); long len=raf.length(); raf.seek(len); Date date=new Date(); String _date=date.toString(); raf.write("==================================================================\n".getBytes()); raf.write((_date+"\n").getBytes()); raf.write("==================================================================\n".getBytes()); raf.write(log.getBytes()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -