JAVA中如何用filechooser选中一个TXT文件后,读取里面的文本内容 然后将其赋给一个string?
2013-12-02
展开全部
public class JFilechooserDemo{
public static void main(String[] args) throws IOException{
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
BufferedReader reader=new BufferedReader(new FileReader(chooser.getSelectedFile()));
String line;
StringBuilder sb = new StringBuilder();
while((line=reader.readLine())!=null){
sb.append(line);
}
reader.close();
System.out.println(sb.toString());
}
}
public static void main(String[] args) throws IOException{
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
BufferedReader reader=new BufferedReader(new FileReader(chooser.getSelectedFile()));
String line;
StringBuilder sb = new StringBuilder();
while((line=reader.readLine())!=null){
sb.append(line);
}
reader.close();
System.out.println(sb.toString());
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询