Reading .txt file from another directory
Solution 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.
Solution 2
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
}
Solution 3
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 '/']
Solution 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
Jason
Updated on January 23, 2020Comments
-
Jason over 4 years
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 fileI 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
.-
Mahesh about 13 yearsTry this -
../Test1/Example.txt
-
Reno about 13 yearsI'd like to see the code you wrote
-
justkt about 13 yearssounds like you are talking about Javascript, not Java?
-
-
Jason about 13 yearsWhat if I am running a java script in an .html document. <script src= "What would I put here to make it read from my .txt file in /Test1?
-
justkt about 13 years@Jason - that is a different question entirely. Might want to edit your question to clarify what you are doing.