Almost every code sample reads a TXT file in turn and stores it in a String array. I donβt need linear processing , because I consider this an unnecessary waste of resources for my requirements: all I want to do is quickly and efficiently dump the contents of .txt on one line. The method described below, however, with one drawback:
private static String readFileAsString(String filePath) throws java.io.IOException{ byte[] buffer = new byte[(int) new File(filePath).length()]; BufferedInputStream f = null; try { f = new BufferedInputStream(new FileInputStream(filePath)); f.read(buffer); if (f != null) try { f.close(); } catch (IOException ignored) { } } catch (IOException ignored) { System.out.println("File not found or invalid path.");} return new String(buffer); }
... The disadvantage is that line breaks are converted to long spaces, for example.
I want line breaks to be converted from \ n or \ r to <br> (HTML tag).
Thanks in advance.
java string file
slashline
source share