Reading .txt file from another directory

独自空忆成欢 提交于 2019-12-31 05:01:27

问题


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.


回答1:


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.




回答2:


You should be able to use the parent directory reference of "../"

You may need to do checks on the OS to determine which directory separation you should be using ['\' compared to '/']




回答3:


When you create a File object in Java, you can give it a pathname. You can either use an absolute pathname or a relative one. Using absolutes to do what you want would require:

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

Using relatives paths if you want to run from the location /Test1/Example:

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



回答4:


I had a similar experience. My requirement is: I have a file named "sample.json" under a directory "input", I have my java file named "JsonRead.java" under a directory "testcase". So, the entire folder structure will be like untitled/splunkAutomation/src and under this I have folders input, testcase.

once after you compile your program, you can see a input file copy named "sample.json" under a folder named "out/production/yourparentfolderabovesrc/input" and class file named "JsonRead.class" under a folder named "out/production/yourparentfolderabovesrc/testcase". So, during run time, Java will actually refer these files and NOT our actual .java file under "src".

So, my JsonRead.java looked like this,

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

This will give you the output like, fileURL: file:/C:/Users/asanthal/untitled/out/production/splunkAutomation/input/sample.json fileIs: C:\Users\asanthal\untitled\out\production\splunkAutomation\input\sample.json




回答5:


It worked for me. I was saving all my classes on a folder but I needed to read an input file from the parent directory of my classes folder. This did the job.

String FileName = "Example.txt";

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


来源:https://stackoverflow.com/questions/5168515/reading-txt-file-from-another-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!