I am working on an application that stores files under /WEB-INF/someFolder/. But I dont find the right way to create files under this folder. I did this, but it is not working:
File newFile = new File("/WEB-INF/fileName.xml");
When I try to check the creation:
boolean isCreated = newFile.createNewFile();
I get :
java.io.IOException: No such file or directory
Please help me doing it in the right way.
Update:
I did this workaround, it is working but I dont see that it is performant solution.
ServletContext servletContext = getServletContext();
String path = servletContext.getRealPath("/WEB-INF/");
File newFile2 = new File(path+"/fileName.xml");
Any ideas?
You shall use ServletContext.getRealPath(String) and build the entire classpath manually
String webInfPath = getServletConfig().getServletContext().getRealPath("WEB-INF");
OR go step by step:
ServletConfig scfg= getServletConfig();
ServletContext scxt = scfg.getServletContext();
String webInfPath = sxct.getRealPath("WEB-INF");
And than use the webInfPath to create a File object inside WEB-INF
File newFile = new File(webInfPath + "/fileName.xml");
make sure you applaction have the permissions to write.
you can get the path ues like this:
String path=Thread.currentThread().getContextClassLoader().getResource("com/youpackage/");
Now you get the path which is your class folder path,so you can get the WEB-INF path.
ps: i remember when create file you must writer some content,otherwies it may not create.
Related
I have shapefiles in my jar which is deployed in OpenShift. I'm able to access these files from my local environment by using "File sourceFile = new ClassPathResource("src/main/resources/CountryBoundaries.shp).getFile();". However, in OpenShift, my service complains that it couldn't find it. I have tried InputStream but I get an error in my IDE because the file is not a text file. Below is my code:
File sourceFile = new ClassPathResource("src/main/resources/CountryBoundaries.shp").getFile();
FileDataStore store = FileDataStoreFinder.getDataStore(sourceFile);
featureSource = store.getFeatureSource();
GeometryDescriptor geomDesc = featureSource.getSchema().getGeometryDescriptor();
attrName = geomDesc.getLocalName();
What am I missing? I'll appreciate any help as I've been struggling with this for quite a while now. Thanks.
I found a solution by changing URL to URI and then doing a toURL(). See below:
URI sourceFile = new ClassPathResource("PoliticalSubdivisionBoundaries.shp").getURI();
FileDataStore store = FileDataStoreFinder.getDataStore(sourceFile.toURL());
featureSource = store.getFeatureSource();
GeometryDescriptor geomDesc = featureSource.getSchema().getGeometryDescriptor();
attrName = geomDesc.getLocalName();
Hope this helps someone in the future. Shapefiles are complex and cannot be accessed the usual way like using inputStream and such. Cheers!
In my code I am prompting the user to load a json file.
I am then attempting to copy this file into an sqlite database.
Once I have the data I am then able to manipulate it as needed - but I need to get it there in the first place.
So step 1 is to get the data in.
I have progressed as far as prompting the user to navigate to the file they want - but when I try and read the file I get this error ..
ERROR: resources must reside in the root directory thus must start with a '/' character in Codename One! Invalid resource: file:///tmp/temp3257201851214246357..json
So I think that I need to copy this file to the root directory
I cannot find a link that shows me how to do this.
Here is my code so far ...
case "Import Script":
try
{
JSONParser json = new JSONParser();
if (FileChooser.isAvailable()) {
FileChooser.showOpenDialog(".json", e2-> {
String file = (String)e2.getSource();
if (file == null) {
home.add("No file was selected");
home.revalidate();
} else {
home.add("Please wait - busy importing");
home.revalidate();
String extension = null;
if (file.lastIndexOf(".") > 0) {
extension = file.substring(file.lastIndexOf(".")+1);
}
if ("json".equals(extension)) {
FileSystemStorage fs = FileSystemStorage.getInstance();
try {
InputStream fis = fs.openInputStream(file);
try(Reader r = new InputStreamReader(Display.getInstance().getResourceAsStream(getClass(), file), "UTF-8"))
{
Map<String, Object> data = json.parseJSON(r);
Result result = Result.fromContent(data);
...... I progress from here
The error is occurring on this line ...
try(Reader r = new InputStreamReader(Display.getInstance().getResourceAsStream(getClass(), file), "UTF-8"))
If I hard code a filename and manually place it in the /src folder it works ... like this ...
try(Reader r = new InputStreamReader(Display.getInstance().getResourceAsStream(getClass(), '/test.json'), "UTF-8"))
But that defeats the purpose of them selecting a file
Any help would be appreciated
Thanks
I suggest watching this video.
It explains the different ways data is stored. One of the core sources of confusion is the 3 different ways to store files:
Resources
File System
Storage
getResourceAsStream returns a read only path that's physically embedded in the jar. It's flat so all paths to getResourceAsStream must start with / and must have only one of those. I would suggest avoiding more than one . as well although this should work in theory.
The sqlite database must be stored in file system which is encapsulated as FileSystemStorage and that's really the OS native file system. But you can't store it anywhere you want you need to give the DB name to the system and it notifies you where the file is stored and that's whats explained in the code above.
I have an application that needs to load an add-on in the form of a dll. The dll needs to take its configuration information from a configuration (app.config) file. I want to dynamically find out the app.config file's name, and the way to do this, as I understand , is AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
However, since it is being hosted INSIDE a parent application, the configuration file that is got from the above piece of code is (parentapplication).exe.config. I am not able to load another appdomain inside the parent application but I'd like to change the configuration file details of the appdomain. How should I be going about this to get the dll's configuration file?
OK, in the end, I managed to hack something together which works for me. Perhaps this will help;
Using the Assembly.GetExecutingAssembly, from the DLL which has the config file I want to read, I can use the .CodeBase to find where the DLL was before I launched a new AppDomain for it. The *.dll
.config is in that same folder.
Then have to convert the URI (as .CodeBase looks like "file://path/assembly.dll") to get the LocalPath for the ConfigurationManager (which doesn't like Uri formatted strings).
try
{
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
string originalAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
Uri uri = new Uri(String.Format("{0}\\{1}.dll", originalAssemblyPath, assemblyName));
string dllPath = uri.LocalPath;
configuration = ConfigurationManager.OpenExeConfiguration(dllPath);
}
catch { }
In Qt in a Linux environment, how would I go about specifying that one file my application creates goes into a specific, hard-coded directory while another file created at the same time goes into a different hard-coded directory?
I am not sure what you want to do but does this work?
QFile aFile("/some/hard/coded/path/filename");
QFile anotherFile("/another/hard/coded/path/filename");
I'm not sure about files other than MOC, obj, ui, rcc, and executable files, but you can specify where each of them go with the following:
Release:DESTDIR = release
Release:OBJECTS_DIR = release/.obj
Release:MOC_DIR = release/.moc
Release:RCC_DIR = release/.rcc
Release:UI_DIR = release/.ui
Debug:DESTDIR = debug
Debug:OBJECTS_DIR = debug/.obj
Debug:MOC_DIR = debug/.moc
Debug:RCC_DIR = debug/.rcc
Debug:UI_DIR = debug/.ui
If you want to set the output path for other files that are generated, I don't know if it is possible (you might need a custom build step, though I might be wrong)
Im writing WPF application and want to add ability to call jump list and open program configuration, app.config or log directory from it. Is it possible(cant find the way to do that..just JumpTasks with application path and JumpPath with path to file, and not just path to be opened via explorer)?
Found answer here. Seems that JumpList wasnt designed for opening anything but files or applications, associated with current program. So that when we see directories in explorer tasklist -it actually means: use explorer with parameters. By the way ill try to use it.
Update
made it with such code:
string explorerPath = #"%windir%\explorer.exe";
JumpTask path = new JumpTask
{
CustomCategory = "Paths",
Title = "Open program directory",
IconResourcePath = explorerPath,
ApplicationPath = explorerPath,
Arguments = #"/root," + AppDomain.CurrentDomain.BaseDirectory,
Description = AppDomain.CurrentDomain.BaseDirectory
};
Im leaving this answer here, because someone can have similar incomprehension.