Why is this check not return true?
String dirpath = getClass().getResource("../util/assets/sounds/").toString();
File dir = new File(dirpath);
System.out.println(dirpath);
System.out.println(dir);
System.out.println(dir.isDirectory());
It returns these:
file:/D:/JAVA/exercises/FX/TasksList/build/classes/taskslist/util/assets/sounds
file:\D:\JAVA\exercises\FX\TasksList\build\classes\taskslist\util\assets\sounds
false
I run it using netbeans IDE as a source code.
getClass().getResource(...) returns a URL, not a file path string. A file path string is needed by the constructor of File.
You can try using the Paths constructor to construct a valid file path from a URI, which you can get from the resource using toUri().
So, this should work:
Files.isDirectory(
Paths.get(
getClass().getResource("../util/assets/sounds/").toUri()
)
)
Or
new File(
getClass().getResource("../util/assets/sounds/").toUri()
).isDirectory();
I haven't got a windows machine to try that on.
Note: If you start packaging your app as jar (as is recommended), then this technique will break. The resource would not be a file, but instead a jar resource. There is no concept of a file directory in relation to a jar resource.
It supposed to be :
String dirpath = getClass().getResource("../util/assets/sounds/").getPath();
instead of:
String dirpath = getClass().getResource("../util/assets/sounds/").toString();
And it will work.
Related
Trying to get the files from filepath using java.io.File "File f = new File("//DEV/Test")" in beanshellsampler JMeter configured in Jenkins, when try to get the absolute path, getting only "/Dev/Test". one '/' is missing.
String folderPath = "//Dev/Test";
#oneway
File folder = new File(folderPath);
folder.toAbsolutePath(); //output :: /Dev/Test
#Secondway
Path p1 = Paths.get(foldertoString, new String[0]);
File pathtoFile = p1.toFile();
String[] fn = pathtoFile.list();
// output : path is not correct
It's not missing, / is the root of the Linux file system, there is no need to have an extra slash there.
Also be informed that starting from JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so consider migrating on next available opportunity.
More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?
i want to ask for help with opening a file from c# app with associated app.
I tried this:
ProcessStartInfo pi = new ProcessStartInfo(file);
pi.Arguments = Path.GetFileName(file);
pi.UseShellExecute = true;
pi.WorkingDirectory = Path.GetDirectoryName(file);
pi.FileName = file;
pi.Verb = "OPEN";
Process.Start(pi);
or this:
Process.Start(file);
where string file in both examples represents full path to the file trying to open. Now, everything is working well, except the (jpg) images with ACDSee app. Irfanview associations works well, MS office documents too. After trying to open the jpg image associated with acdsee it just runs the acdsee in the notification area and does not open the file.
I discovered, that in the registry CLASSES_ROOT for *.jpg images, there is an ACDSee.JPG value as associated app, and under this key there is in the shell->Open->Command a path:
"C:\Program Files\ACD Systems\ACDSee\ACDSee.exe" /dde
and I thing that this weird /dde is the reason, why i cannot open the file. I realized that in the same reg key shell->Open there is some DDEExec key entry with value [open("%1")]
For Irfan view or other checked app there is not a ddeexec, just the normal command like
"C:\Program Files (x86)\IrfanView\i_view32.exe" "%1"
that can be run from command line after swaping the %1 for file name, but I could not run the command from acdsee entry in the command line :(
So my question is, how can I set up the ProcessStartInfo object to ensure that it will run all the files as it would be in the explorer by doubleclick, the standards and this DDEExec ones? Is there something other like DDEExec that I shoul be aware of?
thanks and sorry for my EN
UPDATE: because this question still gets upvotes, I want to clarify that accepted answer works. I only had problem with old version of ACDSee and not with the Process.Start command or with the jpg extension.
Just write
System.Diagnostics.Process.Start(#"file path");
example
System.Diagnostics.Process.Start(#"C:\foo.jpg");
System.Diagnostics.Process.Start(#"C:\foo.doc");
System.Diagnostics.Process.Start(#"C:\foo.dxf");
...
And shell will run associated program reading it from the registry, like usual double click does.
In .Net Core (as of v2.2) it should be:
new Process
{
StartInfo = new ProcessStartInfo(#"file path")
{
UseShellExecute = true
}
}.Start();
Related github issue can be found here
This is an old thread but just in case anyone comes across it like I did.
pi.FileName needs to be set to the file name (and possibly full path to file ) of the executable you want to use to open your file. The below code works for me to open a video file with VLC.
var path = files[currentIndex].fileName;
var pi = new ProcessStartInfo(path)
{
Arguments = Path.GetFileName(path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(path),
FileName = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe",
Verb = "OPEN"
};
Process.Start(pi)
Tigran's answer works but will use windows' default application to open your file, so using ProcessStartInfo may be useful if you want to open the file with an application that is not the default.
File path=new File(".") ? What does this code line do ?
File path=new File(args[0]) Does it gives absolute or relative path to the file with name given in args[0]?
your first line will create a Java File object which will point to the current directory i.e. the directory you started your Java program from. You can call isDirectory() on it and you should get true. Read the Java 7 javadocs here.
your second line will create a Java File object which will point to args[0]. It does not verify that the file exists or is readable or writable. This will be checked when you start trying to work with the file.
You can determine the absolute path of a File object using method getAbsolutePath().
I have the below code.
In my base directory where the main CodeIgniter PHP index file is, I have a directory pagemodules with permission 666.
What I want to do is write in it a file in the following filename format: .main.control.function.var.something.mod
To dynamically create files in this folder I use the below code to check if the file exists, if not, create it with a single space in it.
The only problem is, I come up with a fail every time. I have tried moving the dir into every possible directory to see if it's a file path problem, but that didn't help.
The CodeIgniter wiki isn't much help either because it gives no pointers on how to diagnose errors from the file helper. And no error is thrown whatsoever...
How can I get CodeIgniter to throw an error I can do something with?
I could start working with fopen, but I prefer to use CodeIgniter because of ease of use.
error_reporting(E_ALL);
ini_set("display_errors", 1);
$this->load->helper('file');
if($createfile!=null)
{
$filepath = './pagemodules/'.str_replace('/','.',$data['page']->url).'.mod';
$file = read_file($filepath);
if($file === false)
{
$test = write_file($filepath,' ','w+');
if($test)
{
echo "we have written $filepath";
}
else
{
echo 'fail '.$filepath;
}
}
}
well, just for debug purposes:
Replace './pagemodules/ with 'FCPATH.'pagemodules/ in order to get absolute path to pagemodules (if it is located at index.php codeigniter directory).
To avoid an invalid path, you can use...
get_filenames('path/to/pagemodules/')
...to get its files and corroborate if it is correct.This function return and array with file names into it.
Remember that you can use FCPATH constant to get the absolute path to Codeigniter installation.
I am trying to access a resource file from a servlet but getting HTTP Error 500 - access denied:
File file = new File("//warChildFolder//myFile.txt");
InputStream is = new FileInputStream(file); // <--error on this line
I am on google-app-engine.
Any help appreciated!
Google App Engine docs talk about "white listing" a file. Is that in play here?
I also wonder about this:
File file = new File("//warChildFolder//myFile.txt");
Doesn't the leading slash make this an absolute path?
I'd try it like this:
File file = new File("WEB-INF/warChildFolder/myFile.txt");
Make the path relative to the WAR root and be explicit about WEB-INF.
I'm not sure about Google App Engine but in my experience the only solution that works across containers and platforms is to use ServletContext.getRealPath().
new File(servletContext.getRealPath("/WEB-INF/warChildFolder/myFile.txt"));
The spec says: use forward slashes and a leading slash. This gives you platform independence and you're not relying on the process' current directory.
Does it work if you use single path separators?
(updated to use relative paths):
File file = new File("warChildFolder/myFile.txt");
You need to escape the "\" character in Strings, so use "\", but a single "/" is all that is needed.
Update: It may be that the path being processed is not the same as you expect, you can try logging the absolute path of the file (with file.getAbsolutePath()) to check this.
Another thing to check is that the process has read permissions on the folder/file. If you're not on Windows this can be a problem.