从另一个目录读取.txt文件 [英] Reading .txt file from another directory
问题描述
我正在运行的代码在 / Test1 / Example
中。如果我需要在 / Test1
中读取一个.txt文件,我该如何让Java在目录树中回到1级,然后阅读我的.txt文件。 >
我已经搜索/谷歌搜索,还没有找到一种方法来读取不同位置的文件。
我我在位于/Test1/Test2/testing.htm的.htm文件中运行一个java脚本。它在哪里说脚本 src =
。我将把它从我的文件中找到,它位于 /Test1/example.txt
。
在Java中,您可以使用getParentFile()遍历树。所以你在/ Test1 / Example目录中启动你的程序。并且您要将新文件写入/Test1/Example.txt
文件currentDir = new File(。);
文件parentDir = currentDir.getParentFile();
文件newFile = new File(parentDir,Example.txt);;
显然有多种方法可以实现。
The code I am running is in /Test1/Example
. If I need to read a .txt file in /Test1
how do I get Java to go back 1 level in the directory tree, and then read my .txt file
I have searched/googled and have not been able to find a way to read files in a different location.
I am running a java script in an .htm file located at /Test1/Test2/testing.htm. Where it says script src=" "
. What would I put in the quotations to have it read from my file located at /Test1/example.txt
.
In Java you can use getParentFile() to traverse up the tree. So you started your program in /Test1/Example directory. And you want to write your new file as /Test1/Example.txt
File currentDir = new File(".");
File parentDir = currentDir.getParentFile();
File newFile = new File(parentDir,"Example.txt");;
Obviously there are multiple ways to do this.
这篇关于从另一个目录读取.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!