当前位置: 技术问答>java相关
急求救!
来源: 互联网 发布时间:2015-01-23
本文导语: 我用一个 JFileChooser 打开一个 .txt 文件,并开始读取,但开始读取后,就停止响应,直到读完后才可以。附程序代码: JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File("."...
我用一个 JFileChooser 打开一个 .txt 文件,并开始读取,但开始读取后,就停止响应,直到读完后才可以。附程序代码:
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(
new javax.swing.filechooser.FileFilter()
{ public boolean accept(File f)
{ String fname = f.getName().toLowerCase();
return fname.endsWith(".txt")
|| f.isDirectory();
}
public String getDescription()
{ return "Text Files"; }
});
int r = chooser.showOpenDialog(this);
if (r == JFileChooser.APPROVE_OPTION)
readFile(chooser.getSelectedFile());
}
public void readFile(final File f)
{
FileInputStream fileIn = new FileInputStream(f);
ProgressMonitorInputStream progressIn
= new ProgressMonitorInputStream(TextFrame.this,
"Reading " + f.getName(), fileIn);
InputStreamReader inReader
= new InputStreamReader(progressIn);
BufferedReader in = new BufferedReader(inReader);
int count = 0; //the lines of the .txt file
String line;
while ((line = in.readLine()) != null)
count++;
fileIn.close();
}
请问怎么解决?
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(
new javax.swing.filechooser.FileFilter()
{ public boolean accept(File f)
{ String fname = f.getName().toLowerCase();
return fname.endsWith(".txt")
|| f.isDirectory();
}
public String getDescription()
{ return "Text Files"; }
});
int r = chooser.showOpenDialog(this);
if (r == JFileChooser.APPROVE_OPTION)
readFile(chooser.getSelectedFile());
}
public void readFile(final File f)
{
FileInputStream fileIn = new FileInputStream(f);
ProgressMonitorInputStream progressIn
= new ProgressMonitorInputStream(TextFrame.this,
"Reading " + f.getName(), fileIn);
InputStreamReader inReader
= new InputStreamReader(progressIn);
BufferedReader in = new BufferedReader(inReader);
int count = 0; //the lines of the .txt file
String line;
while ((line = in.readLine()) != null)
count++;
fileIn.close();
}
请问怎么解决?
|
readFile 用线程解决:
... readFile(File f){
try{
Thread readThread=new Thread(){
public void run(){
...
}
};
readThread.start();
}catch(IOException ioe){
System.out.println(ioe);
}
}
Is it OK?
... readFile(File f){
try{
Thread readThread=new Thread(){
public void run(){
...
}
};
readThread.start();
}catch(IOException ioe){
System.out.println(ioe);
}
}
Is it OK?
|
txt太大了吧???要么把readFile变成线程的.
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。