List of utility methods to do Text File Read
String | readFile(String filePath) read File return readFile(filePath, true);
|
String | readFile(String path) read File StringBuilder result = new StringBuilder(); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(path)); String buffer; while ((buffer = reader.readLine()) != null) { result.append(buffer + '\n'); reader.close(); } catch (IOException e) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); JOptionPane.showMessageDialog(null, "<html><br>Could not read from file: " + path + "<br>" + "Error: " + e.toString() + "</html>", "Error!", JOptionPane.ERROR_MESSAGE); } catch (Exception ee) { e.printStackTrace(); return result.toString(); |
String | readTextFile(File file, int max, String ellipsis) Read a text file into a String, optionally limiting the length. InputStream input = new FileInputStream(file); try { if (max > 0) { byte[] data = new byte[max + 1]; int length = input.read(data); if (length <= 0) return ""; if (length <= max) ... |
String | readTextFile(final DataInputStream is) Read file as text file return as US-ASCII string byte[] dataBuf = new byte[is.available()]; is.readFully(dataBuf); return new String(dataBuf, "US-ASCII"); |
String | readTextFile(final String path, final String enc) read Text File ByteArrayOutputStream os = new ByteArrayOutputStream(); try { File f = new File(path); if (f.exists()) { FileInputStream is = new FileInputStream(path); try { copyStream(is, os); } finally { ... |
String | readTextFile(InputStream inStream) read Text File String content = ""; try { int ca = inStream.available(); byte[] by = new byte[ca]; inStream.read(by); content = new String(by, encoding); inStream.close(); } catch (FileNotFoundException e) { ... |
String | readTextFile(String fileName) read Text File File file = new File(fileName); FileInputStream fis = new FileInputStream(file); byte[] data = new byte[(int) file.length()]; fis.read(data); fis.close(); return new String(data, "UTF-8"); |
String | readTextFile(String fn) read Text File FileInputStream fis = new FileInputStream(fn); int numBytes = fis.available(); byte[] buf = new byte[numBytes]; fis.read(buf); String content = new String(buf); return content; |
String | readTextFileFast(final File file) read Text File Fast try { return new String(readCloseBinaryStream(new FileInputStream(file))); } catch (FileNotFoundException e) { System.err.println("readTextFileFast: File " + file + " not found."); return ""; |