在另一个目录中为一个bufferedReader选择一个文件 [英] Selecting a file in another directory for a bufferedReader

查看:105
本文介绍了在另一个目录中为一个bufferedReader选择一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个初始化文件(initialize.java),它使用fileInputStream从fileInput.txt中提取数据,但它们都在不同的目录中。


  Project / library / initialize.java 
项目/资源/文本/ fileInput.txt


我在initialize.java中的代码是:

  FileInputStream fstream = new FileInputStream(/ resources / text / fileInput.txt); 

但是无法读取该文件。我也试过

  FileInputStream fstream = new FileInputStream(./ resources / text / fileInput.txt); 

但是没有这样做。



如何访问txt文件,使用./resources和/ resources有什么区别?



感谢您阅读本文。

解决方案

差异很大。在 linux / unix / macos 以/从根目录开始的路径。以./开头的路径,或从当前应用程序目录开始。调用



System.out.println(new File(。)。getAbsolutePath())你是吗?


I have an initialization file (initialize.java) that pulls in data from fileInput.txt using a fileInputStream, but both of them are in different directories.

Project/library/initialize.java
Project/resources/text/fileInput.txt

my code in initialize.java is:

FileInputStream fstream = new FileInputStream("/resources/text/fileInput.txt");

But the file cannot be read. I've also tried

FileInputStream fstream = new FileInputStream("./resources/text/fileInput.txt");

But that didn't work too.

How can I access the txt file and what's the difference between using "./resources" and "/resources"?

Thanks for reading this.

解决方案

The difference is huge. On // the path starting with / starts from root directory. The path starting with ./ or without starting from current application directory. Call

System.out.println(new File("."). getAbsolutePath()) to check where you are?

这篇关于在另一个目录中为一个bufferedReader选择一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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