Java FileReader无法查找文件 [英] Java FileReader not finding files

查看:133
本文介绍了Java FileReader无法查找文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我无法通过引用.class和.java文件的本地目录中的文件来使用该方法,因此我用google搜索绝对指定并不是一个糟糕的选项。 / p>

任何输入都有帮助!

  public void fileGenerator(String in,字符串输出){
try {
String current_directory = System.getProperty(user.dir);
扫描仪输入=新的扫描仪(新的FileReader(current_directory +/+ in));
PrintWriter output = new PrintWriter(current_directory +/+ out);
while(input.hasNext()){
String line = input.nextLine();
output.println(line);
output.close();
}
} catch(例外e){System.out.println(e.getMessage()); }
}

这是请求的堆栈跟踪:

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

(没有这样的文件或目录)
at java .io.FileInputStream.open(本机方法)
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.BasicBut tonListener.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)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。 EventDispatchT hread.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)


解决方案

打印出 current_directory ,并确认它符合您的期望。



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



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


I decided to start a new question so it can strictly focus on the FileReader errors.

This is a method that takes in a file name, and a desired output name for a new file. Say the inputted filename is "hello.txt"... the method makes it something like "/home/User/hello.txt", which goes into the FileReader as a parameter. The problem is that I get this as output "/home/User/hello.txt (No such file or directory)", even though the file does exist and the directory structure and permissions are correct.

I couldn't get the method to work with just referencing the file in the local directory of the .class and .java file so I googled my way to find that absolute specifying is not a bad option.

Any input is helpful!

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()); }
}

Here is the requested stacktrace:

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)

解决方案

Print out what current_directory is and confirm that it matches your expectations.

I'd also print out the complete file path that you pass to the FileReader as well.

Most times that behavior doesn't match my expectations, I find that my assumptions were wrong.

这篇关于Java FileReader无法查找文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆