0

Im trying to limit the size creation of one file in my java app. For that I made this sample code which I declare one variable lenght of 32 bytes, start WriteOnFile method looping it until 32 bytes. The problem is, it doesnt matter the value I set set on FILE_SIZE (Higher or lower) the download file is always coming with 400kb - that means my code is working since the original file is 200Mb but has some logic mistake. is there another way to do this? cos I based myself on this post How to limit the file size in Java and until now found nothing better

I was wondering if this has something with the bufferedwriter...

Thanks in advance for the help

public static final byte FILE_SIZE = 32;   

 private static void WriteOnFile(BufferedWriter writer, String crawlingNode){

               try {
                   while(file.length()<FILE_SIZE){

                    writer.write(crawlingNode);
                    System.out.println(file.length());
                   }
            } catch (IOException e) {

                JOptionPane.showMessageDialog(null, "Failed to write URL Node");
                    e.printStackTrace();            
            }

           }
6
  • The post you refered in really good in which the first point 1.Figuring out the byte-length, n, of the first line. is very important... Commented Jul 19, 2013 at 12:35
  • @nvrmnd regarding to this post ourownjava.com/javafilewritervsbufferedwriter it says the bufferedwriter has 8192 (or something around) to write, the file (even with the mistake) has 400,000 so, whats the deal with this reduction tip? Im sorry I couldnt get it Commented Jul 19, 2013 at 12:37
  • @AurA Im not breaking lines on the file, so wouldnt be a mistake check it? Commented Jul 19, 2013 at 12:41
  • I bet the file.length() isn't being updated so often. You could use an OutputStream that counts the bytes written and stops when it has reached a limit. Commented Jul 19, 2013 at 12:44
  • @Kayaman I thought the same way, cos of that I settle FILE_SIZE to 50Mb to see what was as gonna get and the result was the same - like, if the value of the FILE_SIZE was too low for 32 and the first write block was 400Kb I should get more information on the file, shouldnt I? Commented Jul 19, 2013 at 12:49

3 Answers 3

1

I just tried around a little bit and I could write files that are 32 bytes large by limiting the BufferedWriter's write-method directly:

        writer.write(crawlingNode, 0, 32);

With that call, only the first 32 chars are written to the file. As my encoding was UTF-8, which means that every char occupies one byte, the size of my output file was only 32 bytes. Writing only 16 chars resulted in a file of 16 bytes and so forth.

So maybe you could use that without implementing some other big stuff.

EDIT:

If your String has less characters than 32, then use the following call:

writer.write(crawlingNode, 0, crawlingNode.length);
Sign up to request clarification or add additional context in comments.

4 Comments

Glad it helped. You only have to make sure, that the String is long enough to fill the 32 chars, otherwise you get a StringIndexOutOfBoundsException.
Any problems with the code or why did you remove the accepted answer mark? Let me know so that I can edit my post :)
I was trying to edit the post and explain, this is a valid solution in a simple way, somehow this works with bit array, it takes the array from the position zero until the 32 which causes lack of information =\ wouldnt be a valid solution for the question and would mess the head of other programmer that see this as an accepted answer
I guess I'm a little confused what you mean by loss of information. You want to write the first 32 bytes of a String to a file. And a String is an Array of chars. This is why the method writes all the chars from position 0 to 31 to the file. This results in a file with 32 x 1 Bytes (depending on the encoding). Does there really get information lost?
0

Use the stream to limit the file size, don't rely on file.length().

Apache Commons has this http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/output/CountingOutputStream.html

But you can easily write your own as well.

Comments

0

I found my own answer. Yes, this has to be with Buffered writer. The Buffered writer has a default buffer of 8kb to READ. To write this buffer is dumped every 400kb in my app case. To write the file with one lower size we need to use another way to write like OutputSteam..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.