Scala: Create File with a Folder Using Relative Path (Not Absolute Path) - scala

In my Scala code I want to create a folder "c:/temp" and then create a file "file.txt" within that folder. I don't want to have to use "c:/temp/file.txt". So, I want to use the relative path of the file to create it within that folder.
Imagine how a human creates a folder and then a file? He creates a folder; goes in the folder, and then creates the file inside that folder. That's what I want to do.
=====
Added the following to make this more clear:
Let's say I created the folder and I have a File object called myFolder that represents that folder. What I want is to be able to do something like myFolder.createFile("file.txt").

val subFile = new File(myFolder, "file.txt")
From the description of the File(File parent, String child) constructor found at the docs page:
Creates a new File instance from a parent abstract pathname and a child pathname string.

Related

Get list of files from a folder inside a packaged project

I have a list of public assets inside the "src/main/resources" folder that looks like this:
public/images/
asd1.png
asd2.png
I'm trying to list all the files inside the "public/images/" directory, as it is a compiled project using sbt universal:packageBin I'm trying to access the assets using:
val input = getClass.getResourceAsStream("/public/images/")
or
val input = getClass.getResource("/public/images/")
getResourceAsStream returns an empty result and getResource returns a URL object that contains a direct path to the folder but I cannot list anything inside that folder even with new File(getClass.getResource("/public/images/")) as the check for .isDirectory() returns a false as a response or .listFiles() throws an exception.
What is the best way to achieve this?

How to create a link of resources under virtual folder in Eclipse

I m trying to create a virtual folder inside a folder in eclipse. I want to link some files to this virtual folder but I m not able to link.
Below is the sample code:
IFolder folder = modelFolder.getFolder("Script_include");
folder.create(IResource.VIRTUAL, false, null);
IPath newLocation = new Path("/Users/dinesh.lodhi");
folder.createLink(newLocation, IResource.NONE, null);
What I exactly want is to create some virtual folders inside one of the folder at the startup of my plugin and want to link some of resources into it.
The given path must be either an absolute file system path, or a relative path whose first segment is the name of a workspace path variable.
I was mistakenly using folder.getFullPath() for getting location.
and one more thing there is no need to
folder.create(IResource.VIRTUAL, false, null);
as createLink() method itself Creates a new file/folder resource as a member of this handle's parent resource.
Thanks

Loading file using a path writen in edit box matlab

hello evryone
wanna load file from a specific path writen in an edit box named by 'Load_text', i got the path from the edit box using :
pth=get(handles.Load_text,'string');
then i used 'dir' as follow:
S=dir(fullfile([pth '*.bmp']));
that what cause me an errur . so any ideas ?
If you want to load a file then why you are using dir?
Since you have the file's name, then you can create the fullpath as you do with the fullpath method and check its existence with exists.
If the file exists, then you can load it with all available methods from MATLAB.
Keep in mind that this means that the file is in the same directory as your GUI files. If it is in another then you will have to add it in the fullpath call.
fullpath online doc: http://www.mathworks.com/help/matlab/ref/fullfile.html
exists online doc: http://www.mathworks.com/help/matlab/ref/exist.html

How to list out all the files in the public/images directory in a Play Framework 2 Scala application?

I want to list out all the file names in the public/images directory for my Scala Play Framework 2 application so that the Play Application just lists those file names to the HTML page. How can this be done?
Is there a right way to access the public/images directory to read files or list the directory? I don't want to use absolute directory paths.
There is method getFile(relateivePath: String): File on Play object and also in the Application class which returns file relative to application root directory. In your case:
Play.getFile("public/images")
return File object, which represent public/images/directory. You can call list or listFiles on it to get contents of the directory.

ResourceException when creating IMarker on IFile (linked resource)

I have some problems updating an "old" Eclipse plugin. Here is what I would like to do and what the original plugin did:
(parse compiler output on console with file name and error information --> still works)
--> set link to the location within the file
--> set marker to location in the file
What I did in the past was to get the IFile from the path String of the file and generated link and marker from it:
IFile ifile;
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath path = new Path(fileName);
IFiles[] files = workspace.getRoot().findFilesForLocation(path);
...
ifile = iFiles[0];
Map attributes = new HashMap();
attributes.put(IMarker.SEVERITY, new Integer (severity));
MarkerUtilities.setLineNumber(attributes, lineNumber);
MarkerUtilities.setMessage(attributes, message);
MarkerUtilities.createMarker(ifile, attributes,
IMarker
Since findFilesForLocation is deprecated, I tried to find another way but I am not succeeding whatsoever. Using the changed code to get the IFile always results in a exception: org.eclipse.core.internal.resources.ResourceException: Resource '/path/to/file.c' does not exist.
Is it possible that this relates to the fact that the source file is only linked into the project, and not physically within the project?
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath location = new Path(fileName);
IFile ifile = workspace.getRoot().getFile(location);
Can anyone help?
Thank you!
I am guessing that fileName is the fully qualified path to the file you want to get. I'm also guessing that the file that you are looking for is already in the workspace, even if it is linked (if not, then this won't work. You will first need to add the file to a project before getting the IFile for it).
You need to do something like this:
IFiles[] files = workspace.getRoot().findFilesForLocationURI("file:" + fileName);
Then this will find all files in the workspace that correspond to the file in the file system.
The reason why your attempt above is giving you a ResourceException is that you are trying to pass in a file system path to get an IFile object from the workspace. The Eclipse workspace is an abstraction over the underlying filesystem and cannot directly work with absolute paths.
For the Resources APIs, Paths usually means a path in the workspace and Location usually refers to a place in the filesystem or outside the workspace. If you already have a workspace path to start with, just ask the IWorkspaceRoot for the IFile and get on with what you're doing.