// call this method to write the description to the file // the file pointer must be already positioned // the description must have been set private void writeDescription() throws IOException { if (description.length() > DESCRIPTION_LENGTH) description = description.substring(0, DESCRIPTION_LENGTH); outStream.writeChars(description); for (int j = 0; j < (DESCRIPTION_LENGTH - description.length()); j++) outStream.writeChar(' '); }
private void write(String partnum, String partdesc, int qty, int ucost) throws IOException { StringBuffer sb = new StringBuffer(partnum); if (sb.length() > PNUMLEN) sb.setLength(PNUMLEN); else if (sb.length() < PNUMLEN) { int len = PNUMLEN - sb.length(); for (int i = 0; i < len; i++) sb.append(" "); } raf.writeChars(sb.toString()); sb = new StringBuffer(partdesc); if (sb.length() > DESCLEN) sb.setLength(DESCLEN); else if (sb.length() < DESCLEN) { int len = DESCLEN - sb.length(); for (int i = 0; i < len; i++) sb.append(" "); } raf.writeChars(sb.toString()); raf.writeInt(qty); raf.writeInt(ucost); }
// call this method to write the column names to the file // the file pointer must be already positioned private void writeColumnNames(ArrayList names) throws IOException { String column; for (int i = 0, numCols = names.size(); i < numCols; i++) { column = (String) names.get(i); if (column.length() > COLUMN_LENGTH) column = column.substring(0, COLUMN_LENGTH); outStream.writeChars(column); for (int j = 0; j < (COLUMN_LENGTH - column.length()); j++) outStream.writeChar(' '); } }
// write a String to the file public boolean writeString(String str) { if (false == this.startedFlag) { System.err.println("TcpFileReadWriter: this object is closed already"); return false; } try { raf.writeChars(str); } catch (Exception e) { e.printStackTrace(); System.err.println("TCPFileReadWriter: writeString fail!!"); return false; } return true; }
public void writeOutput() throws IOException { //noinspection ResultOfMethodCallIgnored new File(path).mkdirs(); RandomAccessFile randomAccessFile = null; try { randomAccessFile = new RandomAccessFile(path + "/" + getExportFileName(), "rw"); randomAccessFile.setLength(0); randomAccessFile.writeChars(output); } finally { try { if (randomAccessFile != null) { randomAccessFile.close(); } } catch (IOException e) { // do nothing } } }
/** sample usage and testing */ public static void main(String[] args) { Itemset is1 = new Itemset(); is1.add(1); is1.add(2); Itemset is2 = new Itemset(); is2.add(3); is2.add(2); Itemset is3 = new Itemset(); is3.add(3); is3.add(1); Itemset is4 = new Itemset(); is4.add(33); is4.add(3); ArrayList colNames = new ArrayList(3); colNames.add("cheese"); colNames.add("pizza"); colNames.add("beer"); System.out.println("\n\nCreating invalid database:"); try { RandomAccessFile invalid = new RandomAccessFile("invalid.db", "rw"); invalid.writeChars(ID + " - a bogus file that looks like a valid one"); invalid.close(); } catch (Exception e) { System.out.println("Shouldn't have happened: " + e); } System.out.println("\n\nCreating corrupted database:"); try { DBWriter corrupted = new DBWriter("corrupted.db"); try { corrupted.addRow(is1); } catch (DBException e) { System.out.println(e); } corrupted.setDescription("a corrupted database"); corrupted.setColumnNames(colNames); corrupted.addRow(is1); corrupted.setDescription("a corrupted database - 2"); corrupted.setColumnNames(colNames); corrupted.addRow(is2); corrupted.setDescription("a corrupted database - 3"); corrupted.addRow(is3); try { corrupted.addRow(is4); } catch (DBException e) { System.out.println(e); } corrupted.close(); System.out.println("corrupting file"); RandomAccessFile raf = new RandomAccessFile("corrupted.db", "rw"); raf.seek(770); // replace the 2 in the second itemset with a 3 raf.writeInt(3); raf.close(); } catch (Exception e) { System.out.println("Shouldn't have happened: " + e); } System.out.println("\n\nCreating empty database:"); try { DBWriter empty = new DBWriter("empty.db"); empty.setDescription("an empty database"); empty.setColumnNames(colNames); empty.close(); } catch (Exception e) { System.out.println("Shouldn't have happened: " + e); } System.out.println("\n\nCreating correct database:"); try { DBWriter correct = new DBWriter("correct.db"); correct.setDescription("a correct database"); correct.setColumnNames(colNames); correct.addRow(is1); correct.setDescription("a correct database - 2"); correct.setColumnNames(colNames); correct.addRow(is2); correct.setDescription("a correct database - 3"); correct.addRow(is3); correct.close(); correct = new DBWriter("correct.db"); correct.setColumnNames(colNames); correct.addRow(is1); correct.setDescription("a correct database - 4"); correct.setColumnNames(colNames); correct.addRow(is2); correct.setDescription("a correct database - 5"); correct.addRow(is3); correct.close(); } catch (Exception e) { System.out.println("Shouldn't have happened: " + e); } System.out.println("\n\nOpening and closing DBWriter:"); try { DBWriter bummer = new DBWriter("bummer.db"); bummer.close(); } catch (Exception e) { System.out.println("Shouldn't have happened: " + e); } }
/** * Set the column names for the database. * * @param names the column names * @exception IOException from library call * @exception DBException size of <code>names</code> does not match number of columns */ public void setColumnNames(ArrayList names) throws IOException, DBException { long namesSize = names.size(); if (wroteColumnNames == false) { numColumns = namesSize; // ID outStream.writeChars(ID); // version number outStream.writeInt(1); outStream.writeInt(0); outStream.writeInt(0); headerSize = COLUMN_NAME_OFFSET + COLUMN_SIZE * numColumns + ID_SIZE + DESCRIPTION_SIZE + ID_SIZE + CRC_SIZE; outStream.writeLong(headerSize); // number of rows outStream.writeLong(0); // number of columns outStream.writeLong(numColumns); // columns writeColumnNames(names); // rest of the header outStream.writeChars(ID); // check if description has been set, otherwise fill in with blanks if (description != null) writeDescription(); else { description = " "; writeDescription(); } outStream.writeChars(ID); // CRC outStream.writeInt(0); wroteColumnNames = true; } else { if (namesSize != numColumns) throw new DBException("Cannot change the number of columns"); else { if (needReposition == false) { lastPosition = outStream.getFilePointer(); needReposition = true; } outStream.seek(COLUMN_NAME_OFFSET); writeColumnNames(names); } } }
/* @see java.io.DataOutput.writeChars(String) */ public void writeChars(String s) throws IOException { raf.writeChars(s); }