Ram Kumar
Ram Kumar

Reputation: 1

Making filewriting process even more faster

I am writing the below code that this code basically writes to a file through buffered writer, I am using JDK 1.5, now my query is that basically this code works perfectly and writes to the file , the file is big let say about the final size of the file is about 1GB, now please let me is there any other alternative offered by Java in JDK 1.5 which could make file writer process even more faster and improve the performance?

 File file = new File(FilePath + getFileName());
 try (BufferedWriter bw = new  BufferedWriter(new FileWriter(file))) {
   for (Posuser posuser : totalusers) {
     for (potook bk : books) {
       if ((posuser.getUpdatedBy() == null)
           && (posuser.getUpdateddateformat() == null)) {
         bw.write("aaa"+"\r\n");
       } else if (posuser.getUpdatedBy() == null
                  && posuser.getUpdateddateformat() != null) {
         bw.write("bbb+"\r\n");
       } else if (posuser.getUpdatedBy() != null
                  && posuser.getUpdateddateformat() == null) {
         bw.write("ccc"+"\r\n");
       } else {
         bw.write("ddd"+\r\n");
       }
     }
   }
 } catch (IOException, AnotherException e) {
   logger.error(e.getMessage());
 }

Upvotes: 0

Views: 104

Answers (3)

Milan Baran
Milan Baran

Reputation: 4222

This should be at least memory efficient. Blog: String intern The string + is costly operation, you should use StringBuilder instead.

   final String aaaString = "aaa".intern();
    final String bbbString = "bbb".intern();
    final String cccString = "ccc".intern();
    final String dddString = "ddd".intern();
    final String EOL = "\r\n".intern();

    File file = new File(FilePath + getFileName());
    try (BufferedWriter bw = new  BufferedWriter(new FileWriter(file))) {
      for (Posuser posuser : totalusers) {
        for (potook bk : books) {
          if ((posuser.getUpdatedBy() == null)
              && (posuser.getUpdateddateformat() == null)) {
            bw.write(aaaString);
            bw.write(EOL);
          } else if (posuser.getUpdatedBy() == null
                     && posuser.getUpdateddateformat() != null) {
            bw.write(bbbString);
            bw.write(EOL);
          } else if (posuser.getUpdatedBy() != null
                     && posuser.getUpdateddateformat() == null) {
            bw.write(cccString);
            bw.write(EOL);
          } else {
            bw.write(dddString);
            bw.write(EOL);
          }
        }
      }
    } catch (IOException, AnotherException e) {
      logger.error(e.getMessage());
    }

Upvotes: 1

samuelgrigolato
samuelgrigolato

Reputation: 202

It's not enough just to use a BufferedWriter, you need to tweak the buffer size (second constructor parameter). The larger the buffer, the most performance you'll get. But remember, if you set the buffer size to 1MB, you'll need to be sure that reserving 1MB of RAM to every concurrent call of this code isn't an issue. Also, Brian's answer (java.nio package) is very nice. Combine the two: change to java.nio AND tweak the buffer size until you find the magic number to your use case.

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272217

I would check out the java.nio package. This SO question/answer details more info re. performance increases.

My experience with larger files sizes has been that java.nio is faster than java.io. Solidly faster. Like in the >250% range.

Upvotes: 1

Related Questions