?? textfile.java
字號:
/* * * textFile - an extension of tinySQL for text file access * * Copyright 1996, Brian C. Jepson * (bjepson@ids.net) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */import java.util.*;import java.lang.*;import java.io.*;public class textFile extends tinySQL { // the data directory where textFile stores its files // static String dataDir = System.getProperty("user.home") + "/.tinySQL"; /** * * Creates a table given the name and a vector of * column definition arrays. * The column definition arrays have the following elements: * * Element 1: Data type * Element 2: Column name * Element 3: Size * * @param table_name the name of the table * @param v a Vector containing arrays of column definitions. * @see tinySQL#CreateTable * */ void CreateTable ( String table_name, Vector v ) throws IOException, tinySQLException { // make the data directory, if it needs to be make // mkDataDirectory(); // perform an implicit drop table. // DropTable(table_name); // create the table definition file // FileOutputStream fdef = new FileOutputStream( dataDir + "/" + table_name + ".def" ); // open it as a DataOutputStream // DataOutputStream def = new DataOutputStream (fdef); // write out the column definition for the _DELETED column // def.writeBytes("CHAR|_DELETED|1\n"); // write out the rest of the columns' definition. The // definition consists of datatype, column name, and // size delimited by a pipe symbol // for (int i = 0; i < v.size(); i++) { def.writeBytes( ((String[]) v.elementAt(i))[0] + "|"); def.writeBytes( ((String[]) v.elementAt(i))[1] + "|"); def.writeBytes( ((String[]) v.elementAt(i))[2] + "\n"); } // flush the DataOutputStream and jiggle the handle // def.flush(); // close the file // fdef.close(); } /** * * Return a tinySQLTable object, given a table name. * * @param table_name * @see tinySQL#getTable * */ tinySQLTable getTable (String table_name) throws tinySQLException { return (tinySQLTable) new textFileTable (dataDir, table_name); } /** * * Drop a named table by deleting it and its associated * .def file. * * @param fname table name * @see tinySQL#DropTable * */ void DropTable (String fname) throws tinySQLException { try { delFile(fname); delFile(fname + ".def"); } catch (Exception e) { throw new tinySQLException(e.getMessage()); } } /* * * Make the data directory unless it already exists * */ void mkDataDirectory() throws NullPointerException { File dd = new File( dataDir ); if (!dd.exists()) { dd.mkdir(); } } /* * * Delete a file in the data directory * */ void delFile (String fname) throws NullPointerException, IOException { File f = new File( dataDir + "/" + fname ); // only delete a file that exists // if (f.exists()) { // try the delete. If it fails, complain // if (!f.delete()) { throw new IOException("Could not delete file: " + dataDir + "/" + fname + "."); } } } /* * regression test */ public static void main(String argv[]) { textFile foo = new textFile(); tsResultSet trs = null; try { trs = foo.sqlexec("CREATE TABLE test (name CHAR(10))"); trs = foo.sqlexec("INSERT INTO test (name) VALUES('test')"); trs = foo.sqlexec("SELECT name FROM test"); } catch (Exception e) { e.printStackTrace(); } tsRow row = trs.rowAt(0); tsColumn column = trs.columnAtIndex(0); String colval = row.columnAsString(column); if (colval.startsWith("test")) { System.out.println("textFile driver installed correctly."); } else { System.out.println("Test was not successful :-("); System.out.println("Got \"" + colval + "\", expected \"test\""); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -