Java 从另一个目录读取.txt文件

Java 从另一个目录读取.txt文件,java,file-io,directory,text-files,Java,File Io,Directory,Text Files,我正在运行的代码位于/Test1/Example中。如果我需要在/Test1中读取.txt文件,如何让Java返回目录树中的1级,然后读取.txt文件 我已经搜索/谷歌搜索过了,但没有找到读取其他位置文件的方法 我在位于/Test1/Test2/testing.htm的.htm文件中运行java脚本。上面写着scriptsrc=”“。如果要从位于/Test1/example.txt的文件中读取,我会在引号中添加什么内容?您应该能够使用“./”的父目录引用 您可能需要在操作系统上进行检查,以确定应

我正在运行的代码位于
/Test1/Example
中。如果我需要在
/Test1
中读取.txt文件,如何让Java返回目录树中的1级,然后读取.txt文件

我已经搜索/谷歌搜索过了,但没有找到读取其他位置文件的方法


我在位于/Test1/Test2/testing.htm的.htm文件中运行java脚本。上面写着script
src=”“
。如果要从位于
/Test1/example.txt

的文件中读取,我会在引号中添加什么内容?您应该能够使用“./”的父目录引用


您可能需要在操作系统上进行检查,以确定应该使用['\'与'/'相比的目录分隔方式]

您应该能够使用“./”的父目录引用


您可能需要在操作系统上进行检查,以确定应该使用['\'而不是'/']

在Java中创建
文件
对象时,可以给它一个路径名。您可以使用绝对路径名或相对路径名。使用absolutes做您想做的事情需要:

File file = new File("/Test1/myFile.txt");
if(file.canRead())
{
    // read file here
}
如果要从位置/Test1/运行,请使用相关路径示例:

File file = new File("../myFile.txt");
if(file.canRead())
{
    // read file here
}

在Java中创建
文件
对象时,可以为其指定路径名。您可以使用绝对路径名或相对路径名。使用absolutes做您想做的事情需要:

File file = new File("/Test1/myFile.txt");
if(file.canRead())
{
    // read file here
}
如果要从位置/Test1/运行,请使用相关路径示例:

File file = new File("../myFile.txt");
if(file.canRead())
{
    // read file here
}

在Java中,可以使用getParentFile()遍历树。因此,您在/Test1/Example目录中启动了程序。您希望将新文件编写为/Test1/Example.txt

    File currentDir = new File(".");
    File parentDir = currentDir.getParentFile();
    File newFile = new File(parentDir,"Example.txt");;

显然有多种方法可以做到这一点。

在Java中,可以使用getParentFile()遍历树。因此,您在/Test1/Example目录中启动了程序。您希望将新文件编写为/Test1/Example.txt

    File currentDir = new File(".");
    File parentDir = currentDir.getParentFile();
    File newFile = new File(parentDir,"Example.txt");;

显然有多种方法可以做到这一点。

这对我来说很有效。我将所有的类保存在一个文件夹中,但是我需要从我的类文件夹的父目录中读取一个输入文件。这就成功了

String FileName = "Example.txt";

File parentDir = new File(".."); // New file (parent file ..)   
File newFile = new File(parentDir,fileName); //open the file

这对我有用。我将所有的类保存在一个文件夹中,但是我需要从我的类文件夹的父目录中读取一个输入文件。这就成功了

String FileName = "Example.txt";

File parentDir = new File(".."); // New file (parent file ..)   
File newFile = new File(parentDir,fileName); //open the file

我也有类似的经历。 我的要求是:在“input”目录下有一个名为“sample.json”的文件,在“testcase”目录下有一个名为“JsonRead.java”的java文件。因此,整个文件夹结构将类似于untitled/splunkAutomation/src,在这个文件夹下我有文件夹输入testcase

编译程序后,您可以在名为“out/production/yourparentfolderabovesrc/input”的文件夹下看到名为“sample.json”的输入文件副本,在名为“out/production/yourparentfolderabovesrc/testcase”的文件夹下看到名为“JsonRead.class”的类文件。因此,在运行时,Java实际上会引用这些文件,而不是“src”下的实际.Java文件

所以,我的JsonRead.java看起来像这样

package testcase;
import java.io.*;
import org.json.simple.JSONObject;

public class JsonRead{
public static void main(String[] args){
java.net.URL fileURL=JsonRead.class.getClass().getResource("/input/sample.json");
System.out.println("fileURL: "+fileURL);
File f = new File(fileURL.toURI());
System.out.println("fileIs: "+f);
}
}
这将为您提供如下输出:, fileURL:file:/C:/Users/asanthal/untitled/out/production/splunkAutomation/input/sample.json
文件是:C:\Users\asanthal\untitled\out\production\splunkAutomation\input\sample.json我也有类似的经历。 我的要求是:在“input”目录下有一个名为“sample.json”的文件,在“testcase”目录下有一个名为“JsonRead.java”的java文件。因此,整个文件夹结构将类似于untitled/splunkAutomation/src,在这个文件夹下我有文件夹输入testcase

编译程序后,您可以在名为“out/production/yourparentfolderabovesrc/input”的文件夹下看到名为“sample.json”的输入文件副本,在名为“out/production/yourparentfolderabovesrc/testcase”的文件夹下看到名为“JsonRead.class”的类文件。因此,在运行时,Java实际上会引用这些文件,而不是“src”下的实际.Java文件

所以,我的JsonRead.java看起来像这样

package testcase;
import java.io.*;
import org.json.simple.JSONObject;

public class JsonRead{
public static void main(String[] args){
java.net.URL fileURL=JsonRead.class.getClass().getResource("/input/sample.json");
System.out.println("fileURL: "+fileURL);
File f = new File(fileURL.toURI());
System.out.println("fileIs: "+f);
}
}
这将为您提供如下输出:, fileURL:file:/C:/Users/asanthal/untitled/out/production/splunkAutomation/input/sample.json
文件是:C:\Users\asanthal\untitled\out\production\splunkAutomation\input\sample.json

试试这个-
。/Test1/Example.txt
我想看看你写的代码听起来像是在说Javascript,而不是Java?试试这个-
。/Test1/Example.txt
我想看看你写的代码听起来像是在说Javascript,不是Java?如果我在.html文档中运行Java脚本呢@杰森-这是一个完全不同的问题。可能需要编辑您的问题以澄清您在做什么。如果我在.html文档中运行java脚本该怎么办@杰森-这是一个完全不同的问题。可能需要编辑您的问题以澄清您正在做什么。