List of usage examples for java.io FileReader FileReader
public FileReader(FileDescriptor fd)
From source file:Main.java
public static int removeUnusedLines(String filename, SortedSet<Integer> linesSet) { try {//w w w . ja va2 s . c om BufferedReader br = new BufferedReader(new FileReader(filename)); //String buffer to store contents of the file StringBuffer sb = new StringBuffer(""); int lineNumber = 1; String line; Iterator iterator = linesSet.iterator(); int lineNumberToBeDeleted = (int) iterator.next(); int count = 0; while ((line = br.readLine()) != null) { if (lineNumber == lineNumberToBeDeleted) { if (iterator.hasNext()) { lineNumberToBeDeleted = (int) iterator.next(); count++; } } else { sb.append(line + "\n"); } lineNumber++; } FileWriter fw = new FileWriter(new File(filename)); //Write entire string buffer into the file fw.write(sb.toString()); fw.close(); System.err.println("deleted lines" + count); } catch (Exception e) { System.err.println("error:" + e.getMessage()); e.printStackTrace(); return -10; } return 10; }
From source file:Main.java
public static String getCpuName() { FileReader fr = null;/*from ww w. ja v a 2s. com*/ BufferedReader br = null; try { fr = new FileReader("/proc/cpuinfo"); br = new BufferedReader(fr); String text = br.readLine(); String[] array = text.split(":\\s+", 2); for (int i = 0; i < array.length; i++) { } return array[1]; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fr != null) try { fr.close(); } catch (IOException e) { e.printStackTrace(); } if (br != null) try { br.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
From source file:Main.java
private static String readFileAsString(File file) throws IOException { String line;/*from w ww . ja v a 2s . c o m*/ StringBuffer sb = new StringBuffer(); BufferedReader br = new BufferedReader(new FileReader(file)); while ((line = br.readLine()) != null) { sb.append(line); } br.close(); return sb.toString(); }
From source file:Main.java
public static int getLineCount(String path) { LineNumberReader lnr = null;/*from w w w . j a v a 2 s . c o m*/ try { lnr = new LineNumberReader(new FileReader(new File(path))); lnr.skip(Long.MAX_VALUE); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (lnr == null) return 0; return lnr.getLineNumber(); }
From source file:Main.java
static Set<Pattern> getFilterFromFile(String filename) { try {//from w w w . j a v a 2 s . com BufferedReader br = new BufferedReader(new FileReader(filename)); Set<Pattern> filter = new HashSet<>(); String line; while ((line = br.readLine()) != null) { if (line.trim().equals("")) { continue; } filter.add(Pattern.compile(line.trim().toLowerCase())); } return filter; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static String getMacFromArpCache(String ip) { if (ip == null) return null; BufferedReader br = null;//from www.j a v a 2 s . c om try { br = new BufferedReader(new FileReader("/proc/net/arp")); String line; while ((line = br.readLine()) != null) { String[] splitted = line.split(" +"); if (splitted != null && splitted.length >= 4 && ip.equals(splitted[0])) { // Basic sanity check String mac = splitted[3]; if (mac.matches("..:..:..:..:..:..")) { return mac; } else { return null; } } } } catch (Exception e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
From source file:Utils.java
public static List<String> readLines(File file) throws Exception { if (!file.exists()) { return new ArrayList<String>(); }// w w w . jav a2 s. c o m BufferedReader reader = new BufferedReader(new FileReader(file)); List<String> results = new ArrayList<String>(); String line = reader.readLine(); while (line != null) { results.add(line); line = reader.readLine(); } return results; }
From source file:Main.java
public static String read(File file) { if (!file.exists()) { return ""; }//from w w w .j a v a 2 s . c o m try { FileReader reader = new FileReader(file); BufferedReader br = new BufferedReader(reader); StringBuffer buffer = new StringBuffer(); String s; while ((s = br.readLine()) != null) { buffer.append(s); } return buffer.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; }
From source file:Main.java
public static StringBuilder ReadFileToString(String filename) { StringBuilder text = new StringBuilder(); try {/*from ww w . ja v a2 s .c om*/ BufferedReader bProcFS = new BufferedReader(new FileReader(filename)); String readLine = null; while ((readLine = bProcFS.readLine()) != null) { text.append(readLine); text.append("\n"); } bProcFS.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return text; }
From source file:Main.java
public static String readFileContents(File file) throws IOException { StringBuilder sb = new StringBuilder(); BufferedReader br = null;/*from ww w . ja v a 2 s.c om*/ try { br = new BufferedReader(new FileReader(file)); String currentLine; while ((currentLine = br.readLine()) != null) { sb.append(currentLine); sb.append('\n'); } } finally { if (br != null) { br.close(); } } return sb.toString(); }