我正在尝试从一个文件读取并将结果处理到另一个文件,但我在使用 PrintWriter 时遇到了困难。我知道当我打印到控制台时一切正常,但是当我使用 JOFileChooser 将 PrintWriter 设置为指定文件时,它不会写入或创建文件。我不确定我哪里出错了。对此的任何帮助将不胜感激。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FileIntAdder extends JFrame implements ActionListener
{
private JTextField txtFileIn;
private JTextField txtFileOut;
private JButton btnFileIn;
private JButton btnFileOut;
private JButton btnProcess;
private JButton btnClear;
public FileIntAdder()
{
this.setTitle("File I/O");
Container canvas = this.getContentPane();
canvas.add(createCenterPanel(), BorderLayout.CENTER);
canvas.add(createSouthPanel(), BorderLayout.SOUTH);
this.setResizable(false);
this.setSize(600, 150);
this.setLocation(800, 500);
this.setVisible(true);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
private JPanel createSouthPanel()
{
JPanel pnlSouth = new JPanel();
btnProcess = new JButton("Process");
btnProcess.addActionListener(this);
pnlSouth.add(btnProcess);
btnClear = new JButton("Clear");
btnClear.addActionListener(this);
pnlSouth.add(btnClear);
pnlSouth.setBackground(Color.DARK_GRAY);
return pnlSouth;
}
private JPanel createCenterPanel()
{
JPanel pnlCenter = new JPanel();
pnlCenter.setLayout(new GridLayout(2,2));
btnFileIn = new JButton("File In");
pnlCenter.add(PanelWrap(btnFileIn));
btnFileIn.addActionListener(this);
txtFileIn = new JTextField(25);
pnlCenter.add(PanelWrap(txtFileIn));
btnFileOut = new JButton("File Out");
pnlCenter.add(PanelWrap(btnFileOut));
btnFileOut.addActionListener(this);
txtFileOut = new JTextField(25);
pnlCenter.add(PanelWrap(txtFileOut));
return pnlCenter;
}
public static void main(String[] args)
{
new FileIntAdder();
}
@Override
public void actionPerformed(ActionEvent a)
{
JFileChooser fileInput = new JFileChooser();
JFileChooser fileOutput = new JFileChooser();
if(a.getSource() == btnClear)
txtFileIn.setText("");
txtFileOut.setText("");
if(a.getSource() == btnFileIn)
{
if( fileInput.showOpenDialog(btnFileIn) != JFileChooser.CANCEL_OPTION)
{
File inFile = fileInput.getSelectedFile();
String fileInName = inFile.getAbsolutePath();
txtFileIn.setText(fileInName);
}
}
if(a.getSource() == btnFileOut)
{
if(fileOutput.showSaveDialog(btnFileOut) != JFileChooser.CANCEL_OPTION)
{
File outFile = fileOutput.getSelectedFile();
String fileOutName = outFile.getAbsolutePath();
txtFileOut.setText(fileOutName);
}
}
if(a.getSource() == btnProcess)
{
PrintWriter fout = null;
try
{
Scanner lineScanner = new Scanner(new FileInputStream(txtFileIn.getText()));
fout = new PrintWriter(txtFileOut.getText());
while(lineScanner.hasNext())
{
String line = lineScanner.nextLine();
Scanner rowScanner = new Scanner(line);
int i=0;
int parentAge = 0;
int childsAge = 0;
while(rowScanner.hasNext())
{
if(i==0)
{
fout.println("Whoes your daddy: " + rowScanner.next() + " ");
parentAge = rowScanner.nextInt();
}
else
{
fout.println(i +". " + rowScanner.next() + " ");
childsAge+= rowScanner.nextInt();
}
i++;
}
if(childsAge>parentAge)
{
fout.println("You are older than dirt");
}
else
{
fout.println("Just a kid still ...");
}
fout.println("\n");
}
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
finally
{
if(fout != null)
{
fout.close();
}
}
}
}
private JPanel PanelWrap(Component c)
{
JPanel panel = new JPanel();
panel.setBackground(Color.DARK_GRAY);
panel.add(c);
return panel;
}
}