gpt4 book ai didi

java - 修改 .csv 文件数据 JAVA 时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:39 25 4
gpt4 key购买 nike

我在使用 Java AddressBook 时遇到问题,我正在尝试编辑和更新 .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

更新错误

java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at com.mycompany.ppaddressbook.ammContact.editRecord(ammContact.java:174)
at
com.mycompany.ppaddressbook.ammContact$ammendActionListener.actionPerformed(ammContact.java:154)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6632)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6397)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

最佳答案

我猜问题出在File#renameTo()。根据 Javadoc,此方法严重依赖平台库。

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

您的代码可以在我的环境中运行,但要使其保存以在任何平台上使用,您应该使用 NIO-Library(新 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(...) 中也许会更好。您可以捕获异常并将其显示在调用者上。

关于java - 修改 .csv 文件数据 JAVA 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57037614/

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