[英]Using JFileChooser and File handlng
我无法使用JFileChooser打开文本文件并在控制台中读取它,我尝试从一些教程中获取源代码,但我只获得了“文件处理”和“如何使用JFileChooser”的代码,我试图将它们组合起来或者只是为了解决这个问题,但我似乎无法做到这一点,我真的没有想法,任何帮助都会做。
If the JFileChooser returns JFileChooser.APPROVE_OPTION
, using .getSelectedFile()
will return a File
object 如果JFileChooser返回
JFileChooser.APPROVE_OPTION
,则使用.getSelectedFile()
将返回File
对象
File file;
JFileChooser chooser = new JFileChooer();
int returnValue = JFileChooser.showOpenDialog(this);
if (returnVal = JFileChooser.APPROVE_OPTION){
file = chooser.getSelectedFile();
}
If you understand how to you basic I/O, then you should know what to do with that file. 如果您了解如何使用基本I / O,那么您应该知道如何处理该文件。
Something fairly simple would just be something like this 相当简单的东西就是这样的东西
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null){
textArea.append(line + "\n");
} catch(IOException ex){
ex.printStackTrace();
}
Another option is to use the JTextComponent#read()
method 另一种选择是使用
JTextComponent#read()
方法
Another option is to use a JEditorPane
and just use its setPage()
method 另一种选择是使用
JEditorPane
并使用其setPage()
方法
JEditorPane document = new JEditorPane(); File file = fileChooser.getSelectedFile(); try { document.setPage(file.toURI().toURL()); } catch(Exception e) { e.printStackTrace(); }
If you need basic help with I/O, see this tutorial 如果您需要I / O的基本帮助,请参阅本教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.