Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JAVA中有.csv文件数据的问题_Java_Exception_Try Catch - Fatal编程技术网

在JAVA中有.csv文件数据的问题

在JAVA中有.csv文件数据的问题,java,exception,try-catch,Java,Exception,Try Catch,我遇到了一个Java地址簿问题,我试图编辑和更新存储在.csv文件中的一行数据中的值 我试图运行代码并检查逻辑错误,但似乎找不到引发try-catch异常的原因。这确实在某一点上起作用,但无法找出是什么改变了这一点,使其停止工作 private class ammendActionListener implements ActionListener { public ammendActionListener() { } @Overrid

我遇到了一个Java地址簿问题,我试图编辑和更新存储在.csv文件中的一行数据中的值

我试图运行代码并检查逻辑错误,但似乎找不到引发try-catch异常的原因。这确实在某一点上起作用,但无法找出是什么改变了这一点,使其停止工作

    private class ammendActionListener implements ActionListener {

        public ammendActionListener() {
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
           String filePath = "Personal.csv";
           String editTerm = id.getText();
           String newID = id.getText();
           String newFName = fn.getText();
           String newLName = ln.getText();
           String newAddr = addr.getText(); 
           String newAddr2 = addr2.getText();
           String newPost = post.getText();
           String newCont = cont.getText();

           editRecord(filePath, editTerm, newID, newFName, newLName, newAddr, newAddr2, newPost, newCont);
        }
    }
    private Scanner x; 
    public void editRecord(String filePath, String editTerm, String newID, String newFName, String newLName, String newAddr, String newAddr2, String newPost, String newCont){
        String tempFile = "temp.csv"; 
        File oldFile = new File(filePath);
        File newFile = new File(tempFile);
        String ID = ""; String first = ""; String last = ""; String addr =""; String addr2=""; String post = ""; String cont = ""; 
        try{
            FileWriter fw = new FileWriter(tempFile, true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            x = new Scanner(new File(filePath));
            x.useDelimiter("[,\n]");

            while(x.hasNext()){
                ID = x.next();
                first = x.next();
                last = x.next();
                addr = x.next();
                addr2 = x.next();
                post = x.next();
                cont = x.next();

                if(ID.equals(editTerm)){
                    pw.println(newID+","+newFName+","+newLName+","+newAddr+","+newAddr2+","+newPost+","+newCont);
                }else{
                    pw.println(ID+","+first+","+last+","+addr+","+addr2+","+post+","+cont);
                }
            }
            x.close();
            pw.flush();
            pw.close();
            oldFile.delete();
            File dump = new File(filePath);
            newFile.renameTo(dump);

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "ERROR");
        }

    }

}
我希望该文件能够通过标识ID列、重写并重命名temp.csv文件以及删除带有更新行的原始Presonal.csv来添加适当的数据。执行时没有错误


建立成功 总时间:36.814秒 完成时间:2019-07-15T11:07:47+01:00

最终内存:13M/47M 更新错误


我想问题出在
文件#renameTo()
。根据Javadoc,这种方法严重依赖于平台库

此方法行为的许多方面本质上依赖于平台:重命名操作可能无法将文件从一个文件系统移动到另一个文件系统,它可能不是原子的,并且如果具有目标抽象路径名的文件已存在,则可能无法成功。应始终检查返回值,以确保重命名操作成功

您的代码可以在我的环境中工作,但要使其保存起来以便在任何平台上使用,您应该使用NIO库(新IO)。下面是一个快速代码,它应该可以完成您想要做的事情。请注意,您不需要刷新或关闭读写器,因为try-with-resources可以为您完成这些工作

public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        test.editRecord("C:/temp/test.csv", "2", "2", "foobar", "baz");
    }

    public void editRecord(String filePath, String editTerm, String newID, String newFName, String newLName){
        String tempFile = "temp.csv"; 
        Path oldPath = Paths.get(filePath);
        Path newPath = Paths.get(tempFile);
        
        try(BufferedReader reader = Files.newBufferedReader(oldPath); BufferedWriter writer = Files.newBufferedWriter(newPath)) {
            String read;
            while( (read = reader.readLine()) != null ) {
                String[] line = read.split(",");
                if(line[0].equals(editTerm)) {
                    line[0] = newID;
                    line[1] = newFName;
                    line[2] = newLName;
                }
                
                writer.append(String.join(",", line));
                writer.newLine();
            }
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        
        try {
            Files.move(newPath, oldPath, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }

    }

}
编辑: 考虑使用
Files.createTempFile(“temp”和“.csv”)用于临时文件


由于抛出
IOException
的可能性很大,因此最好将
throws
添加到
editRecord(…)
。您可以捕获并显示调用者的异常。

我想问题在于
文件#renameTo()
。根据Javadoc,这种方法严重依赖于平台库

此方法行为的许多方面本质上依赖于平台:重命名操作可能无法将文件从一个文件系统移动到另一个文件系统,它可能不是原子的,并且如果具有目标抽象路径名的文件已存在,则可能无法成功。应始终检查返回值,以确保重命名操作成功

您的代码可以在我的环境中工作,但要使其保存起来以便在任何平台上使用,您应该使用NIO库(新IO)。下面是一个快速代码,它应该可以完成您想要做的事情。请注意,您不需要刷新或关闭读写器,因为try-with-resources可以为您完成这些工作

public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        test.editRecord("C:/temp/test.csv", "2", "2", "foobar", "baz");
    }

    public void editRecord(String filePath, String editTerm, String newID, String newFName, String newLName){
        String tempFile = "temp.csv"; 
        Path oldPath = Paths.get(filePath);
        Path newPath = Paths.get(tempFile);
        
        try(BufferedReader reader = Files.newBufferedReader(oldPath); BufferedWriter writer = Files.newBufferedWriter(newPath)) {
            String read;
            while( (read = reader.readLine()) != null ) {
                String[] line = read.split(",");
                if(line[0].equals(editTerm)) {
                    line[0] = newID;
                    line[1] = newFName;
                    line[2] = newLName;
                }
                
                writer.append(String.join(",", line));
                writer.newLine();
            }
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        
        try {
            Files.move(newPath, oldPath, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }

    }

}
编辑: 考虑使用
Files.createTempFile(“temp”和“.csv”)用于临时文件


由于抛出
IOException
的可能性很大,因此最好将
throws
添加到
editRecord(…)
。您可以捕获并在调用者上显示异常。

不要在catch块中抛出一条仅表示
错误的消息。那没告诉你什么。使用:
e.printStackTrace()然后让我们知道实际的错误是什么。感谢您的回复我添加了堆栈跟踪的结果希望这会有所帮助。不要在catch块中抛出一条消息,只说明
错误
。那没告诉你什么。使用:
e.printStackTrace()然后让我们知道实际的错误是什么。感谢您的回复,我添加了堆栈跟踪的结果,希望这会有所帮助。谢谢,这对解决我遇到的问题非常有帮助。这个答案让我有了更好的理解。很高兴我能帮上忙。如果这解决了你的问题,请考虑打勾或写你自己的答案,澄清我错过的地方。因此,任何人都可以尽快找到他的答案。谢谢你,这真的帮助了我解决了这个问题。这个答案让我有了更好的理解。很高兴我能帮上忙。如果这解决了你的问题,请考虑打勾或写你自己的答案,澄清我错过的地方。因此,任何其他人都可以尽快找到他的答案。