gpt4 book ai didi

java - 寻找使用 PrintWriter 的指南

转载 作者:行者123 更新时间:2023-12-02 07:12:20 25 4
gpt4 key购买 nike

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

最佳答案

问题出在这里:

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

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

更改为:

if (a.getSource() == btnClear) {
txtFileIn.setText("");
txtFileOut.setText("");
}

关于java - 寻找使用 PrintWriter 的指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15351245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com