JFileChooser 不会按我的意愿逐行打印我的文件。我怎么能够?
The JFileChooser will not print of my file line by line as I wish. How can I?
My code will bring up the JFileChooser GUI, but when I select a file from my desktop, it will not print the contents of the file line by line as I wish. Your help would be appreciated.
public static void main(String[] args) {
File file;
Scanner in;
int click;
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
click = jfc.showSaveDialog(null);
if(jfc.equals(JFileChooser.APPROVE_OPTION))
{
file = jfc.getSelectedFile();
try
{
in = new Scanner(file);
if(file.isFile())
{
while(in.hasNextLine())
{
String fileline = in.nextLine();
System.out.println(fileline);
}
}
in.close();
} catch(FileNotFoundException e )
{
System.out.println("Cannot locate file.");
}
}
}
if(jfc.equals(JFileChooser.APPROVE_OPTION))
为什么要将 JFileChooser
与 APPROVE_OPTION
进行比较?
您想比较从 showOpenDialog()
方法返回的值:
if (click == JFileChooser.APPROVE_OPTION)
My code will bring up the JFileChooser GUI, but when I select a file from my desktop, it will not print the contents of the file line by line as I wish. Your help would be appreciated.
public static void main(String[] args) {
File file;
Scanner in;
int click;
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
click = jfc.showSaveDialog(null);
if(jfc.equals(JFileChooser.APPROVE_OPTION))
{
file = jfc.getSelectedFile();
try
{
in = new Scanner(file);
if(file.isFile())
{
while(in.hasNextLine())
{
String fileline = in.nextLine();
System.out.println(fileline);
}
}
in.close();
} catch(FileNotFoundException e )
{
System.out.println("Cannot locate file.");
}
}
}
if(jfc.equals(JFileChooser.APPROVE_OPTION))
为什么要将 JFileChooser
与 APPROVE_OPTION
进行比较?
您想比较从 showOpenDialog()
方法返回的值:
if (click == JFileChooser.APPROVE_OPTION)