-1

我已经创建了一个从客户端到服务器的流。ProgressMonitorInputStream对于这段代码,如何创建或其他类似的东西?

        FileInputStream fileStream = new FileInputStream(file);
        int ch;
        do
        {
            ch = fileStream.read();
            exitStream.writeUTF(String.valueOf(ch));
        }
        while(ch != -1);
        fileStream.close();

更新的代码 - 窗口出现,但它是空的。只有一个框架。如何解决?

         String fileName = "aaa.jpg";
         File fileToBeSend = new File(fileName);

         InputStream input = new ProgressMonitorInputStream(
         null, 
         "Reading: " + fileName, 
         new FileInputStream(fileToBeSend));

         int ch;
         do 
         {
             ch = input.read();
            exitStream.writeUTF(String.valueOf(ch)); 
         } while(ch != -1);

         input.close();
4

2 回答 2

1

为了ProgressMonitorInputStream工作,您需要阅读非常大的文件。在其文件中规定:

这将创建一个进度监视器来监视读取输入流的进度。如果需要一段时间,会弹出一个 ProgressDialog 来通知用户。如果用户点击 Cancel 按钮,则会在下一次读取时抛出 InterruptedIOException。当流关闭时,所有正确的清理工作都完成了。

这是示例。确保您输入的文件(bigFile.txt)包含许多要阅读的内容。

在此处输入图像描述

import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import javax.swing.JLabel;
import javax.swing.ProgressMonitorInputStream;

public class ProgressMonitorInputStreamDemo {

  public static void main(String args[]) throws Exception {
  String file = "bigFile.txt";
  FileInputStream fis = new FileInputStream(file);
  JLabel filenameLabel = new JLabel(file, JLabel.RIGHT);
  filenameLabel.setForeground(Color.black);
  Object message[] = { "Reading:", filenameLabel };
  ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(null, message, fis);
  InputStreamReader isr = new InputStreamReader(pmis);
  BufferedReader br = new BufferedReader(isr);
  String line;
  while ((line = br.readLine()) != null) {
    System.out.println(line);
  }
  br.close();
  }
}

注意: 如果要查看ProgressBar,无论您正在阅读的文件多么小,都可以使用SwingWorker. 看看 这篇文章

于 2013-04-09T18:12:05.873 回答
1

你的意思是,如何使用嵌套流,像这样?

    ProgressMonitorInputStream input = new ProgressMonitorInputStream(
         null, 
         "Reading: " + file, 
         new FileInputStream(file));

    ProgressMonitor monitor = input.getProgressMonitor();
    // do some configuration for monitor here

    int ch;
    do {
        ch = input.read();
        // note: writing also the last -1 value
        exitStream.writeUTF(String.valueOf(ch)); 
    } while(ch != -1);

    input.close();
于 2013-04-09T19:33:05.200 回答