List of usage examples for java.io RandomAccessFile readLine
public final String readLine() throws IOException
From source file:org.xwalk.runtime.extension.api.device_capabilities.DeviceCapabilitiesMemory.java
private long getTotalMemFromFile() { long capacity = 0; RandomAccessFile file = null; try {/*ww w .ja va 2 s . c om*/ file = new RandomAccessFile(MEM_INFO_FILE, "r"); String line = file.readLine(); String[] arrs = line.split(":"); if (!arrs[0].equals("MemTotal")) { return 0; } String[] values = arrs[1].trim().split("\\s+"); capacity = Long.parseLong(values[0]) * 1024; } catch (IOException e) { capacity = 0; Log.e(TAG, e.toString()); } finally { try { if (file != null) { file.close(); } } catch (IOException e) { Log.e(TAG, e.toString()); } } return capacity; }
From source file:net.naijatek.myalumni.util.utilities.FileUtil.java
public static String[] getLastLines(final File file, final int linesToReturn, final String logType) 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 . ja v a 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]; ArrayList<String> resultLines = new ArrayList<String>(); 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) { if (logType.equalsIgnoreCase("ALL")) { //resultLines[i] = line; resultLines.add(line); i++; } else if (line.indexOf(logType) > -1) { //resultLines[i] = line; resultLines.add(line); i++; } } } catch (IOException ie) { logger.error("Error" + ie); throw ie; } finally { if (in != null) { try { in.close(); } catch (IOException ex) { } } } String[] resultLines1 = new String[resultLines.size()]; String tmp = new String(); for (int i = 0; i < resultLines.size(); i++) { tmp = (String) resultLines.get(i); resultLines1[i] = tmp; } return resultLines1; }
From source file:titutorial.sysinfo.SysteminfoModule.java
@Kroll.method public String getTotalRAM() { RandomAccessFile reader = null; String load = null;// w w w . ja v a 2s . co m try { reader = new RandomAccessFile("/proc/meminfo", "r"); load = reader.readLine(); load = load.replace("MemTotal:", ""); load = load.replaceAll("\\s+", " "); } catch (IOException ex) { ex.printStackTrace(); } finally { // Streams.close(reader); } return load; }
From source file:pl.wasko.utils.xlsxtract.MainFrame.java
private String[] readMainCfgFile(File selectedFile) throws FileNotFoundException, IOException { RandomAccessFile raf = new RandomAccessFile(selectedFile, "r"); List<String> mainCfgData = new ArrayList<>(); String line;/*from w w w . j a v a 2s . co m*/ while ((line = raf.readLine()) != null) { String[] cline = line.split("="); if (cline.length != 2) { System.err.println("niepoprawna linai w glownym pliku konfiguracyjnym: " + line); } else { mainCfgData.add("Typrek: " + cline[0] + ", " + cline[1]); } } return mainCfgData.toArray(new String[mainCfgData.size()]); }
From source file:org.openoverlayrouter.noroot.logActivity.java
public void refresh() { StringBuffer contents = new StringBuffer(); final StringBuffer fixedContents = contents; try {//w w w . jav a 2 s . c om RandomAccessFile logFile = new RandomAccessFile(log_file, "r"); if (logFile.length() > maxReadBytes) { logFile.seek(logFile.length() - maxReadBytes); } String currentLine = logFile.readLine(); while (currentLine != null) { if (currentLine != null) { contents.append(currentLine); contents.append('\n'); } currentLine = logFile.readLine(); } try { if (logFile != null) { logFile.close(); } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } mHandler.post(new Runnable() { public void run() { // Put the file contents into the TextView TextView log = (TextView) llLayout.findViewById(R.id.logView); log.setText(fixedContents); // Auto scroll to the bottom final ScrollView scroll = (ScrollView) llLayout.findViewById(R.id.scrollView1); scroll.post(new Runnable() { public void run() { scroll.fullScroll(View.FOCUS_DOWN); } }); if (myDialog != null) { myDialog.dismiss(); myDialog = null; } } }); }
From source file:org.xdi.util.FileUtil.java
public long findLastPosition(String filePath, String searchStr) { try {/*from w w w .j a v a 2s.co m*/ File f = new File(filePath); RandomAccessFile raf; raf = new RandomAccessFile(f, "r"); long position = -1; while (raf.getFilePointer() < raf.length()) { String line = raf.readLine(); if (line.contains(searchStr)) { position = raf.getFilePointer() + line.indexOf(searchStr) - line.length(); continue; } } return position; } catch (FileNotFoundException e) { e.printStackTrace(); return -1; } catch (IOException e) { e.printStackTrace(); return -1; } }
From source file:org.xdi.util.FileUtil.java
/** * returns the first occurrence of specified string in a file. * /* w ww . j av a 2s. c o m*/ * @param filePath * @param searchStr * @return */ public long findFirstPosition(String filePath, String searchStr) { try { File f = new File(filePath); RandomAccessFile raf; raf = new RandomAccessFile(f, "r"); long position = -1; while (raf.getFilePointer() < raf.length()) { String line = raf.readLine(); if (line.contains(searchStr)) { position = raf.getFilePointer() + line.indexOf(searchStr) - (line.length() + 1); break; } } return position; } catch (FileNotFoundException e) { e.printStackTrace(); return -1; } catch (IOException e) { e.printStackTrace(); return -1; } }
From source file:com.baidu.terminator.manager.service.LogServiceImpl.java
@Override public Log readLog(int linkId, long offset) throws IOException { String logFileLocation = LinkLogger.getLogFileLocation(linkId); FileUtils.createFile(logFileLocation); RandomAccessFile raf = null; List<String> lines = new ArrayList<String>(); long length = 0; try {/*w w w . ja v a 2 s. co m*/ raf = new RandomAccessFile(logFileLocation, "r"); raf.seek(offset); length = raf.length(); long point = raf.getFilePointer(); while (point < length) { String line = raf.readLine(); String utf8Line = new String(line.getBytes("8859_1"), "utf-8"); lines.add(utf8Line); if (point - offset >= MAX_READ_BYTES) { length = point; break; } point = raf.getFilePointer(); } } finally { if (raf != null) { raf.close(); } } Log log = new Log(); log.setLogLocation(logFileLocation); log.setOffset(length); log.setContent(lines); return log; }
From source file:mediathekplugin.Database.java
public ArrayList<MediathekProgramItem> getMediathekPrograms(final Program program) { ArrayList<MediathekProgramItem> result = new ArrayList<MediathekProgramItem>(); String channelName = unifyChannelName(program.getChannel().getName()); HashMap<Long, ArrayList<Integer>> programsMap = mChannelItems.get(channelName); // search parts in brackets like for ARD if (programsMap == null && channelName.contains("(")) { String bracketPart = StringUtils.substringBetween(channelName, "(", ")"); programsMap = mChannelItems.get(bracketPart); }//ww w. j ava 2s. c o m // search for partial name, if full name is not found if (programsMap == null && channelName.contains(" ")) { String firstPart = StringUtils.substringBefore(channelName, " "); programsMap = mChannelItems.get(firstPart); } if (programsMap == null) { for (Entry<String, HashMap<Long, ArrayList<Integer>>> entry : mChannelItems.entrySet()) { if (StringUtils.startsWithIgnoreCase(channelName, entry.getKey())) { programsMap = entry.getValue(); break; } } } if (programsMap == null) { return result; } String title = program.getTitle(); ArrayList<Integer> programs = programsMap.get(getKey(title)); if (programs == null && title.endsWith(")") && title.contains("(")) { String newTitle = StringUtils.substringBeforeLast(title, "(").trim(); programs = programsMap.get(getKey(newTitle)); } if (programs == null && title.endsWith("...")) { String newTitle = title.substring(0, title.length() - 3).trim(); programs = programsMap.get(getKey(newTitle)); } if (programs == null) { return result; } try { RandomAccessFile file = new RandomAccessFile(new File(mFileName), "r"); for (Integer byteOffset : programs) { file.seek(byteOffset); String lineEncoded = file.readLine(); String line = new String(lineEncoded.getBytes(), "UTF-8"); Matcher itemMatcher = ITEM_PATTERN.matcher(line); if (itemMatcher.find()) { String itemTitle = itemMatcher.group(3).trim(); String itemUrl = itemMatcher.group(4).trim(); result.add(new MediathekProgramItem(itemTitle, itemUrl, null)); } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; }
From source file:org.xdi.util.FileUtil.java
/** * Writes data in a file on specified position * // w w w.j a v a 2 s. com * @param filePath * @param position * @param data * @return */ public boolean writeToFile(String filePath, long position, String data) { try { File f = new File(filePath); RandomAccessFile raf; raf = new RandomAccessFile(f, "rw"); raf.seek(position); StringBuilder dataAfterPostion = new StringBuilder(data); while (raf.getFilePointer() < raf.length()) { String line = raf.readLine(); dataAfterPostion.append(line); } raf.seek(position); raf.writeUTF(dataAfterPostion.toString()); raf.close(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } }