使用Java限制文件大小的创建 [英] Limiting file size creation with java

查看:358
本文介绍了使用Java限制文件大小的创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试限制在Java应用程序中创建一个文件的大小.为此,我编写了一个示例代码,声明了一个32字节的变量长度,启动WriteOnFile方法将其循环到32字节.问题是,我在FILE_SIZE上设置的值(较高或较低)无关紧要,下载文件始终带有400kb-这意味着我的代码可以正常工作,因为原始文件为200Mb,但是存在一些逻辑错误.还有另一种方法可以做到这一点吗? cos是我根据这篇文章得出的结论如何在Java中限制文件大小直到现在都找不到更好的选择

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

我想知道bufferedwriter是否有问题...

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

预先感谢您的帮助

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();            
            }

           }

推荐答案

我找到了自己的答案.是的,这必须与缓冲编写器一起使用. Buffered writer具有默认的8kb缓冲区以读取.要写入此缓冲区,在我的应用程序案例中每400kb会转储一次.要以较小的尺寸写入文件,我们需要使用另一种写入方式,例如 OutputSteam ..

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..

这篇关于使用Java限制文件大小的创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆