I am looking at code new FileInputStream("config.properties").
I have the same file "config.properties" in multiple places in my project(doing windows file search) and I am now confused as to which one does this function call refer to. How do i get to know the absolute path of file?
I found this on the internet but this location doesnt look like the right answer.
"ClassName".class.getProtectionDomain().getCodeSource().getLocation().getPath() but this doesnt look it. Can you please correct it if I am wrong
You can use File:
File f = new File("config.properties");
System.out.println(f.getAbsolutePath());
The path returned will be deduced from the current working directory.
File f = new File("config.properties");
String dirPath = file.getParentFile().getAbsolutePath()
Related
I try to write a text to a file and read this text later. When I use FileWriter I become a NullPointerException?
Is that a permission problem or ...? I also try the PrintWriter but I see the same Exception
.
This my code:
FileWriter fw = new FileWriter(new File("file.file"));
fw.write("XYZ");
best regards
londi
I guess your problem is that you use a relative file path, but that the origin of the relative path is not the one you think.
First of all, try to use an absolute path, that would be, on linux-like machines something like /home/me/myCode/myfile.txt or on windows something like c:/some/path/myfile.txt
Another thing you can do, in order to know what happens is print the origin.
File origin = new File(".");
System.out.println(origin.getAbsolutePath());
Once you know where the origin is, you can see what you need in order to get to your file.
Hope it will help.
Sounds like a permission issue. On iOS your application lives within a security sandbox, so you cannot just randomly read and write files anywhere you want. You could either use File.createTempFile to create a temp file somewhere hidden you your sandbox where nothing else can see it, or use the native api to determine where to dump your files. The following example will give you a file reference to the Documents Directory folder:
NSArray nsa = NSFileManager.defaultManager().URLsForDirectory$inDomains$(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask);
NSURL nsu = (NSURL)nsa.getFirst();
String snsu = nsu.getAbsoluteString() + "MyNewDocument.pdf";
File newFile = new File(new URI(snsu));
I'm trying to write in a file with a path like this:
D:\abcd\efgh\..\ijkl\file.txt
So I have an File object with such a path, but in the line
FileOutputStream fos = new FileOutputStream(f);
I get this:
java.io.FileNotFoundException: ..\ijkl\file.txt (The system cannot find the path specified)
Does anybody know what's wrong here? Is there a possibility to resolve the path in an absolute path?
Initialisation of the File object:
File f = new File(strImagePath);
strImagePath is built out of different Strings and looks exactly like the path shown above.
Thanks!
According to your code java try to access the folder D:\abcd\ijkl\file.txt as you have place ..\ijkl\file.txt but in your system doesnot have any file on that path. Thus you are getting the error.
Edit: can you please, try using D:\\abcd\\efgh\\../efgh\\ijkl\\file.txt
Is there a easy way to get the filePath provided I know the Filename?
You can use the Path api:
Path p = Paths.get(yourFileNameUri);
Path folder = p.getParent();
Look at the methods in the java.io.File class:
File file = new File("yourfileName");
String path = file.getAbsolutePath();
I'm not sure I understand you completely, but if you wish to get the absolute file path provided that you know the relative file name, you can always do this:
System.out.println("File path: " + new File("Your file name").getAbsolutePath());
The File class has several more methods you might find useful.
Correct solution with "File" class to get the directory - the "path" of the file:
String path = new File("C:\\Temp\\your directory\\yourfile.txt").getParent();
which will return:
path = "C:\\Temp\\your directory"
You may use:
FileSystems.getDefault().getPath(new String()).toAbsolutePath();
or
FileSystems.getDefault().getPath(new String("./")).toAbsolutePath().getParent()
This will give you the root folder path without using the name of the file. You can then drill down to where you want to go.
Example: /src/main/java...
I think I am really close, but I am unable to open a file I have called LocalNews.txt. Error says can't find file specified.
String y = "LocalNews.txt";
FileInputStream fstream = new FileInputStream(y);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
Name of file is LocalNews.txt in library called News....anyone know why the file will not open?
The file is in the same Java Project that I am working on.
Error: LocalNews.txt (The system cannot find the file specified)
Project is named Bst, package is src in subPackage newsFinder, and library that the text files are stored in is called News.
Found out it was looking in
C:\EclipseIndigoWorkspace1\Bst\bin\LocalNews.txt
But I want it to look in (I believe)
C:\EclipseIndigoWorkspace1\Bst\News\LocalNews.txt
But if I make the above url a string, I get an error.
String y = "LocalNews.txt";
instead use
String y = "path from root/LocalNews.txt"; //I mean the complete path of the file
Your program can probably not find the file because it is looking in another folder.
Try using a absolute path like
String y = "c:\\temp\\LocalNews.txt";
By 'library called News' I assume you mean a jar file like News.jar which is on the classpath and contains the LocalNews.txt file you need. If this is the case, then you can get an InputStream for it by calling:
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("LocalNews.txt");
Use
System.out.println(System.getProperty("user.dir") );
to find out what your current directory is. Then you'll know for sure whether your file is in the current directory or not. If it is not, then you have to specify the path so that it looks in the right directory.
Also, try this -
File file = new File (y);
System.out.println(file.getCanonicalPath());
This will tell you the exact path of your file on the system, provided your file is in the current directory. If it does not, then you know your file is not in the current directory.
I have the same problem as the guy in this post:
Get the absolute path of the currently edited file in Eclipse
but I dont understand how to do it. In the post they say that I should use IResource.getRawLocation() but how to I get the IResource from the current file that I am viewing?
IFile file2 = (IFile)workBench.getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
IPath path = file2.getRawLocation().makeAbsolute();
String ss = path.toString();
System.out.println(ss);
But this will still not give me the right path to the linked file, I get something like:
/path/to/plugin/path/to/relative/path/to/file
An IFile is-an IResource. You don't need to get one from another. Just call file2.getRawLocation(). If that's not working it's not because it's not an IResource.
Is the file in a jar/zip/any archive file? If so, it looks like that could be the correct absolute path.