Java Code Examples for java.io.RandomAccessFile#readLine()
The following examples show how to use
java.io.RandomAccessFile#readLine() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CPUStatistic.java From VIA-AI with MIT License | 6 votes |
public int printCpuUsages() { try { RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r"); String load = reader.readLine(); while (load != null) { Log.d("CPU", "CPU usage: " + load); load = reader.readLine(); } } catch (IOException ex) { // ex.printStackTrace(); return 0; } return 0; }
Example 2
Source File: PipeTailer.java From bidder with Apache License 2.0 | 6 votes |
/** * Run until interrupted. */ public void run() { try { while (!me.isInterrupted()) { File f_pipe = new File(fileName); RandomAccessFile raf = new RandomAccessFile(f_pipe, "r");//p.1 String line = null; for (; ; ) { line = raf.readLine(); //Take care to check the line - //it is null when the pipe has no more available data. if (line != null) { handler.handle(line); } else { break; //stop reading loop } } } } catch (Exception error) { error.printStackTrace(); } }
Example 3
Source File: PartFilesReassembly.java From p3-batchrefine with Apache License 2.0 | 6 votes |
private static long findRDFNameSpaceEnd(RandomAccessFile file) throws IOException { long filePosition = 0; String line; int count = 0; while ((line = file.readLine()) != null) { if (line.trim().startsWith("<rdf:")) count++; if (count == 2) break; filePosition = file.getFilePointer(); } return filePosition; }
Example 4
Source File: PartFilesReassembly.java From p3-batchrefine with Apache License 2.0 | 6 votes |
private static long findRDFNameSpaceEnd(RandomAccessFile file) throws IOException { long filePosition = 0; String line; int count = 0; while ((line = file.readLine()) != null) { if (line.trim().startsWith("<rdf:")) count++; if (count == 2) break; filePosition = file.getFilePointer(); } return filePosition; }
Example 5
Source File: PartFilesReassembly.java From p3-batchrefine with Apache License 2.0 | 6 votes |
private static long findClosingRDFBracket(RandomAccessFile file) throws IOException { long length = file.length(); file.seek((long) (length * 0.9)); long lastLinePosition = -1; String lastLine = ""; while (!lastLine.trim().equals("</rdf:RDF>")) { lastLinePosition = file.getFilePointer(); lastLine = file.readLine(); if (lastLine == null) throw new IOException( "Malformed RDF, last line is not an RDF closing bracket: " + lastLine); } return lastLinePosition; }
Example 6
Source File: Cpu.java From batteryhub with Apache License 2.0 | 6 votes |
public static synchronized long[] readUsagePoint() { long idle = 0; long cpu = 0; try { RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r"); String load = reader.readLine(); String[] tokens = load.split(" "); for (int i = 2; i <= 8; i++) { // 5 index has idle value if (i == 5) { idle = Long.parseLong(tokens[i]); continue; } cpu += Long.parseLong(tokens[i]); } reader.close(); } catch (IOException e) { e.printStackTrace(); } return new long[]{idle, cpu}; }
Example 7
Source File: DxfGroup.java From hortonmachine with GNU General Public License v3.0 | 5 votes |
public static DxfGroup readGroup( RandomAccessFile raf ) throws IOException { try { long pos = raf.getFilePointer(); String line1 = raf.readLine(); String line2 = raf.readLine(); DxfGroup dxfGroup = new DxfGroup(Integer.parseInt(line1.trim()), line2); dxfGroup.setAddress(pos); return dxfGroup; } catch (IOException ioe) { raf.close(); throw ioe; } }
Example 8
Source File: ChunkSplit.java From p3-batchrefine with Apache License 2.0 | 5 votes |
private long findNextEOLPosition(RandomAccessFile seeakable, long blocksize) throws IOException, EndReachedException { if (seeakable.skipBytes((int) blocksize) < blocksize) throw new EndReachedException(); else seeakable.readLine(); return seeakable.getFilePointer(); }
Example 9
Source File: ReadFileUsingRandomAccessFile.java From journaldev with MIT License | 5 votes |
public static void main(String[] args) { try { RandomAccessFile file = new RandomAccessFile("/Users/pankaj/Downloads/myfile.txt", "r"); String str; while ((str = file.readLine()) != null) { System.out.println(str); } file.close(); } catch (IOException e) { e.printStackTrace(); } }
Example 10
Source File: LoadConfig.java From Introspy-Android with GNU General Public License v2.0 | 5 votes |
private String readFirstLineOfFile(String fn) { String lineData = ""; try{ RandomAccessFile inFile = new RandomAccessFile(fn, "r"); lineData = inFile.readLine(); inFile.close(); } // file not found catch(Exception e){ // Log.i("IntrospyLog", "--> "+ e); // app won't be hooked } return lineData; }
Example 11
Source File: StorageUtils.java From FireFiles with Apache License 2.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private long getSizeTotalRAM(boolean isTotal) { long sizeInBytes = 1000; MemoryInfo mi = new MemoryInfo(); activityManager.getMemoryInfo(mi); if(isTotal) { try { if(Utils.hasJellyBean()){ long totalMegs = mi.totalMem; sizeInBytes = totalMegs; } else{ RandomAccessFile reader = new RandomAccessFile("/proc/meminfo", "r"); String load = reader.readLine(); String[] totrm = load.split(" kB"); String[] trm = totrm[0].split(" "); sizeInBytes=Long.parseLong(trm[trm.length-1]); sizeInBytes=sizeInBytes*1024; reader.close(); } } catch (Exception e) { } } else{ long availableMegs = mi.availMem; sizeInBytes = availableMegs; } return sizeInBytes; }
Example 12
Source File: CpuUtils.java From StickyDecoration with Apache License 2.0 | 5 votes |
public static double getCpuUsage0() { try { RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r"); String load = reader.readLine(); String[] toks = load.split(" "); double idle1 = Double.parseDouble(toks[5]); double cpu1 = Double.parseDouble(toks[2]) + Double.parseDouble(toks[3]) + Double.parseDouble(toks[4]) + Double.parseDouble(toks[6]) + Double.parseDouble(toks[8]) + Double.parseDouble(toks[7]); // 2:user 3:nice 4:system 6:iowait 7:irq 8:softirq try { Thread.sleep(360); } catch (Exception e) { e.printStackTrace(); } reader.seek(0); load = reader.readLine(); reader.close(); toks = load.split(" "); double idle2 = Double.parseDouble(toks[5]); double cpu2 = Double.parseDouble(toks[2]) + Double.parseDouble(toks[3]) + Double.parseDouble(toks[4]) + Double.parseDouble(toks[6]) + Double.parseDouble(toks[8]) + Double.parseDouble(toks[7]); double value = div((100.00 * ((cpu2 - cpu1))), (cpu2 + idle2) - (cpu1 + idle1), 2); // BigDecimal b = new BigDecimal(Double.toString(value)); // double res = b.setScale(2, // BigDecimal.ROUND_HALF_UP).doubleValue(); return value; } catch (IOException ex) { ex.printStackTrace(); } return 0; }
Example 13
Source File: DataMapStore.java From COLA with GNU Lesser General Public License v2.1 | 5 votes |
private String readLine(RandomAccessFile raf) throws IOException { String line = raf.readLine(); if(StringUtils.isBlank(line)){ return line; } line = new String(line.getBytes("ISO-8859-1"), "utf-8"); line = StringUtils.trim(line); return line; }
Example 14
Source File: ByteStore.java From COLA with GNU Lesser General Public License v2.1 | 5 votes |
private List<MockData> loadDataFileHead(RandomAccessFile rf) throws IOException { List<MockData> mockDataList = new ArrayList<>(); long fileLength = rf.length(); // 返回此文件中的当前偏移量 long start = rf.getFilePointer(); long readIndex = start + fileLength -1; String line; // 设置偏移量为文件末尾 rf.seek(readIndex); int c = -1; while (readIndex > start) { c = rf.read(); String readText = null; if (c == '\n' || c == '\r') { line = rf.readLine(); if (line != null) { readText = new String(line.getBytes("ISO-8859-1")); } if(StringUtils.isBlank(readText)){ continue; } if(Constants.RESPONSE_DATA_DELIMITER.equals(readText)){ break; }else{ MockData mockData = createMockDataHead(readText); mockDataList.add(mockData); } readIndex--; } readIndex--; rf.seek(readIndex); } return mockDataList; }
Example 15
Source File: GPSTesterActivityController.java From android-gps-test-tool with Apache License 2.0 | 5 votes |
/** * Returns CPU usage info. Careful using this it's CPU intensive by itself! * @return Percentage */ private float getCPU() { 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; }
Example 16
Source File: TestFileLineIndex.java From bboxdb with Apache License 2.0 | 5 votes |
/** * Read a line from the index * @param pos * @param expected * @throws IOException */ protected void readLineFomIndex(final RandomAccessFile file, final FileLineIndex fli, final long lineNumber, final String expected) throws IOException { final long pos = fli.locateLine(lineNumber); file.seek(pos); final String line = file.readLine(); Assert.assertEquals(expected, line); }
Example 17
Source File: Storage.java From hadoop with Apache License 2.0 | 4 votes |
/** * Attempts to acquire an exclusive lock on the storage. * * @return A lock object representing the newly-acquired lock or * <code>null</code> if storage is already locked. * @throws IOException if locking fails. */ @SuppressWarnings("resource") FileLock tryLock() throws IOException { boolean deletionHookAdded = false; File lockF = new File(root, STORAGE_FILE_LOCK); if (!lockF.exists()) { lockF.deleteOnExit(); deletionHookAdded = true; } RandomAccessFile file = new RandomAccessFile(lockF, "rws"); String jvmName = ManagementFactory.getRuntimeMXBean().getName(); FileLock res = null; try { res = file.getChannel().tryLock(); if (null == res) { throw new OverlappingFileLockException(); } file.write(jvmName.getBytes(Charsets.UTF_8)); LOG.info("Lock on " + lockF + " acquired by nodename " + jvmName); } catch(OverlappingFileLockException oe) { // Cannot read from the locked file on Windows. String lockingJvmName = Path.WINDOWS ? "" : (" " + file.readLine()); LOG.error("It appears that another node " + lockingJvmName + " has already locked the storage directory: " + root, oe); file.close(); return null; } catch(IOException e) { LOG.error("Failed to acquire lock on " + lockF + ". If this storage directory is mounted via NFS, " + "ensure that the appropriate nfs lock services are running.", e); file.close(); throw e; } if (!deletionHookAdded) { // If the file existed prior to our startup, we didn't // call deleteOnExit above. But since we successfully locked // the dir, we can take care of cleaning it up. lockF.deleteOnExit(); } return res; }
Example 18
Source File: PegasusSubmitDAG.java From pegasus with Apache License 2.0 | 4 votes |
/** * Modifies the dagman condor submit file for metrics reporting. * * @param file * @return true if file is modified, else false * @throws CodeGeneratorException */ protected boolean modifyDAGManSubmitFileForMetrics(File file) throws CodeGeneratorException { // modify the environment string to add the environment for // enabling DAGMan metrics if so required. Metrics metricsReporter = new Metrics(); metricsReporter.initialize(mBag); ENV env = metricsReporter.getDAGManMetricsEnv(); if (env.isEmpty()) { return false; } else { // we read the DAGMan submit file in and grab the environment from it // and add the environment key to the second last line with the // Pegasus metrics environment variables added. try { RandomAccessFile raf = new RandomAccessFile(file, "rw"); String dagmanEnvString = ""; String line = null; long previous = raf.getFilePointer(); while ((line = raf.readLine()) != null) { if (line.startsWith("environment")) { dagmanEnvString = line; } if (line.startsWith("queue")) { // backtrack to previous file position i.e just before queue raf.seek(previous); StringBuilder dagmanEnv = new StringBuilder(dagmanEnvString); if (dagmanEnvString.isEmpty()) { dagmanEnv.append("environment="); } else { dagmanEnv.append(";"); } for (Iterator it = env.getProfileKeyIterator(); it.hasNext(); ) { String key = (String) it.next(); dagmanEnv.append(key).append("=").append(env.get(key)).append(";"); } mLogger.log( "Updated environment for dagman is " + dagmanEnv.toString(), LogManager.DEBUG_MESSAGE_LEVEL); raf.writeBytes(dagmanEnv.toString()); raf.writeBytes(System.getProperty("line.separator", "\r\n")); raf.writeBytes("queue"); break; } previous = raf.getFilePointer(); } raf.close(); } catch (IOException e) { throw new CodeGeneratorException( "Error while reading dagman .condor.sub file " + file, e); } } return true; }
Example 19
Source File: Storage.java From big-c with Apache License 2.0 | 4 votes |
/** * Attempts to acquire an exclusive lock on the storage. * * @return A lock object representing the newly-acquired lock or * <code>null</code> if storage is already locked. * @throws IOException if locking fails. */ @SuppressWarnings("resource") FileLock tryLock() throws IOException { boolean deletionHookAdded = false; File lockF = new File(root, STORAGE_FILE_LOCK); if (!lockF.exists()) { lockF.deleteOnExit(); deletionHookAdded = true; } RandomAccessFile file = new RandomAccessFile(lockF, "rws"); String jvmName = ManagementFactory.getRuntimeMXBean().getName(); FileLock res = null; try { res = file.getChannel().tryLock(); if (null == res) { throw new OverlappingFileLockException(); } file.write(jvmName.getBytes(Charsets.UTF_8)); LOG.info("Lock on " + lockF + " acquired by nodename " + jvmName); } catch(OverlappingFileLockException oe) { // Cannot read from the locked file on Windows. String lockingJvmName = Path.WINDOWS ? "" : (" " + file.readLine()); LOG.error("It appears that another node " + lockingJvmName + " has already locked the storage directory: " + root, oe); file.close(); return null; } catch(IOException e) { LOG.error("Failed to acquire lock on " + lockF + ". If this storage directory is mounted via NFS, " + "ensure that the appropriate nfs lock services are running.", e); file.close(); throw e; } if (!deletionHookAdded) { // If the file existed prior to our startup, we didn't // call deleteOnExit above. But since we successfully locked // the dir, we can take care of cleaning it up. lockF.deleteOnExit(); } return res; }
Example 20
Source File: FileIOTest.java From ignite with Apache License 2.0 | 3 votes |
/** * @throws Exception If failed. */ @Test public void testReadLineFromBinaryFile() throws Exception { File file = new File(FILE_PATH); file.deleteOnExit(); RandomAccessFile raf = new RandomAccessFile(file, "rw"); byte[] b = new byte[100]; Arrays.fill(b, (byte)10); raf.write(b); raf.writeBytes("swap-spaces/space1/b53b3a3d6ab90ce0268229151c9bde11|" + "b53b3a3d6ab90ce0268229151c9bde11|1315392441288" + U.nl()); raf.writeBytes("swap-spaces/space1/b53b3a3d6ab90ce0268229151c9bde11|" + "b53b3a3d6ab90ce0268229151c9bde11|1315392441288" + U.nl()); raf.write(b); raf.writeBytes("test" + U.nl()); raf.getFD().sync(); raf.seek(0); while (raf.getFilePointer() < raf.length()) { String s = raf.readLine(); X.println("String: " + s + ";"); X.println("String length: " + s.length()); X.println("File pointer: " + raf.getFilePointer()); } }