List of utility methods to do Text File Read
String[] | readLines(String file) Generic read line method implementation from a file. String line; fr = new FileReader(file); lines = new ArrayList<>(); lr = new LineNumberReader(fr); while ((line = lr.readLine()) != null) { if (!line.startsWith("#") && !line.isEmpty()) { lines.add(line); fr.close(); lr.close(); String[] ret = lines.toArray(new String[0]); lines.clear(); lines = null; fr = null; lr = null; return ret; |
String | readStringFromClasspath(String path, Class c) Reads the entire InputStream and returns its content as a single String final InputStream is = c.getResourceAsStream(path); return readStringFromBufferedReader(createBufferedUtf8Reader(is)); |
String | readStringFromFile(String fileName) Reads the entire file and returns its content as a single String return readStringFromBufferedReader(createBufferedUtf8Reader(fileName));
|
String | readText(String path) read Text InputStream resourceAsStream = FileUtil.class .getResourceAsStream(path); BufferedReader br = new BufferedReader(new InputStreamReader( resourceAsStream, Charset.forName("UTF-8"))); StringBuffer sb = new StringBuffer(); String line = br.readLine(); while (line != null) { sb.append(line).append("\n"); ... |
String | readTextFile(String filePath) read Text File File textFile = new File(filePath); BufferedReader in = null; String text = null; StringBuilder allText = new StringBuilder(""); try { in = new BufferedReader(new FileReader(textFile)); while ((text = in.readLine()) != null) { allText.append(text).append("\n"); ... |
String | readTextFile(String path) Reads the contents of a text file into a String. StringBuilder contents = new StringBuilder(); String lineSep = System.getProperty("line.separator"); BufferedReader input = new BufferedReader(new FileReader(path)); try { String line; while ((line = input.readLine()) != null) { contents.append(line); contents.append(lineSep); ... |
void | setFileResourceText(String path, String content) set File Resource Text BufferedWriter fileWriter = null; try { fileWriter = new BufferedWriter(new FileWriter(path)); fileWriter.write(content); } finally { if (fileWriter != null) { fileWriter.close(); |
String | getContent(String file, String encodType) get Content StringBuffer content = new StringBuffer(); FileInputStream fis = new FileInputStream(file); DataInputStream dis = new DataInputStream(fis); BufferedReader br = new BufferedReader(new InputStreamReader(dis, encodType)); String line = null; if ((line = br.readLine()) != null) { content.append(line); ... |
String | getString(File file) Get string from file if (file == null) throw new NullPointerException("null file"); StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { sb.append(line); sb.append('\n'); ... |
String | readFileSdcard(File file) read File Sdcard String res = ""; FileInputStream fin = new FileInputStream(file); int length = fin.available(); byte[] buffer = new byte[length]; fin.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fin.close(); return res; ... |