2

I have to write a code in JAVA like following structure:

Read String From File
// Perform some string processing
Write output string in file

Now, for reading/writing string to/from file, I am using,

BufferedReader br = new BufferedReader(new FileReader("Text.txt"), 32768);
BufferedWriter out = new BufferedWriter(new FileWriter("AnotherText.txt"), 32768);

  while((line = br.readLine()) != null) {
          //perform some string processing
          out.write(output string) ;
          out.newLine();
    }

However, it seems reading and writing is quite slow. Is there any other fastest method to read/write strings to/from a file in JAVA ?

Additional Info:

1) Read File is 144 MB.
2) I can allocate large memory (50 MB) for reading or writing.
3)I have to write it as a string, not as Byte.
Arpssss
  • 3,850
  • 6
  • 36
  • 80
  • How slow is "quite slow" exactly? – Jon Skeet Apr 18 '12 at 14:41
  • Is this homework? You should tag it as such. – twain249 Apr 18 '12 at 14:42
  • 1
    What else are you doing apart from reading and writing? Do you have some transformations? Could that transformation be the expensive part? If you aren't transforming the data, why not just copy the file? – Mark Byers Apr 18 '12 at 14:42
  • have you tried with RandomAccessFile? – Florin Ghita Apr 18 '12 at 14:43
  • @JonSkeet, takes near about 50 seconds to read(144 MB) +writre(148 MB). – Arpssss Apr 18 '12 at 14:43
  • @Arpssss: That does sound unusual, assuming it's on a "normal" hard disk. Please post the code you're actually using to do the IO. – Jon Skeet Apr 18 '12 at 14:44
  • @JonSkeet the legend is present. – Jonathan Payne Apr 18 '12 at 14:45
  • You show how you open files, but give no details as to how you're actually reading and writing the data. Given this, no one can really answer this "question". – Brian Roach Apr 18 '12 at 14:45
  • @MarkByers, I checked how much time takes the transformations. Minus that one I found reading+writing is expensive. – Arpssss Apr 18 '12 at 14:46
  • 4
    (As an aside, using `FileWriter` and `FileReader` is usually a bad idea, IMO. I prefer using `FileOutputStream` and `FileInputStream`, wrapped in an `OutputStreamWriter` or `InputStreamReader`, and explicitly specifying the character encoding (typically UTF-8). Otherwise it will always use the platform default encoding, making your file less-than-ideally-portable...) – Jon Skeet Apr 18 '12 at 14:47
  • @JonathanPayne, I am using br.readLine() and out.write() methods. – Arpssss Apr 18 '12 at 14:47
  • @JonSkeet, I really don't bother about standard encoding. I want a faster one to read/write. Can you inform, is that one a faster one ? – Arpssss Apr 18 '12 at 14:50
  • @Arpssss: Well if you don't care about what data you're writing, just create an empty file :) Really, you *should* be bothered by the encoding you use. It's very important, and expensive to realize too late that you've been using the wrong one. It really does sound like you're doing something unusual though - it simply shouldn't take that long regardless of encoding. – Jon Skeet Apr 18 '12 at 14:54
  • @JonSkeet, OK. Thanks a lot. However, I modified my code to show what I am actually doing. Is any comment from you about methods ? – Arpssss Apr 18 '12 at 14:57
  • 1
    If you get rid of the String processing & just write back out what you read in, what does your performance look like? – oconnor0 Apr 18 '12 at 15:00

2 Answers2

1

It sounds slower than it should be.

You can try increasing the buffer size.

Maybe also try FileOutputStream instead of FileWriter.

You mentioned 50MB. Are you modifying the memory parameters of the program at all when you run it using a -X switch?

Sarel Botha
  • 12,419
  • 7
  • 54
  • 59
  • Thanks. I try for 32 KB, 64 KB. However, same time. In one discussion, in SO, it is told to me 32 KB is more or less optimal one. However, is NIO can be a better one ? – Arpssss Apr 18 '12 at 15:04
0

Ignoring the fact that you have not posted what your performance requirements are:

Try reading/writing the file as bytes and internally convert the byte to characters/string.

This question might be helpful: Number of lines in a file in Java

Community
  • 1
  • 1
Colin D
  • 5,641
  • 1
  • 23
  • 35