Code example for RandomAccessFile

Methods: close, readLine, seek, sleep

0
	private long lastIdle = 0;
	private long lastCPU = 0;
	 
	private float readUsage() { 
	    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]);
	        long idle2, cpu2;
		    if (lastCPU + lastIdle == 0){
		        try { 
		            Thread.sleep(360);
		        } catch (Exception e) {}
	 
		        reader.seek(0);
		        load = reader.readLine();
		        reader.close();
	 
		        toks = load.split(" ");
	 
		        idle2 = Long.parseLong(toks[5]);
		        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]);
		    } 
		    else{ 
		    	reader.close();
		    	idle2 = idle1;
		    	cpu2 = cpu1;
		    	idle1 = lastIdle;
		    	cpu1 = lastCPU;
		    }