java读取文件时如何获得已读取的字节大小? 5
我已经得到流了,需要解析文件,我想知道解析的进度。可是我不知道已经解析多少了。我该怎样得到已解析的大小呢?部分代码:reader=newBufferedReader(ne...
我已经得到流了,需要解析文件,我想知道解析的进度。可是我不知道已经解析多少了。我该怎样得到已解析的大小呢?
部分代码:
reader = new BufferedReader(new InputStreamReader(in, "GBK"));
String str = reader.readLine();
<-----解析文件------> 展开
部分代码:
reader = new BufferedReader(new InputStreamReader(in, "GBK"));
String str = reader.readLine();
<-----解析文件------> 展开
1个回答
2015-11-19
展开全部
// 查看copy例子即可。
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class Bar extends JFrame implements PropertyChangeListener
{
private static final long serialVersionUID = 1L;
private static final int MIN = 0;
private static final int MAX = 100;
private JProgressBar progressBar;
private JButton copy;
private Task task;
public Bar(String title)
{
setTitle(title);
setLayout(new BorderLayout());
progressBar = new JProgressBar(MIN, MAX);
progressBar.setValue(MIN);
progressBar.setStringPainted(true);
add(progressBar, BorderLayout.CENTER);
copy = new JButton("拷贝");
copy.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
start();
copy.setEnabled(false);
}
});
add(copy, BorderLayout.EAST);
}
class Task extends SwingWorker<Void, Void>
{
@Override
public Void doInBackground()
{
setProgress(MIN);
File file = new File("a.jpg");
long size = file.length();
byte[] bts = new byte[(int) (size / (Math.random() * 50 + MAX - 50))];
int length = 0;
long cLength = 0;
try
{
InputStream stream = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File("copy_aa.jpg"));
while((length = stream.read(bts)) > 0)
{
os.write(bts, 0, length);
cLength += length;
setProgress((int) (cLength * MAX / size));
Thread.sleep(30);
}
os.flush();
os.close();
stream.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
public void done()
{
setTitle("拷贝完毕");
Toolkit.getDefaultToolkit().beep();
copy.setEnabled(true);
setCursor(null);
}
}
public void propertyChange(PropertyChangeEvent evt)
{
if("progress" == evt.getPropertyName())
{
int progress = (Integer) evt.getNewValue();
progressBar.setValue(progress);
}
}
private void start()
{
task = new Task();
task.addPropertyChangeListener(this);
task.execute();
setTitle("正在拷贝...");
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
private static void createAndShowGUI()
{
Bar frame = new Bar("Bar");
frame.setAlwaysOnTop(true);
frame.setSize(280, 60);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
追问
亲,请看我写的那部分代码,我需要衔接上去。不能length = stream.read(bts)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |