public String[] getNextRecord() { try { String ans[] = new String[3]; String line; while ((line = filein.readLine()) != null) { // get description if (!line.substring(0, 1).equals("@")) return null; ans[0] = line.substring(1); String seq = ""; String qual = ""; while ((line = filein.readLine()) != null) { if (line.matches("[ACTGNacgtnURYSWKMBDHVN.-]*")) { seq += line; } else { if (!line.substring(0, 1).equals("+")) return null; // ans[2] = line.substring(1); while ((line = filein.readLine()) != null) { qual += line; if (seq.length() <= qual.length()) { ans[1] = seq; ans[2] = qual; return ans; } } } } } return null; } catch (Exception e) { return null; } }
/** * Search index file for a primary key value * * @param pkvalue the primary key value to search for * @param position [0] = index entry, [1] = table row * @throws Exception */ public boolean searchIndex(String pkvalue, Positions pos) throws Exception { boolean found = false; boolean end = false; if (!indexExists(def.getPK())) { throw new Exception("No index created"); } int pkindex = def.getColPosition(def.getPK()); if (pkindex == -1) { throw new Exception("Primary key does not exist"); } // calculate index = hash value String s_value = pkvalue.trim(); int index = hash(s_value); Integer[] size = def.getSizes(); int recordSize = idxFixedRecordLength() + size[pkindex]; indexFile.seek(index * recordSize); String line = indexFile.readLine(); if (line.substring(0, 1).equals(" ")) { // Empty record, end of search found = false; return found; } String[] parts = line.split("#"); String s_part = parts[2].trim(); if (s_part.equals(pkvalue) && !(parts[1].equals("D"))) { found = true; pos.index = Integer.parseInt(parts[0].trim()); pos.table = Integer.parseInt(parts[3].trim()); } while (!found && !end) { if (parts[4].substring(0, 4).equals("null")) { // end of linked list end = true; found = false; } else { index = Integer.parseInt(parts[4].trim()); indexFile.seek(index * recordSize); line = indexFile.readLine(); parts = line.split("#"); if (parts[2].trim().equals(pkvalue) && !(parts[1].equals("D"))) { found = true; pos.index = Integer.parseInt(parts[0].trim()); pos.table = Integer.parseInt(parts[3].trim()); } } } return found; }
/* (non-Javadoc) * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int) */ public int print(Graphics g, PageFormat pf, int page) throws PrinterException { int ret = PAGE_EXISTS; String line = null; try { if (fph.knownPage(page)) { in.seek(fph.getFileOffset(page)); line = in.readLine(); } else { long offset = in.getFilePointer(); line = in.readLine(); if (line == null) { ret = NO_SUCH_PAGE; } else { fph.createPage(page); fph.setFileOffset(page, offset); } } if (ret == PAGE_EXISTS) { // Seite ausgeben, Grafikkontext vorbereiten Graphics2D g2 = (Graphics2D) g; g2.scale(1.0 / RESMUL, 1.0 / RESMUL); int ypos = (int) pf.getImageableY() * RESMUL; int xpos = ((int) pf.getImageableX() + 2) * RESMUL; int yd = 12 * RESMUL; int ymax = ypos + (int) pf.getImageableHeight() * RESMUL - yd; // Seitentitel ausgeben ypos += yd; g2.setColor(Color.black); g2.setFont(new Font("Monospaced", Font.BOLD, 10 * RESMUL)); g.drawString(fbname + " Seite " + (page + 1), xpos, ypos); g.drawLine( xpos, ypos + 6 * RESMUL, xpos + (int) pf.getImageableWidth() * RESMUL, ypos + 6 * RESMUL); ypos += 2 * yd; // Zeilen ausgeben g2.setColor(new Color(0, 0, 127)); g2.setFont(new Font("Monospaced", Font.PLAIN, 10 * RESMUL)); while (line != null) { g.drawString(line, xpos, ypos); ypos += yd; if (ypos >= ymax) { break; } line = in.readLine(); } } } catch (IOException e) { throw new PrinterException(e.toString()); } return ret; }
/** * Delete an index entry in the idx file of the specified column. * * @param colname the column's name * @param idxPos index entry (line nr) to be deleted * @throws Exception */ protected long deleteIndexEntry(String colname, int idxPos) throws Exception { long s = System.currentTimeMillis(); if (!indexExists(colname)) { throw new Exception("No index created"); } int pkindex = def.getColPosition(colname); if (pkindex == -1) { throw new Exception("Column does not exist"); } Integer[] size = def.getSizes(); int recordSize = idxFixedRecordLength() + size[pkindex]; indexFile.seek(idxPos * recordSize); String sLine = indexFile.readLine(); String[] parts = sLine.split("#"); if (Integer.parseInt(parts[0].trim()) != idxPos) { throw new Exception("Index not found in index file"); } else { indexFile.seek(idxPos * recordSize + 6); String flag = "D"; indexFile.write(flag.toString().getBytes()); } long e = System.currentTimeMillis(); return e - s; }
// RandomAccessFile public void randomAccess(String path) { // RandomAccessFile 的参数可为 "r","w","rw","rwd"(同步保存文件更改),"rws"(同步保存文件更改和文件metadata更改) try (RandomAccessFile randomAccessFile = new RandomAccessFile(path, "r")) { String s; while ((s = randomAccessFile.readLine()) != null) System.out.println(s); } catch (IOException e) { e.printStackTrace(); } }
public static String read(String s) throws IOException { String s1; RandomAccessFile randomaccessfile; s1 = ""; randomaccessfile = null; File file = new File(s); randomaccessfile = new RandomAccessFile(file, "r"); s1 = randomaccessfile.readLine(); randomaccessfile.close(); return s1; }
/** * Method description * * @return */ public String readLine() { // read in a line from the file s = null; try { s = f.readLine(); // could throw error } catch (IOException e) { errflag = true; System.out.println("File read error"); } return s; }
protected void parseHeader() { String line = null; header = ""; long pos = 0; try { pos = filein.getFilePointer(); while ((line = filein.readLine()) != null) { String tag = line.substring(0, 1); if (tag.equals("#")) { header += line; header += "\n"; pos = filein.getFilePointer(); } else { break; } } if (pos >= 0) { filein.seek(pos); } } catch (IOException ie) { } }
public static void produceStockReport() { // initialising variables String area; String stockItem; String qty; String employee; String[] parts; String currentArea; String previousArea; int areaTransactionCount = 0; int areaItemQty = 0; String line = ""; int transactionTotal = 0; int totalItemQty = 0; // Using print headings method printHeadings(); try { RandomAccessFile raf = new RandomAccessFile("StockReport.txt", "rw"); // reading file line = raf.readLine(); // splitting strings parts = line.split(","); boolean eof = false; // putting splits into array area = parts[0]; stockItem = parts[1]; qty = parts[2]; employee = parts[3]; currentArea = area; previousArea = area; // while loop while (line != null) { parts = line.split(","); area = parts[0]; stockItem = parts[1]; qty = parts[2]; employee = parts[3]; currentArea = area; // if statement to print area totals if (!currentArea.equals(previousArea)) { System.out.println("++++++++++++++++++++++++++"); System.out.println("Area: " + previousArea); System.out.println("Area transaction count: " + areaTransactionCount); System.out.println("Area item qty: " + areaItemQty); System.out.println("++++++++++++++++++++++++++"); System.out.println( "----------------------------------------------------------------------------------"); totalItemQty = totalItemQty + areaItemQty; previousArea = area; areaTransactionCount = 0; areaItemQty = 0; } System.out.println( "Area:" + area + ",\tStock item:" + stockItem + ",\tQty exchange:" + qty + ",\tEmployee:" + employee); ++areaTransactionCount; int itemQty = Integer.parseInt(qty); ++transactionTotal; areaItemQty += itemQty; // reading file line = raf.readLine(); } System.out.println("++++++++++++++++++++++++++"); System.out.println("Area: " + previousArea); System.out.println("Area transaction count: " + areaTransactionCount); System.out.println("Area item qty: " + areaItemQty); System.out.println("++++++++++++++++++++++++++"); System.out.println( "----------------------------------------------------------------------------------\n"); totalItemQty = totalItemQty + areaItemQty; // printing grand totals System.out.println( "----------------------------------------------------------------------------------"); System.out.println( "----------------------------------------------------------------------------------"); System.out.println("Total transaction count: " + transactionTotal); System.out.println("Total item qty: " + totalItemQty); System.out.println( "\n----------------------------------------------------------------------------------"); System.out.println( "----------------------------------------------------------------------------------"); } catch (IOException io) { System.out.println("Error" + io.getMessage()); } }
private void parseMobFile(String strStore, RandomAccessFile rafFile) { try { if (strStore.equalsIgnoreCase("skill")) { strStore = rafFile.readLine(); int value = Byte.parseByte(rafFile.readLine()); addToSkill(strStore, value); engGame.log.printMessage(Log.DEBUG, strStore + "=" + value); return; } if (strStore.equalsIgnoreCase("condition")) { Condition cndStore = engGame.getCondition(rafFile.readLine()); cndStore.intTicksPast = Integer.parseInt(rafFile.readLine()); cndStore.intDuration = Integer.parseInt(rafFile.readLine()); addCondition(cndStore); engGame.log.printMessage(Log.DEBUG, "condition \"" + cndStore.strName + "\""); return; } if (strStore.equalsIgnoreCase("giveitem")) { String strItem = rafFile.readLine(); double dblChance = Double.valueOf(rafFile.readLine()).doubleValue(); vctGiveItems.addElement(new GiveItem(strItem, dblChance)); engGame.log.printMessage( Log.DEBUG, strName + " gives a \"" + strItem + "\" " + (100 * dblChance) + "% of the time."); return; } if (strStore.equalsIgnoreCase("item")) { Item itmStore = engGame.getItem(rafFile.readLine()); if (itmStore != null) { itmStore.lngDurability = Long.parseLong(rafFile.readLine()); itmStore.intUses = Integer.parseInt(rafFile.readLine()); vctItems.addElement(itmStore); } return; } if (strStore.equalsIgnoreCase("clan")) { strClan = rafFile.readLine(); return; } if (strStore.equalsIgnoreCase("race")) { strRace = rafFile.readLine(); return; } if (strStore.equalsIgnoreCase("title")) { strTitle = rafFile.readLine(); return; } if (strStore.equalsIgnoreCase("description")) { strDescription = rafFile.readLine(); return; } if (strStore.equalsIgnoreCase("x")) { intLocX = Integer.parseInt(rafFile.readLine()); return; } if (strStore.equalsIgnoreCase("y")) { intLocY = Integer.parseInt(rafFile.readLine()); return; } if (strStore.equalsIgnoreCase("maxhp")) { maxhp = Integer.parseInt(rafFile.readLine()); return; } if (strStore.equalsIgnoreCase("maxmp")) { maxmp = Integer.parseInt(rafFile.readLine()); return; } if (strStore.equalsIgnoreCase("stre")) { stre = Integer.parseInt(rafFile.readLine()); return; } if (strStore.equalsIgnoreCase("inte")) { inte = Integer.parseInt(rafFile.readLine()); return; } if (strStore.equalsIgnoreCase("dext")) { dext = Integer.parseInt(rafFile.readLine()); return; } if (strStore.equals("cons")) { cons = Integer.parseInt(rafFile.readLine()); return; } if (strStore.equalsIgnoreCase("wisd")) { wisd = Integer.parseInt(rafFile.readLine()); return; } if (strStore.equalsIgnoreCase("image")) { intImage = Integer.parseInt(rafFile.readLine()); return; } if (strStore.equalsIgnoreCase("bravery")) { dblBravery = Double.valueOf(rafFile.readLine()).doubleValue(); return; } if (strStore.equalsIgnoreCase("grouprelation")) { dblGroupRelation = Double.valueOf(rafFile.readLine()).doubleValue(); return; } if (strStore.equalsIgnoreCase("wield")) { equWorn.wield = engGame.getItem(rafFile.readLine()); if (equWorn.wield != null) { equWorn.wield.lngDurability = Long.parseLong(rafFile.readLine()); equWorn.wield.intUses = Integer.parseInt(rafFile.readLine()); } runWearScript(equWorn.wield); return; } if (strStore.equalsIgnoreCase("arms")) { equWorn.arms = engGame.getItem(rafFile.readLine()); if (equWorn.arms != null) { equWorn.arms.lngDurability = Long.parseLong(rafFile.readLine()); equWorn.arms.intUses = Integer.parseInt(rafFile.readLine()); } runWearScript(equWorn.arms); return; } if (strStore.equalsIgnoreCase("legs")) { equWorn.legs = engGame.getItem(rafFile.readLine()); if (equWorn.legs != null) { equWorn.legs.lngDurability = Long.parseLong(rafFile.readLine()); equWorn.legs.intUses = Integer.parseInt(rafFile.readLine()); } runWearScript(equWorn.legs); return; } if (strStore.equalsIgnoreCase("torso")) { equWorn.torso = engGame.getItem(rafFile.readLine()); if (equWorn.torso != null) { equWorn.torso.lngDurability = Long.parseLong(rafFile.readLine()); equWorn.torso.intUses = Integer.parseInt(rafFile.readLine()); } runWearScript(equWorn.torso); return; } if (strStore.equalsIgnoreCase("waist")) { equWorn.waist = engGame.getItem(rafFile.readLine()); if (equWorn.waist != null) { equWorn.waist.lngDurability = Long.parseLong(rafFile.readLine()); equWorn.waist.intUses = Integer.parseInt(rafFile.readLine()); } runWearScript(equWorn.waist); return; } if (strStore.equalsIgnoreCase("neck")) { equWorn.neck = engGame.getItem(rafFile.readLine()); if (equWorn.neck != null) { equWorn.neck.lngDurability = Long.parseLong(rafFile.readLine()); equWorn.neck.intUses = Integer.parseInt(rafFile.readLine()); } runWearScript(equWorn.neck); return; } if (strStore.equalsIgnoreCase("skull")) { equWorn.skull = engGame.getItem(rafFile.readLine()); if (equWorn.skull != null) { equWorn.skull.lngDurability = Long.parseLong(rafFile.readLine()); equWorn.skull.intUses = Integer.parseInt(rafFile.readLine()); } runWearScript(equWorn.skull); return; } if (strStore.equalsIgnoreCase("eyes")) { equWorn.eyes = engGame.getItem(rafFile.readLine()); if (equWorn.eyes != null) { equWorn.eyes.lngDurability = Long.parseLong(rafFile.readLine()); equWorn.eyes.intUses = Integer.parseInt(rafFile.readLine()); } runWearScript(equWorn.eyes); return; } if (strStore.equalsIgnoreCase("hands")) { equWorn.hands = engGame.getItem(rafFile.readLine()); if (equWorn.hands != null) { equWorn.hands.lngDurability = Long.parseLong(rafFile.readLine()); equWorn.hands.intUses = Integer.parseInt(rafFile.readLine()); } runWearScript(equWorn.hands); return; } if (strStore.equalsIgnoreCase("faction")) { String strFaction = rafFile.readLine(); fctFaction = engGame.getFaction(strFaction); if (fctFaction != null) { engGame.log.printMessage(Log.DEBUG, "faction=\"" + fctFaction.strName + "\""); } else { engGame.log.printMessage(Log.DEBUG, "no faction found for \"" + strFaction + "\""); } return; } if (strStore.equalsIgnoreCase("onBattle")) { strOnBattle = rafFile.readLine(); return; } if (strStore.equalsIgnoreCase("nofollow")) { noFollow = true; return; } } catch (Exception e) { engGame.log.printError( "parseMobFile():Parsing \"" + strStore + "\" from " + strName + "'s file", e); } }
/** * Create an index entry in the idx file of the specified column. * * @param tablePosition position or line nr in table *.tbl * @param pkValue the value of the primary key to create an index entry for * @throws Exception */ protected void addIndexEntry(int tablePosition, String pkValue) throws Exception { if (!indexExists(def.getPK())) { throw new Exception("No index created"); } int pkindex = def.getColPosition(def.getPK()); if (pkindex == -1) { throw new Exception("Primary key does not exist"); } Integer[] size = def.getSizes(); if (pkValue.length() > TableDefinition.INTEGER_SIZE) { throw new Exception("Supplied pkValue too large"); } else { // make sure key value has appropriate length StringBuffer temp = new StringBuffer(); temp.append(pkValue); for (int i = 0; i < TableDefinition.INTEGER_SIZE - pkValue.length(); i++) { temp.append(' '); } pkValue = temp.toString(); } // calculate index = hash value String s_value = pkValue.trim(); int indexPosition = hash(s_value); int recordSize = idxFixedRecordLength() + size[pkindex]; indexFile.seek(indexPosition * recordSize); String line = indexFile.readLine(); if (line.substring(0, 1).equals(" ")) { // empty record, reset file pointer and fill record indexFile.seek(indexPosition * recordSize); String indexPositionFormatted = String.format("%-5s", Integer.toString(indexPosition)); String tablePositionFormatted = String.format("%-5s", Integer.toString(tablePosition)); String indexRecord = indexPositionFormatted + "# #" + pkValue + "#" + tablePositionFormatted + "#" + "null " + "\r\n"; indexFile.writeBytes(indexRecord); } else { String[] parts = line.split("#"); if (parts[1].equals("D")) { // Deleted record, reset file pointer, fill record but keep previous link ! indexFile.seek(indexPosition * recordSize); String indexPositionFormatted = String.format("%-5s", Integer.toString(indexPosition)); String tablePositionFormatted = String.format("%-5s", Integer.toString(tablePosition)); String indexRecord = indexPositionFormatted + "# #" + pkValue + "#" + tablePositionFormatted; indexFile.writeBytes(indexRecord); } else { // Collision found ! a valid record is found, so add new record at EOF // Calculate new record number int newIndexPosition = (int) (indexFile.length() / recordSize); String newIndexPositionFormatted = String.format("%-5s", Integer.toString(newIndexPosition)); String tablePositionFormatted = String.format("%-5s", Integer.toString(tablePosition)); // reset file pointer and update the current record indexFile.seek((indexPosition * recordSize) + (recordSize - 2 - 5)); indexFile.write(newIndexPositionFormatted.toString().getBytes()); // move file pointer to EOF and append new record indexFile.seek(indexFile.length()); String indexRecord = newIndexPositionFormatted + "# #" + pkValue + "#" + tablePositionFormatted + "#" + "null " + "\r\n"; indexFile.writeBytes(indexRecord); } } }
/** * Go to the beginning of the rtpdump file and skip the first line of ascii (giving the file * version) and skip the file header (useless) * * @throws IOException if an error occur during the seek and reading of the file. */ private void resetFile() throws IOException { stream.seek(0); stream.readLine(); // read the first line that is in ascii stream.seek(stream.getFilePointer() + RtpdumpFileReader.FILE_HEADER_LENGTH); }