List of utility methods to do Text File Read
String | fileAsString(File file) reads a File as one single string. byte[] content = fileAsByteArray(file); if (content == null) return ""; return new String(content); |
String | fileAsString(String fileName) Returns the contents of the specified file as a string. File f = null; BufferedReader reader = null; try { f = new File(fileName); reader = new BufferedReader(new FileReader(f)); StringBuilder result = new StringBuilder(10000); String line = null; while ((line = reader.readLine()) != null) { ... |
String | fileAsString(String filePath) Read a file, putting its contents into a String. String retVal = null; if (filePath == null) { return null; try { FileInputStream fin = new FileInputStream(filePath); return fileAsString(fin); } catch (Exception exc) { ... |
String | fileContentsToString(File errFile) Utility method to read in contents of file and concatenate into String String errFileString = null; BufferedReader br = null; StringBuffer sb = new StringBuffer(); br = new BufferedReader(new FileReader(errFile)); String errLine = br.readLine(); while (errLine != null) { sb.append(errLine); errLine = br.readLine(); ... |
String | fileContentsToString(String file) Read the contents of a file and place them in a string object. String contents = ""; File f = new File(file); if (f.exists()) { try { FileReader fr = new FileReader(f); char[] template = new char[(int) f.length()]; fr.read(template); contents = new String(template); ... |
char[] | getChars(File file) get Chars String str; FileReader reader = new FileReader(file); BufferedReader buffer = new BufferedReader(reader); String line; StringBuilder result = new StringBuilder(); while ((line = buffer.readLine()) != null) { result.append(line).append("\n"); str = result.toString(); buffer.close(); reader.close(); return str.toCharArray(); |
String | getChars(Reader r) Because this method takes a Reader it can be locale-correct by creating a FileReader (for example) that uses UTF-8 or some other charset.
if (r == null) { throw new IllegalArgumentException("Reader cannot be null"); try (StringWriter sw = new StringWriter(10 * 1024)) { char[] c = new char[4096]; while (true) { int read = r.read(c); if (read == -1) { ... |
String | getFileContentAsString(File cpfile) get File Content As String FileReader in = null; try { in = new FileReader(cpfile); } catch (FileNotFoundException e1) { e1.printStackTrace(); char[] buffer = new char[128]; int len; ... |
String | getFileContentAsString(final String filePath) reads file and concatenate content to a string final StringBuffer fileData = new StringBuffer(1000); final BufferedReader reader = new BufferedReader(new FileReader(filePath)); char[] buf = new char[1024]; int numRead = 0; while ((numRead = reader.read(buf)) != -1) { final String readData = String.valueOf(buf, 0, numRead); fileData.append(readData); buf = new char[1024]; ... |
String | getFileContentAsString(InputStream inputStream) Reads the content of a given file as a String and returns it. String data = ""; byte[] buffer = new byte[1024]; int numRead = -1; while (true) { numRead = inputStream.read(buffer); if (numRead == 1024) { data += new String(buffer); } else { ... |