使用PrintWriter寻找指导

时间:2013-03-12 00:36:34

标签: java

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

2 个答案:

答案 0 :(得分: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(""); 
}

答案 1 :(得分:0)

之前我遇到过PrintWriter的问题,因为documentation says

  

“此类中的方法永远不会抛出I / O异常,尽管有些异常   它的构造者可能。客户可以询问是否有任何错误   通过调用checkError()发生了。“

我试图使用带有完整磁盘的PrintWriter,却什么也没得到。

您可以使用BufferedWriter代替,它会抛出IOExceptions,或者您可以执行类似我所做的事情here并制作一个抛出未经检查的异常的BufferedWriter副本。