开发者

Reading .txt file from another directory

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 scr开发者_C百科ipt 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.


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 '/']


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
}


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜