List of usage examples for java.io RandomAccessFile readLine
public final String readLine() throws IOException
From source file:com.netease.qa.emmagee.utils.CpuInfo.java
/** * read stat of each CPU cores/*from w w w .j a v a 2 s . co m*/ */ private void readTotalCpuStat() { try { // monitor total and idle cpu stat of certain process RandomAccessFile cpuInfo = new RandomAccessFile(CPU_STAT, "r"); String line = ""; while ((null != (line = cpuInfo.readLine())) && line.startsWith("cpu")) { String[] toks = line.split("\\s+"); idleCpu.add(Long.parseLong(toks[4])); totalCpu.add(Long.parseLong(toks[1]) + Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4]) + Long.parseLong(toks[6]) + Long.parseLong(toks[5]) + Long.parseLong(toks[7])); } cpuInfo.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.apache.cassandra.db.VerifyTest.java
@Test public void testVerifyIncorrectDigest() throws IOException, WriteTimeoutException { CompactionManager.instance.disableAutoCompaction(); Keyspace keyspace = Keyspace.open(KEYSPACE); ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CORRUPT_CF); fillCF(cfs, KEYSPACE, CORRUPT_CF, 2); List<Row> rows = cfs.getRangeSlice(Util.range("", ""), null, new IdentityQueryFilter(), 1000); SSTableReader sstable = cfs.getSSTables().iterator().next(); RandomAccessFile file = new RandomAccessFile(sstable.descriptor.filenameFor(Component.DIGEST), "rw"); Long correctChecksum = Long.parseLong(file.readLine()); file.close();/*from w ww .ja v a2 s.c o m*/ writeChecksum(++correctChecksum, sstable.descriptor.filenameFor(Component.DIGEST)); Verifier verifier = new Verifier(cfs, sstable, false); try { verifier.verify(false); fail("Expected a CorruptSSTableException to be thrown"); } catch (CorruptSSTableException err) { } }
From source file:com.netease.qa.emmagee.utils.CpuInfo.java
/** * get CPU name./* ww w . j a v a 2 s . c o m*/ * * @return CPU name */ public String getCpuName() { try { RandomAccessFile cpuStat = new RandomAccessFile(CPU_INFO_PATH, "r"); // check cpu type String line; while (null != (line = cpuStat.readLine())) { String[] values = line.split(":"); if (values[0].contains(INTEL_CPU_NAME) || values[0].contains("Processor")) { cpuStat.close(); Log.d(LOG_TAG, "CPU name=" + values[1]); return values[1]; } } } catch (IOException e) { Log.e(LOG_TAG, "IOException: " + e.getMessage()); } return ""; }
From source file:com.piaoyou.util.FileUtil.java
public static String[] getLastLines(File file, int linesToReturn) throws IOException, FileNotFoundException { final int AVERAGE_CHARS_PER_LINE = 250; final int BYTES_PER_CHAR = 2; RandomAccessFile randomAccessFile = null; StringBuffer buffer = new StringBuffer(linesToReturn * AVERAGE_CHARS_PER_LINE); int lineTotal = 0; try {/*from w w w. j a va 2s.c o m*/ randomAccessFile = new RandomAccessFile(file, "r"); long byteTotal = randomAccessFile.length(); long byteEstimateToRead = linesToReturn * AVERAGE_CHARS_PER_LINE * BYTES_PER_CHAR; long offset = byteTotal - byteEstimateToRead; if (offset < 0) { offset = 0; } randomAccessFile.seek(offset); //log.debug("SKIP IS ::" + offset); String line = null; String lineUTF8 = null; while ((line = randomAccessFile.readLine()) != null) { lineUTF8 = new String(line.getBytes("ISO8859_1"), "UTF-8"); lineTotal++; buffer.append(lineUTF8).append('\n'); } } finally { if (randomAccessFile != null) { try { randomAccessFile.close(); } catch (IOException ex) { } } } String[] resultLines = new String[linesToReturn]; BufferedReader in = null; try { in = new BufferedReader(new StringReader(buffer.toString())); int start = lineTotal /* + 2 */ - linesToReturn; // Ex : 55 - 10 = 45 ~ offset if (start < 0) start = 0; // not start line for (int i = 0; i < start; i++) { in.readLine(); // loop until the offset. Ex: loop 0, 1 ~~ 2 lines } int i = 0; String line = null; while ((line = in.readLine()) != null) { resultLines[i] = line; i++; } } catch (IOException ie) { log.error("Error" + ie); throw ie; } finally { if (in != null) { try { in.close(); } catch (IOException ex) { } } } return resultLines; }
From source file:com.datasayer.meerkat.MeerJobRunner.java
@SuppressWarnings("unchecked") @Override//from ww w . j a v a2 s. co m public void bsp(final BSPPeer<Writable, Writable, Writable, Writable, Writable> peer) throws IOException, SyncException, InterruptedException { while (true) { try { long currentTime = System.currentTimeMillis(); FileSystem fs = FileSystem.get(conf); if (!fs.isFile(logPath)) { System.out.println("can not read input file"); return; } RandomAccessFile file = new RandomAccessFile(logPath.toString(), "r"); long fileLength = file.length(); if (fileLength > filePointer) { file.seek(filePointer); String line = null; while (file.length() > file.getFilePointer()) { line = file.readLine(); line = new String(line.getBytes("8859_1"), "utf-8"); guardMeer.observe(line); } filePointer = file.getFilePointer(); } else { // nothing to do } file.close(); long timeDiff = currentTime - this.lastAggregatedTime; if (timeDiff >= this.aggregationInterval) { peer.sync(); if (peer.getPeerName().equals(masterName)) { bossMeer.masterCompute(new Iterator<Writable>() { private final int producedMessages = peer.getNumCurrentMessages(); private int consumedMessages = 0; @Override public boolean hasNext() { return producedMessages > consumedMessages; } @Override public Writable next() throws NoSuchElementException { if (consumedMessages >= producedMessages) { throw new NoSuchElementException(); } try { consumedMessages++; return peer.getCurrentMessage(); } catch (IOException e) { throw new NoSuchElementException(); } } @Override public void remove() { // BSPPeer.getCurrentMessage originally deletes a message. // Thus, it doesn't need to throw exception. // throw new UnsupportedOperationException(); } }, signalMeer); this.lastAggregatedTime = currentTime; } } } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.jraf.irondad.handler.pixgame.PixGameHandler.java
public String getRandomWord(HandlerContext handlerContext) throws IOException { RandomAccessFile file = new RandomAccessFile( ((PixGameHandlerConfig) handlerContext.getHandlerConfig()).getDictPath(), "r"); file.seek((long) (Math.random() * file.length())); // Eat the characters of the current line to go to beginning of the next line file.readLine(); // Now read and return the line return file.readLine(); }
From source file:com.netease.qa.emmagee.utils.CpuInfo.java
/** * read the status of CPU./*from www. j av a2 s .co m*/ * * @throws FileNotFoundException */ public void readCpuStat() { String processPid = Integer.toString(pid); String cpuStatPath = "/proc/" + processPid + "/stat"; try { // monitor cpu stat of certain process RandomAccessFile processCpuInfo = new RandomAccessFile(cpuStatPath, "r"); String line = ""; StringBuffer stringBuffer = new StringBuffer(); stringBuffer.setLength(0); while ((line = processCpuInfo.readLine()) != null) { stringBuffer.append(line + "\n"); } String[] tok = stringBuffer.toString().split(" "); processCpu = Long.parseLong(tok[13]) + Long.parseLong(tok[14]); processCpuInfo.close(); } catch (FileNotFoundException e) { Log.w(LOG_TAG, "FileNotFoundException: " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); } readTotalCpuStat(); }
From source file:hydrograph.ui.perspective.dialog.PreStartActivity.java
private boolean updateINIOnJDKUpgrade(String javaHome) { logger.debug("Updating INI file if JDK path is updated"); RandomAccessFile file = null; boolean isUpdated = false; try {/* ww w. ja v a2s .c om*/ file = new RandomAccessFile(new File(HYDROGRAPH_INI), "rw"); byte[] text = new byte[(int) file.length()]; while (file.getFilePointer() != file.length()) { if (StringUtils.equals(file.readLine(), "-vm")) { String currentLine = file.readLine(); if (StringUtils.equals(currentLine, javaHome)) { logger.debug("JAVA_HOME and -vm in configuration file are same"); } else { logger.debug( "JAVA_HOME and -vm in configuration file are different. Updating configuration file with JAVA_HOME"); file.seek(0); file.readFully(text); file.seek(0); updateData(text, javaHome, currentLine, file); isUpdated = true; } break; } } } catch (IOException ioException) { logger.error("IOException occurred while updating " + HYDROGRAPH_INI + " file", ioException); } finally { try { if (file != null) { file.close(); } } catch (IOException ioException) { logger.error("IOException occurred while closing " + HYDROGRAPH_INI + " file", ioException); } } return isUpdated; }
From source file:org.ms123.common.git.GitServiceImpl.java
protected static String getFileType(File file) { if (file.isDirectory()) { return "sw.directory"; } else if (file.toString().endsWith(".txt")) { return "text/plain"; } else if (file.toString().endsWith(".jpeg") || file.toString().endsWith(".jpg")) { return "image/jpeg"; } else if (file.toString().endsWith(".png")) { return "image/png"; } else if (file.toString().endsWith(".svg")) { return "image/svg+xml"; } else if (file.toString().endsWith(".xml")) { return "text/xml"; } else if (file.toString().endsWith(".woff") || file.toString().endsWith(".woff.gz")) { return "application/x-font-woff"; } else if (file.toString().endsWith(".js") || file.toString().endsWith(".js.gz")) { return "text/javascript"; } else if (file.toString().endsWith(".adoc") || file.toString().endsWith(".adoc.gz")) { return "text/x-asciidoc"; } else if (file.toString().endsWith(".html") || file.toString().endsWith(".html.gz")) { return "text/html"; } else if (file.toString().endsWith(".css") || file.toString().endsWith(".css.gz")) { return "text/css"; } else if (file.toString().endsWith(".yaml") || file.toString().endsWith(".yml")) { return "text/x-yaml"; } else if (file.toString().endsWith(".json") || file.toString().endsWith(".json.gz")) { return "application/json"; } else if (file.toString().endsWith(".odt")) { return "application/vnd.oasis.opendocument.text"; } else if (file.toString().endsWith(".groovy")) { return "sw.groovy"; } else if (file.toString().endsWith(".java")) { return "sw.java"; }/* w ww .j a va2 s .com*/ RandomAccessFile r = null; try { int lnr = 0; r = new RandomAccessFile(file, "r"); int i = r.readInt(); if (i == 1534293853) { r.seek(0); while (true) { String line = r.readLine(); if (line == null) { break; } if (lnr == 0 && !line.startsWith("[sw]")) break; if (line.trim().startsWith("type")) { int eq = line.indexOf("="); if (eq != -1) { return line.substring(eq + 1).trim(); } } lnr++; if (lnr > 20) break; } } else if (i == -2555936) { return "image/jpeg"; } else if (i == -1991225785) { return "image/png"; } } catch (Exception e) { return "sw.unknown"; } finally { closeFile(r); } return detectFileType(file); }
From source file:com.landenlabs.all_devtool.ConsoleFragment.java
/** * Gets total system cpu usage (not just this app) * @return//from w w w . ja v a 2 s . c o m */ private float getCpuUsage() { try { RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r"); String load = reader.readLine(); String[] toks = load.split(" "); long idle1 = Long.parseLong(toks[5]); long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4]) + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]); try { Thread.sleep(360); } catch (Exception e) { } reader.seek(0); load = reader.readLine(); reader.close(); toks = load.split(" "); long idle2 = Long.parseLong(toks[5]); long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4]) + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]); return (float) (cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1)); } catch (IOException ex) { ex.printStackTrace(); } return 0; }