Advertisement
kijato

Java : Read whole file to...

Sep 17th, 2019
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. // https://howtodoinjava.com/java/io/java-read-file-to-string-examples/
  2. // https://stackoverflow.com/questions/19486077/java-fastest-way-to-read-through-text-file-with-2-million-lines
  3. // http://net-informations.com/q/faq/memory.html
  4. // http://www.java2s.com/Tutorials/Java/java.nio.file/Files/Java_Tutorial_Files.htm
  5.  
  6. import java.io.*;
  7. import java.nio.file.*;
  8. import java.nio.charset.Charset;
  9. import java.nio.charset.StandardCharsets;
  10. import java.util.*;
  11. //import org.apache.commons.io.*;
  12.  
  13. public class m {
  14.     public static void main(String[] args) throws InterruptedException {
  15.         try {
  16.             byte[] bytes = Files.readAllBytes(Paths.get(args[0])); //  // a "legjobb" memóriafoglalás: 164 MB -> 340 MB
  17.             //String str = new String(bytes, StandardCharsets.UTF_8);
  18.             //List<String> lines = Files.readAllLines(Paths.get(args[0]),Charset.forName("ISO-8859-2")); // túlzott memóriafoglalás: 164 MB -> 775 MB
  19.             //String str1 = new String ( Files.readAllBytes( Paths.get(args[0]) ) ); // túlzott memóriafoglalás: 164 MB -> 664 MB
  20.             //String str2 = Files.readString(Paths.get(args[0])); // only Java 11
  21.             //String str3 = FileUtils.readFileToString(args[0], "utf-8"); // apache
  22.             //String str4 = new Scanner(new File(args[0])).useDelimiter("\\Z").next(); // irreális memóriafoglalás: 164 MB -> 2086 MB
  23.  
  24.             /*BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
  25.             StringBuilder sb = new StringBuilder();
  26.             String line = "";
  27.             while ( (line = buf.readLine()) != null){
  28.                 sb.append(line).append("\n");
  29.             }*/ // 164 MB-> 808 MB & java.lang.NullPointerException
  30.            
  31.             /*BufferedReader br = new BufferedReader(new FileReader(args[0]), 8192 * 8192);
  32.             StringBuilder sb = new StringBuilder();
  33.             String thisLine="";
  34.             while ((thisLine = br.readLine()) != null) {
  35.                 sb.append(thisLine).append("\n");
  36.             }*/ // 164 MB -> 1112 MB
  37.            
  38.             //FileReader fileReader = new FileReader(new File(args[0])); ... while read() is -1
  39.             //InputStreamReader isr = new InputStreamReader(new FileInputStream(args[0]),Charset.defaultCharset().name()); // 164 MB -> 340 MB
  40.             //InputStreamReader isr = new InputStreamReader(new FileInputStream(args[0]),Charset.forName("ISO-8859-2")); // 164 MB -> 340 MB
  41.             //InputStreamReader isr = new InputStreamReader(new FileInputStream(args[0]),StandardCharsets.UTF_8); // 164 MB -> 340 MB
  42.  
  43.             // input stream reader is closed
  44.            
  45.         } catch (Exception e) {
  46.             //e.printStackTrace();
  47.             System.out.println(e);
  48.         }
  49.         Thread.sleep(10000);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement