0

I have this code :

try {
            bufferedReader = new BufferedReader(new FileReader(new File(filePath)));
            String currentLine;
            int numLine = 0;
            while ((currentLine = bufferedReader.readLine()) != null) {
                numLine++;
               for (String word : currentLine.split(" ")) {
                    if (!word.isEmpty() ) {
                        mapAllWordsPositionInFilesInFolder.computeIfAbsent(word,v -> new HashMap<>())
                                .computeIfAbsent(filePath,  val -> new HashSet<>())
                                .add(numLine);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        }

I want change BufferedReader readline() by read() because it's more faster! Someone can help please !

Orland
  • 13
  • 4
  • 1. What have you tried so far? 2. Why do you think that reading character by character would be faster than reading whole lines? – Amongalen May 07 '20 at 10:55
  • because it's the best way to read a file. and it consumes less memory – Orland May 07 '20 at 10:58
  • I don't mean to be antagonistic, but could you please share evidence that calling read is faster than readLine? In my humble experience it's best to read the whole line, not to mention much better readability, which bar some very niche scenarios, is way more important than a small percentage gain in performance. – fpezzini May 07 '20 at 11:46
  • In case the files are huge (large size) which is my case, the best solution for memory is read. Than i want try it, can you help please :) – Orland May 07 '20 at 11:52
  • Your logic is parsing each line as you read it. It does not make sense to try to read the entire file and then parse it. *In case the files are huge (large size) which is my case, the best solution for memory is read.* - If you read line by line, then after each line of data is read the memory can be released. If you read the entire file, then you need more memory to hold the entire file in memory. – camickr May 07 '20 at 14:23
  • Sorry. I think am not too clear in my post. The same question, how can i get the some result of this code by using read() nstead of readLine() ?!! Thank's :) – Orland May 08 '20 at 18:08

0 Answers0