gpt4 book ai didi

java - 在java中使用文件分隔符读取文件

转载 作者:行者123 更新时间:2023-11-30 06:44:16 25 4
gpt4 key购买 nike

我正在尝试读取 java 中的文件内容,但我需要使其独立于平台。所以,我做到了

FileInputStream fis = new FileInputStream(new File(File.separator + "com" + File.separator + "test.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(fis));

我正在尝试在 Eclipse 中运行它。但我收到了FileNotFoundExcption。现在我的 test.txt 与我的源文件位于同一位置。请任何人都可以指导我完成它。 Eclipse 到底在哪里寻找这个文件?提前致谢..

最佳答案

File a = new File(File.separator + "com" + File.separator + "test.txt");
System.out.println(a.getAbsolutePath());

可能会输出一些你没有预料到的东西。

如果您发布返回的内容,我可以告诉您您的问题是什么。

它可能使用项目目录,您可以使用以下方式输出:

how to Locate the path of the Current project in Java, Eclipse?


解决方案:

如果Windows上的路径是:

C:\com\test.txt

但是在 Linux 上你想要:

/com/test.txt

您想使用:

new File(System.getenv("SystemDrive") + File.separator + "com" + File.separator + "test.txt")

因为这将按上述方式运行。

关于java - 在java中使用文件分隔符读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50842218/

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