0

我正在尝试从一个文件读取并将结果处理到另一个文件,但我在使用 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;
}
 }
4

2 回答 2

2

问题在这里:

if (a.getSource() == btnClear)
   txtFileIn.setText("");
   txtFileOut.setText(""); <-- txtFileOut is always cleared

您需要在此语句中添加大括号if,否则您将清除 txtFileOut JTextField. 当您写出文件时,您正在尝试写入一个名为的文件,""从而导致FileNotFoundException.

改成:

if (a.getSource() == btnClear) {
   txtFileIn.setText("");
   txtFileOut.setText(""); 
}
于 2013-03-12T00:50:59.730 回答
0

我之前遇到过 PrintWriter 的问题,因为正如文档所说

“这个类中的方法从不抛出 I/O 异常,尽管它的一些构造函数可能会。客户端可以通过调用 checkError() 来查询是否发生了任何错误。”

我试图使用带有完整磁盘的 PrintWriter 却一无所获。

您可以使用 aBufferedWriter代替,这将引发 IOExceptions,或者您可以执行类似我在此处所做的操作,并复制 BufferedWriter 以引发未经检查的异常。

于 2013-03-12T00:55:08.290 回答