-1

我决定开始一个新问题,以便它可以严格关注 FileReader 错误。

这是一种接受文件名和新文件所需输出名称的方法。假设输入的文件名是“hello.txt”......该方法使其类似于“/home/User/hello.txt”,它作为参数进入 FileReader。问题是我把它作为输出“/home/User/hello.txt(没有这样的文件或目录)”,即使该文件确实存在并且目录结构和权限是正确的。

我无法仅通过引用 .class 和 .java 文件的本地目录中的文件来使用该方法,因此我用谷歌搜索了我的方法,发现绝对指定不是一个坏选择。

任何输入都是有帮助的!

public void fileGenerator(String in, String out) {      
try {
    String current_directory = System.getProperty("user.dir");
    Scanner input = new Scanner(new FileReader(current_directory+"/"+in));
    PrintWriter output = new PrintWriter(current_directory+"/"+out);
        while(input.hasNext()) {
        String line = input.nextLine(); 
    output.println(line);
    output.close(); 
    }
  }  catch (Exception e) { System.out.println(e.getMessage()); }
}

这是请求的堆栈跟踪:

java.io.FileNotFoundException: /home/User/hello.txt

(No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:137)
    at java.io.FileInputStream.<init>(FileInputStream.java:96)
    at java.io.FileReader.<init>(FileReader.java:58)
    at TEST.fileGenerator(TEST.java:131)
    at TEST.generateCSV_TWO(TEST.java:122)
    at TEST$4.actionPerformed(TEST.java:102)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.java:6203)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:5968)
    at java.awt.Container.processEvent(Container.java:2105)
    at java.awt.Component.dispatchEventImpl(Component.java:4564)
    at java.awt.Container.dispatchEventImpl(Container.java:2163)
    at java.awt.Component.dispatchEvent(Component.java:4390)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
    at java.awt.Container.dispatchEventImpl(Container.java:2149)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4390)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$2.run(EventQueue.java:622)
    at java.awt.EventQueue$2.run(EventQueue.java:620)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
4

2 回答 2

0

没有这样的文件或目录通常意味着文件不存在。请编辑代码并使用 IDE 或添加打印语句对其进行调试。此外, current_directory 可以重命名为 currentDirectory 以符合Java 命名约定。尝试运行下面的代码。

public static void fileGenerator(String in, String out) {      
try {
    String currentDirectory = System.getProperty("user.dir");
    System.out.println(currentDirectory);
    String inputFileName = currentDirectory+"/"+in;
    File inputFile = new File(inputFileName);
    System.out.println(inputFile.getAbsolutePath());
    FileReader inputFileReader = new FileReader(inputFile);
    Scanner input = new Scanner(inputFileReader);
    PrintWriter output = new PrintWriter(currentDirectory+"/"+out);
    while(input.hasNext()) {
        String line = input.nextLine(); 
    output.println(line);
    output.close(); 
    }
  }  catch (Exception e) { 
      e.printStackTrace();
  }
}
于 2012-05-01T01:34:08.387 回答
-1

打印出是什么current_directory并确认它符合您的期望。

我还将打印出您传递给的完整文件路径FileReader

大多数时候,这种行为与我的期望不符,我发现我的假设是错误的。

于 2012-05-01T01:29:19.980 回答