Create a File in src directory of Eclipse plugin - eclipse

I want to create a file in the current directory of eclipse plugin.
I tried to create the file using :
File file = new File("filename.txt");
But, it is not working.
EDIT: I want to create the file in the same directory as that of "src" folder

Are you trying to create empty file within the pproject dir tree? or you want to create new file programatically? If you want to create empy file, go to File>New>Empty file (or similar, I do not have eclipse installed, Im writing out of memory), and if programatically: you have written line that initializes file at given path (if file cannot be initialized (no such file, or similar) - this will throw an exeption), but does not create it. To create file you need to write second instruction, just like:
file.create();

If the location (inside your project) is correct, then simply do this (not including try catch etc):
File test = new File("test.txt");
test.createNewFile();

Related

Flutter Dart File path

quick Flutter Dart issue.
I am trying to read from a text file located inside the app's directory, however Flutter does not recognize a single file path that I provide it. The file path is always responded to with "OS Error: No such file or directory, errno = 2".
I am trying to read from a file located in assets/importfiles, from a dart class inside lib/viewmodels. The assets directory has already been added to pubsec.yaml.
Here is the picture of my directory structure
I can even have the main class locate and print out the files' paths using rootBundle, but trying to initialize a new File using those paths provides the same error text.
If I were to use path provider to locate and read from files, how would I initially put the files into the Temporary Directory or Application Documents Directory, if Flutter does not recognize the file paths of the files that I want to insert?
Just You need to remove folder name ''.
assets\importfiles
to
assets Or importfiles
Thanks You.Happy To help you
Have you tried putting a "/" before "assets" in the file path?
I'm not sure that this will fix the error, let me know if it works!

How to get the location(path) of the unsaved/untitled new file created in vscode, using node js?

I'm new to creating extension in vscode. I am creating an extension that loads the file using 'fs' and make some changes to the file and write back the file. it works flawlessly for a file that is saved. But it shows:
Error: ENOENT: no such file or directory
for unsaved/untitled file.
I used const loc = vscode.window.activeTextEditor.document.uri.fsPath;
can someone please help me how to get the location of unsaved/untitled file.
An unsaved file does not and cannot have a path. It simply doesn't exist yet in the filesystem. When you create the editor, you will get a reference to the new content and that's it.

How to copy file from applicationStorageDirectory into dataDirectory?

I would like to copy an image file located in www/assets/imgs/christmas.jpg to the dataDirectory folder using the Ionic Native File Plugin, however I don't know how to access the www folder. I tried the following:
this.file.checkFile(this.file.applicationStorageDirectory, 'assets/imgs/christmas.jpg')
But it always returns NOT_FOUND_ERR.
You should change applicationStorageDirectory to applicationDirectory (although I am not sure what the difference is) and add www in front of the file path as the application directory contains the www folder and not its contents. So instead of assets/imgs/christmas.jpg use www/assets/imgs/christmas.jpg

file copy in scala play

i am to copy a file in scala But getting FileNotFound error, The assets folder is in the same directory where is src:
val src = new File("/assets/public/images/default/male.jpg")
val dest = new File("/assets/public/images/profile/male1.jpg")
new FileOutputStream(dest) getChannel() transferFrom(
new FileInputStream(src) getChannel, 0, Long.MaxValue )
In your code you are trying to copy the file using FileOutputStream, which requires a valid path to the existing file else it'll throw FileNotFoundException. (see the doc here)
val src = new File("/assets/public/images/default/male.jpg")
val dest = new File("/assets/public/images/profile/male1.jpg")
new FileOutputStream(dest) //dest should exist
Nevertheless, Play has its own utility to copy files. Here is the link.
import play.api.libs.Files
Files.copyFile(src, dest, true, true)
println(dest.getAbsolutePath()) // filepath of copied file
Also, since the files get copied to the working directory, you might not be able to see the new file in the folder structure of your favorite IDE.
Aside, you may get the path for public assets by using routes
val srcPath = routes.Assets.at("public/images/default/male.jpg").url
A general advise
When copying files on Java, use the FileUtils.copy(...) from the Apache commons project.
For your specific problem
You get "File not found" if the file cannot be found by the running process. This could be because your file is indeed not there or because the process is lacking rights to see the file.
Your wording is a little ambiguous, it seems you meant to give paths relative to the current working directory. That means that this code should be executed from a directory which contains the assets directory. If this is so, then you have made a mistake and given absolute paths to your file objects, not relative ones. All you have to do is remove the initial forward slash from those paths and it should work.
As it is, you are telling Scala/Java to look in the root directory for assets.

Servlet cannot find the file I'm trying to open

I read that the servlets map the current location based on the url. Clicking a button from my Home.jsp page directs me to my servlet, ExcelUploader. The URL when said button is clicked is
http://localhost:8080/ServletExample/ExcelUploader
I'm trying to open an excel file located in the same folder as my JSP. so that means I have to move one folder up relative to the url above. I have this in my servlet:
InputStream inp = new FileInputStream("../OpenMe.xls");
However I'm still getting a
java.io.FileNotFoundException: ..\OpenMe.xls (The system cannot find the file specified)
This is how my project is setup:
The FileInputStream operates on the local disk file system relative to the working directory and knows absolutely nothing about the fact that it's invoked from a Java EE web application. Any relative path you pass to it is relative to the folder which was been opened at the moment the command to start the server is executed. This is often the server's own installation folder, but in case of an IDE this can also be project's own root folder. This variable is not controllable from inside your Java code. You should not be relying on that.
You've stored the file as a resource of the public webcontent. So it's available as a webcontent resource by ServletContext#getResourceAsStream() which returns an InputStream. If you have absolutely a legitimate reason to invoke the servlet by its URL instead of just using the file's own URL http://localhost:8080/ServletExample/OpenMe.xls, then you should be getting at as follows:
InputStream input = getServletContext().getResourceAsStream("/OpenMe.xls");
// ...
If your intent is indeed to restrict the file's access to by the servlet only, you might want to consider to move the file into the /WEB-INF folder, so that the enduser can never open it directly by entering the file's own URL. You only need to change the resource path accordingly.
InputStream input = getServletContext().getResourceAsStream("/WEB-INF/OpenMe.xls");
// ...
You should not be using getRealPath() as suggested by the other answer. This won't work when the servletcontainer is configured to expand the WAR file into memory instead of into local disk file system, which is often the case in 3rd party hosts. It would return null then.
See also:
getResourceAsStream() vs FileInputStream
Paths for files that live in the webtree have to be "translated" using getRealPath before they are usable, like this:
File excelFile = new File(getServletContext().getRealPath("/OpenMe.xls"));
While you're at it, using the default package isn't a good idea, create a package for your files.