This question already has answers here:
How to convert a Hadoop Path object into a Java File object
(3 answers)
Closed 5 years ago.
I am working on code which processes File objects. I would like to store the actual files in HDFS, but to retrieve them as File objects.
I found the method pathToFile (Hadoop LocalFileSystem), which supposedly does exactly what I want, but it doesn't seem to work.
There is another question with almost the same topic (How to convert a Hadoop Path object into a Java File object) but it isn't solved.
Is there anyone who actually used the method and converted a Hadoop Path to a Java File?
fyi: I searched all over the Internet and I couldn't find not even a chunk of code where the pathToFile is used and works efficiently.
In my code:
String uri = args[0]; //give the hdsf path as argument
Configuration conf = new Configuration(); //create a Configuration obj
Path pathOfFile = new Path(uri); //create a Hadoop Path obj
LocalFileSystem myFS = FileSystem.getLocal(conf); //LocalFileSystem creation
File theFile = myFS.pathToFile(pathOfFile); // using pathToFile
Perhaps this piece of code could help replace /etc/hadoop/conf with your local config
Configuration configuration = new Configuration();
configuration.addResource("/etc/hadoop/conf/core-site.xml");
configuration.addResource("/etc/hadoop/conf/core-site.xml");
configuration.addResource("/etc/hadoop/conf/hdfs-site.xml");
FileSystem hdfsFileSystem = FileSystem.get(configuration);
Path pathOfFile = new Path("/user/giorgos/test.txt"));
Related
This question already has answers here:
How to read and write excel file
(22 answers)
Closed 3 years ago.
What should be the syntax when the excel file that I'm trying to call is imported within the project.
The syntax you are using is correct but make sure your using the correct path of your local file.
When your local file is in the same package/folder where you write your codes.Just write down its file name.
var file = new FileInputStream("Dashboard.xlsx");
or try using Absolute path of your filename.
var file = new FileInputStream("M3A FrontEndPoc/Dashboard.xlsx");
Use :
public class ReadFileExcel
{
public static void main(String[] args) throws IOException
{
ClassLoader classLoader = new ReadFileExcel.getClass().getClassLoader();
File file = new File(classLoader.getResource("./dashboards.xlsx").getFile());
// replace the below one with the above but just change the name of the class
var file = new FileInputStream("Dashboard.xlsx");
}
}
Note : Build the project, otherwise classloader will not able to read.
This question already has answers here:
How to get the real path of Java application at runtime?
(15 answers)
Closed 6 years ago.
I'm trying to load a .txt file into an arrayList in java using a combination of relative paths.
My jar file is in /usr/tool/dist/tool.jar
The file I want to load is in /usr/tool/files/file.txt
I think I was able to retrieve the path of my tool.jar, but how can I go from that path to the one where my file is?
I have the following code
// String path should give me '/usr/tool'
File f = new File(System.getProperty("java.class.path"));
File dir = f.getAbsoluteFile().getParentFile();
String path = dir.toString();
String table1 = this should represent /usr/tool/files/file.txt
BufferedReader buf_table1 = new BufferedReader(new FileReader(new File(table1)));
To find the path of your jar file being executed, java.class.path is not the right property. This property may contain more than one file, and you cannot know which is the right one. (See the docs.)
To find the path of the correct jar file, you can use this instead:
URL url = MainClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Where MainClass the main class of your tool, or any other class in the same jar file.
Next, the parent File of a file is its directory. So the parent File of /usr/tool/dist/tool.jar is /usr/tool/dist/. So if you want to get to /usr/tool/files/file.txt, you need to get the parent of the parent, and then from there files/file.txt.
Putting it together:
File jarFile = new File(MainClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
File file = new File(jarFile.getParentFile().getParent(), "files/file.txt");
This question already has answers here:
Read properties file outside JAR file
(8 answers)
How to get the path of a running JAR file?
(33 answers)
Closed 6 years ago.
I have a folder structured like this:
MyFolder:
file1.xml
file2.xml
project.jar
But if in a class I use:
File f = new File("file1.xml");
I receive an error, because it doesnt find the file. Why?
You should use a relative path in your code.
Example: File f = new File("./file1.xml");
If you are using Windows the code you posted will work, but not on Linux where the default parent file is your home.
But you can do in any OS by using:
public class MyClass {
public void loadFile() {
URL url = MyClass.class.getProtectionDomain().getCodeSource().getLocation();
File jar = new File(url.toURI());
File f = new File(jar.getParent(), "file1.xml");
//your code
}
}
PS: This needs to be inside project.jar because you are getting the location where you jar file is.
This question already has answers here:
java.io.FileNotFoundException: the system cannot find the file specified
(8 answers)
Closed 4 years ago.
My program get a filename in parameter but if i want to use it i need the full directory path or i get filenotfoundexception.
For example:
My program got sample.txt in parameter from C:\Users\me\documents.
File file = new File(args[0]);
FileReader fr = new FileReader(file);
That throw filenotfoundexception.
So what should i use to locate the file?
I saw so many similar question but i didnt find solution :(
I tried to use getResources and getPath but nothing.
You can open a file with just the filename if that file exists in the same directory as of your source code. If the file is located at any random location then you need to give the complete path of the file along with its name.
Eg: c:\documents\sample.txt
Or another thing that you can try is recursively going through all folders present in your file system and locate the file. However, this is will be a very horrible solution.
File fileName = new File("myfile.txt");
if(!fileName.exists()) {
fileName.createNewFile();
}
FileOutputStream oFile = new FileOutputStream(fileName, false);
add this code if your file is not on the location this will create a one for you then you wont be getting filenotfound exception at the latter part
This question already has answers here:
FileNotFoundException, the file exists Java [closed]
(2 answers)
Closed 7 years ago.
Hello I have this in my code
File file = new File("words.txt");
Scanner scanFile = new Scanner(new FileReader(file));
ArrayList<String> words = new ArrayList<String>();
String theWord;
while (scanFile.hasNext()){
theWord = scanFile.next();
words.add(theWord);
}
But for some reason I am getting a
java.io.FileNotFoundException
I have the words.txt file in the same folder as all of my .java files
What am I doing wrong? Thanks!
Tip: add this line to your code...
System.out.println(file.getAbsolutePath());
Then compare that path with where your file actually is. The problem should be immediately obvious.
The file should reside in the directory from which you execute the application, i.e. the working directory.
Generally it's a good idea to package data files with your code, but then using java.io.File to read them is a problem, as it's hard to find them. The solution is to use the getResource() methods in java.lang.ClassLoader to open a stream to a file. That way the ClassLoader looks for your files in the location where your code is stored, wherever that may be.
try:
URL url = this.getClass().getResource( "words.txt" );
File file = new File(url.getPath());
You haven't specified an absolute path. The path would therefore be treated as a path, relative to the current working directory of the process. Usually this is the directory from where you've launched the Main-Class.
If you're unsure about the location of the working directory, you can print it out using the following snippet:
System.out.println(System.getProperty("user.dir"));
Fixing the problem will require adding the necessary directories in the original path, to locate the file.