0

I write a String to a file, the string is very long, about 100K, here is my code:

public static void main(String[] args) throws IOException {
    String s = "kkkk";

    BufferedWriter bw = new BufferedWriter(new FileWriter("/Users/liaoliuqing/Downloads/1.txt",true),1024);
    bw.write(s);
    bw.flush();
    bw.close();
}

when the string is short, it works well. but when the string is very long, the string will be truncate, and only some character written to the file.

what is the problem?

here is my code, and it works in many threads.

private void writeToFile(String filterName, String messageToWrite)
        throws IOException {
    if(!messageToWrite.contains("2015")){
        LOG.warn(messageToWrite);
    }
    bufferWritter = getBufferWriter(filterName, messageToWrite);
    bufferWritter.write(messageToWrite);
    if(!messageToWrite.contains("2015")){
        LOG.warn(messageToWrite);
    }
    bufferWritter.flush();
}

I pretty sure messageToWrite is the full string, since I log it. but just the latter part of the string is written to the file.

It works in about 10 threads.

I find the problem. when one thread is writting the file, then it is the time for another thread to run, the content of first thread will confuse with 2rd thread. I try to incread the buffer size of the bufferwriter by

    returnWriter = new BufferedWriter(new FileWriter(fileName, true),64*1024);

but it is not work. it still write just 8192(the default buffer size) to the file. how to solve this?

jinhong_lu
  • 238
  • 1
  • 2
  • 11
  • 4
    Well I very much doubt that it's a problem with `BufferedWriter`. My guess is that it's something to do with how you're viewing the file, but we don't really have enough information to help you. Can you produce a short but *complete* program demonstrating the problem? – Jon Skeet Sep 07 '15 at 11:36
  • 2
    are you sure that your string is complete? there might be a problem with obtaining the string. I hardly doubt that java would fail at a simple task like this after so much time. – Tamas Hegedus Sep 07 '15 at 11:42
  • I'm pretty sure that it's not working if You try to write message larger than Your buffer size, since u write 'only once'. – Fincio Sep 07 '15 at 11:43
  • You are actually appending to the file (param true passed to FileWriter). Maybe this is causing confusion? – wero Sep 07 '15 at 11:45
  • 1
    @Fincio The buffer size only defines after how many characters the buffer is flushed to the underlying filesystem. It does not have an impact of the total amount of characters written. – SubOptimal Sep 07 '15 at 11:50
  • 2
    BufferedWriter is not thread safe, if you write to it from multiple threads, strange things will happen. – Will Sep 08 '15 at 02:13
  • I found the problem, when one thread is writing the file, and then, it is time for another thread to run, but the line of the first thread is not yet finish written, so the 2 lines form 2 threads will confuse. How to solve this problem? flust() not help. – jinhong_lu Sep 08 '15 at 03:40
  • I find the problem. when one thread is writting the file, then it is the time for another thread to run, the content of first thread will confuse with 2rd thread. I try to incread the buffer size of the bufferwriter by returnWriter = new BufferedWriter(new FileWriter(fileName, true),64*1024); but it is not work. it still write just 8192(the default buffer size) to the file. how to solve this? – jinhong_lu Sep 08 '15 at 04:32
  • 1
    Since you asked again about this issue/program (http://stackoverflow.com/questions/32451526/how-to-set-the-buffer-size-on-a-bufferedwriter-over-a-filewriter), I think this might be redundant. If not removed this question I could add my same answer here as well. – Javier Sep 08 '15 at 10:58

1 Answers1

0

You need to ensure that only a single thread can use the buffered writer at a time. A simple solution is to make the writeToFile method synchronized. Otherwise you can use some sort of locking mechanism. You can either use Object.wait/notify or use a semaphore from java.util.concurrent package.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
  • not just some threads, but also some processes will write the same file. so I seems that synchronized is not worked. – jinhong_lu Sep 08 '15 at 05:04
  • If you want a locking mechanism that works across processes, you need to look for some other solution. One typical way is to use a file as a lock. If a process can create the lock file, it goes ahead and removes the file at the end. All the processes wait till they can create the file. Though I doubt that solution will help you. – Dakshinamurthy Karra Sep 08 '15 at 05:09