根据未知长度的文本文件设置进度条值

发布于 2024-12-06 23:05:03 字数 276 浏览 0 评论 0原文

我目前有一个程序,它从文本文件中读取数据,然后在读取每一行后写入数据库,文本文件的大小未确定,有时文件的行数可能比其他日子多或少。

我已经有一个 Swing Worker 来执行我的函数,所以我的进度条可以工作,但现在我只是将 setIndependentate 设置为 true,这样用户就知道正在执行某些操作,而不是实际执行的操作进步。

有没有办法可以在读取每一行后增加进度条,但不要太早或太晚达到 100,最好不要事先完全读取文本文件。谢谢,牛肉。

I currently have a program which reads from a text file and then writes to a database after each line it reads, the size of the text file is undetermined, some days the file could be more or less lines than other days.

I already have a swing worker that executes my functions so my progress bar works but right now I just have setIndeterminate to true so the user knows something is being done just not the actual progress.

Is there a way I can increment the progress bar after each line is read, but have it not reach 100 too early or too late, preferably without reading the the text file entirely before hand. Thanks, Beef.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

有深☉意 2024-12-13 23:05:03

我会使用 文件.length() 确定文件大小。然后跟踪读取的字节数以确定进度。

I'd use File.length() to determine the file size. Then keep track of the number of bytes read to determine the progress.

翻身的咸鱼 2024-12-13 23:05:03

如果在开始读取之前知道文件大小,则可以逐行读取它并计算每行后的百分比:计算每行中的字节数并将其除以总字节数(即 <代码>file.length())。

If the file size is known before you are starting to read it you can read it line-by-line and count the percentage after every line: count the number of bytes in each line and devide it by the total number of bytes (i.e. file.length()).

灯角 2024-12-13 23:05:03

并将输出包装到 GUI(在您的情况下是来自 JProgressBarProgress)到 invokeLater(),因为您已经完全脱离了EDT,更多信息请参见 Swing 中的并发性

and wrap output to the GUI (in your case the Progress from the JProgressBar) to the invokeLater(), because you are pretty out of the EDT, more in Concurency in Swing

坚持沉默 2024-12-13 23:05:03

使用 java.io.File 很容易确定文件大小,因此这里的问题是获取简单进度条实际读取的字节大小,有两种可能性:

  1. 估计:假设1 个字符 = 1 个字节(如果您的文件是 UTF-16,则 1 个字符 = 2 个字节,...)。 1 字节的猜测会让您低估实际读取的大小,因此根据文件中的多字节字符数,您将在末尾看到条形的跳跃。

  2. 计算:使用文件字符编码将读取的字符重新编码为字节数组,并获取编码数组的长度。

  3. Count:正如 Brendan 在下面的评论中所建议的那样,使用 CountingInputStream (在缓冲后以正确的顺序)来计算实际读取的字节数。

编号。 2.对我来说,这种情况似乎是不必要的开销,所以我想我会坚持使用Nr。 1 或 Nr。 3

To determine the file size is easy using java.io.File so the problem here is to get the size in bytes actually read for a simple progress bar two possibilities come to mind:

  1. Estimation: Assume 1 Character = 1 Byte (or 1 Character = 2 Byte if your file is UTF-16,...). The 1 Byte guess will have you underestimate your actual size read so you will have a jump of the bar at the end depending on how many multi byte characters are in your file.

  2. Calculate: Recode the characters read to a byte array using the files character encoding and take the length of the encoded array.

  3. Count: As proposed by Brendan in the comments below, use a CountingInputStream (in correct order after Buffering) to count the bytes actually read.

Nr. 2. seems unneccessary overhead for this case to me so I think I'd stick to Nr. 1 or Nr. 3

橪书 2024-12-13 23:05:03

您可以在您的应用程序中添加类似下图的内容:)

circle Progress bar

You can have in your app something like this image :)

circle progress bar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文