【发布时间】:2018-09-29 19:23:46
【问题描述】:
作业:
我需要使用 GUI 界面打开一个文件,对每一行求和,然后将每行的内容后跟一系列空格和每行的总和输出到一个新文件。如果愿意,新文件必须标记为“Previousfilename”_out.txt aka *_out.txt。
我在哪里:
我有可以工作并复制文件的 GUI。
它正在编辑和汇总文件中似乎有问题的行。
我当前只有 GUI 工作的程序无法识别换行符,并显示每个 int 总和在一个新行上,例如,
输入:
1, 2, 3, 4, 5
5, 6
4, 3, 9
输出:
1
3
6
10
15
20
26
30
33
42
由于不需要 ',' 分隔 testfile.txt 中的整数,我删除了分隔符,并且在我点击复制并出现错误后整个程序失败:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1 2 3 4 5"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at fileSumCompute.computeF(fileSumCompute.java:210)
at fileSumCompute$myHandler.actionPerformed(fileSumCompute.java:105)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
即使 Jfilechooser 正在使用该副本,我如何编辑它?我怎样才能让它识别换行符,但仍然用 EOF 打印出最后一个总和(以前的构建有问题,在那里出错。)?
旧代码: https://drive.google.com/open?id=1OOB5-21sJXQSl8XLlFPASaAPoIsXWnPu
新代码:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.nio.file.*;
import java.util.*;
public class fileSumCompute extends JFrame{
//my first gui
private JFileChooser fc;
private JButton copyButton;
private JButton chooseFileButton;
private JButton destinationButton;
private File workingDirectory;
private JLabel sourceLabel;
private JLabel destinationLabel;
private JTextField sourceText;
private JTextField sourceFileText;
private JTextField destinationText;
public static void main(String [] args)
{
fileSumCompute cpy1 = new fileSumCompute();
cpy1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cpy1.setSize(600, 150);
cpy1.setVisible(true);
}
//my first time creating and opening files without direct command line input.
public fileSumCompute() { @Override
//peiced together a couple JfileChooser examples and customized... I cant seem to get it to work without the other path selection and preprograming it into the same directory as source file.
//It works though.
super("Compute sum of row. New file = \" *_out.txt\" ");
setLayout(new GridLayout(3, 3, 5, 5));
fc = new JFileChooser();
//Open dialog box to make easier to find files
workingDirectory = new File(System.getProperty("user.dir"));
fc.setCurrentDirectory(workingDirectory);
//create labels and buttons
chooseFileButton = new JButton("CHOOSE SOURCE FILE");
destinationButton = new JButton("DESTINATION FOLDER");
copyButton = new JButton("COMPUTE & SAVE FILE"); //copies file so origonal is preserved
sourceLabel = new JLabel("SOURCE: ");
sourceText = new JTextField(10);
sourceText.setEditable(false);
destinationLabel = new JLabel("DESTINATION: ");
destinationText = new JTextField(10);
destinationText.setEditable(false);
//JFrame tools
add(sourceLabel);
add(sourceText);
add(chooseFileButton);
add(destinationLabel);
add(destinationText);
add(destinationButton);
add(copyButton);
//Create myHandler object to add action listeners for the buttons.
myHandler handler = new myHandler(); //handles the files and copies them
chooseFileButton.addActionListener(handler);
destinationButton.addActionListener(handler);
copyButton.addActionListener(handler);
//computeF(name);
}
//Inner class to create action listeners
private class myHandler implements ActionListener {
private File selectedSourceFile;
private File selectedDestinationFile;
public void actionPerformed(ActionEvent event)
{
int returnVal;
String selectedFilePath;
File selectedFile;
if (event.getSource() == chooseFileButton)
{
returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
selectedSourceFile = fc.getSelectedFile();
sourceText.setText(selectedSourceFile.getName());
}
}
if (event.getSource() == destinationButton)
{
returnVal = fc.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
selectedDestinationFile = fc.getSelectedFile();
destinationText.setText(selectedDestinationFile.getName());
//where we implement the second file... here is where we'll call to compute the sum of the lines
String name = selectedSourceFile.getName();
name = selectedSourceFile.getName().substring(0, name.lastIndexOf(".")) + "_out.txt"; //changed the output file to read ("Orig name here"_out.txt)
File destinationFile = new File(selectedDestinationFile.getParentFile(), name);
//destinationFile.deleteOnExit();
try {
Files.copy(selectedSourceFile.toPath(), destinationFile.toPath()); //copying and computing
computeF(destinationFile); //if we can get this to edit the _out.txt file we can do with out creating two seperate files- the previous *_.tmp can just be the *_out.txt
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
if (event.getSource() == copyButton)
{
Path sourcePath = selectedSourceFile.toPath();
Path destinationPath = selectedDestinationFile.toPath();
try
{
Files.copy(sourcePath, destinationPath);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
以上是图形用户界面 然后是我处理下面过程的方法
public static void computeF(File selectedDestinationFile) throws IOException{
//int i=0;
if (!selectedDestinationFile.isFile()) // IF CANNOT FIND FILE
{
System.out.println("File.txt - Parameter is not an existing file");
return;
}
Scanner fileReader = null; //using the namespace for readability.
int lineSum=0;
try
{
fileReader = new Scanner(selectedDestinationFile);
}
catch (IOException a) //it insists on a try catch
{
a.printStackTrace();
}
String name = selectedDestinationFile.getName();
System.setOut(new PrintStream(new FileOutputStream(name)));
while(fileReader.hasNext()) {
String number = fileReader.next();
sum += Integer.parseInt(number);
System.out.println(sum);
if(fileReader.hasNextLine()) {
System.out.println(sum);
sum = 0;
}
}
}
}
/*_______________________________________________________________________________________________________________________*/
/*implement after Working script*/
//is there a way to append instead of doing the System.setout? like with this similarly found code.
/* appending how can I implement this??
try(FileWriter fw = new FileWriter("myfile.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
out.println("the text");
//more code
out.println("more text");
//more code
} catch (IOException e) {
//exception handling left as an exercise for the reader
}
// or this one? I need to keep the copy but just add to the end so that makes the most sense.
fout = new FileWriter("filename.txt", true);
*/
/*______________________________________________________________________________________________________________________*/
_________________edit________________ //手术后约 2 小时
忽略 Jfilechooser 中的覆盖命令,它不应该在那里。 同样对于新代码,它基本上如何让它做我想做的事情,从我知道的工作开始?也就是我怎样才能让它做我需要它做的事情:
public static void computeF(FILE in)
{
if (!selectedDestinationFile.isFile()) // IF CANNOT FIND FILE
{
System.out.println("File.txt - Parameter is not an existing file");
return;
}
Scanner fileReader = null; //using the namespace for readability.
try
{
fileReader = new Scanner(in);
}
catch (IOException a) //it insists on a try catch
{
a.printStackTrace();
}
String name = in.getName();
fout = new FileWriter(name, true);// to try to append to the copy instead of rewriting the whole file
}
【问题讨论】:
-
简化。你试图一次做太多事情,这会妨碍你的调试能力,你应该做的是孤立地解决每个问题。您当前的问题只是读取文件并解析为 int,仅此而已,因此只有这应该是您尝试的代码和显示的代码。现在看起来你正试图解析一整行数字,就好像它是一个数字一样,这是行不通的。阅读:How to Debug Small Programs
-
I have 3 other assignments also due on Sunday night and a test Monday and Tuesday, so I need to get this done asap- 与问题无关。我们没有时间阅读不必要的信息。如果你想帮助你简化问题和问题。我们都有时间限制。有时间的时候回答问题。无论如何,你需要学会管理你的时间。我们不是来为你写代码的。 -
@Phatez 我已经删除了一堆无关紧要的措辞。以后请只包含相关的注释和代码:)
标签: java file-io filereader jfilechooser file-writing