How can I read a .txt file in a single java line while keeping line breaks?

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.

+8
java string file
source share
7 answers

How about using the scanner and adding the lines themselves:

 sc = new java.util.Scanner ("sample.txt") while (sc.hasNext ()) { buf.append (sc.nextLine ()); buf.append ("<br />"); } 

I don’t see where you got your long spaces from.

+3
source share

You can read the buffer directly, and then create a line from the buffer:

  File f = new File(filePath); FileInputStream fin = new FileInputStream(f); byte[] buffer = new byte[(int) f.length()]; new DataInputStream(fin).readFully(buffer); fin.close(); String s = new String(buffer, "UTF-8"); 
+3
source share

You can add this code:

 return new String(buffer).replaceAll("(\r\n|\r|\n|\n\r)", "<br>"); 

Is this what you are looking for?

+1
source share

The code will read the contents of the file as they appear in the file, including line breaks. If you want to change the breaks to something else, for example, display in html, etc., you will need to either process it or do it by reading the file line by line. Since you don't want the latter, you can replace your return by following what the transformation should do -

 return (new String(buffer)).replaceAll("\r[\n]?", "<br>"); 
+1
source share
 StringBuilder sb = new StringBuilder(); try { InputStream is = getAssets().open("myfile.txt"); byte[] bytes = new byte[1024]; int numRead = 0; try { while((numRead = is.read(bytes)) != -1) sb.append(new String(bytes, 0, numRead)); } catch(IOException e) { } is.close(); } catch(IOException e) { } 

your String result: String result = sb.toString();

then replace whatever you want in this result .

0
source share

You should try org.apache.commons.io.IOUtils.toString (InputStream) to get the contents of the file as String. There you can pass an InputStream object that you get from

 getAssets().open("xml2json.txt") *<<- belongs to Android, which returns InputStream* 

in your business. To force String to use this:

 String xml = IOUtils.toString((getAssets().open("xml2json.txt"))); 

So,

 String xml = IOUtils.toString(*pass_your_InputStream_object_here*); 
0
source share

I agree with @Sanket Patel's general approach, but using Commons I / O, you'll probably want File Utilities .

So your codeword is as follows:

 String myString = FileUtils.readFileToString(new File(filePath)); 

There is also another version for specifying alternative character encodings.

0
source share

All Articles