Getting cannot find file exception in Play Framework - file

I have a file called product.csv in my application root. I want to read from the file. But I am getting file not found exception. Here is the code I have written.
val lines = scala.io.Source.fromFile("/product.csv").mkString
println(lines)
What am I doing wrong?
I am using Windows OS.

You need to use a relative filepath "product.csv" rather than "/product.csv" which will look at the root of your drive.

Related

Loading files into MAGMA

I'm trying to load files into MAGMA and am running into some trouble. Ostensibly, the command load "filename";should be sufficient. I've attempted, but keep getting the same result:
>> load "filename";
^
User error: Could not open file "filename" (No such file or directory)
The file is saved in my documents folder, so I'm not sure what the issue is. Do I have to specify the path? Save the file in a particular place?
I've tried reformatting, using both txt and rtf files, so I don't think that's the issue.
For loading file in MAGMA you can place your file in installed place folder. For example: C:\Program Files (x86)\Magma
Also if your file have an special format you should mention it.
Suppose You want loading a txt file with name a. with load"a"; you face with error. You must type load"a.txt";.
Try using GetCurrentDirectory() command to find your current directory location. And then you can use SetPath() to change where MAGMA has to be to search for your file. This will fix it.

GM:Studio - *.ini files doesn't load/save

So i'm trying to make a configuration for my app, although it doesn't seem to load or save anything from/in it.
Here's the code upon creation:
ini_open(working_directory + "\properties.ini");
global.width = ini_read_real('screen','width',640);
global.height = ini_read_real('screen','height',480);
ini_close();
Here's the ini file:
[screen]
width = 1280
height = 1024
when i return global.width and global.height values they're still 640;480 but not as stated in the ini file, which means it doesn't even load values from the file. I was wondering, maybe I need to compile the executable and then run it as administrator, but I doubt that there could be a problem with permissions. I also added properties.ini file to "Included Files" folder so it would come with compiled exe.
This seems weird, but now it works. Actually the *.ini file isn't saved the same place where exe is. I just found out that it saves everything in %localappdata%// . Also it looks to be working better without dir shortcut "working_directory". So thanks, anyway

How can I prevent AccessDeniedException during Files.walkFileTree?

I'm trying to use Java 1.7 nio. When I call
Files.walkFileTree(source[i], tc);
where source[i] is any folder on my Windows file system and tc is a SimpleFileVisitor, I get an java.nio.file.AccessDeniedException. I've checked the folder and the folder contents and ensured that read, write, and execute are allowed for all users.
I can access the file fine using File. Why does walkFileTree have a problem accessing the file?
I found the solution to this issue. I overrode the visitFile method and it was incorrectly referencing a file path.

What is a relative path like in C?

I am now using Visual C++ 2010 to open a txt file.
fp = fopen("E:\\CProg\\Huffman\\Debug\\Huffman.txt","r"); //Right
//Wrong
//fp = fopen(".\\Huffman.txt","r");
//fp = fopen("\\Huffman.txt","r");
//fp = fopen("Huffman.txt","r");
In VB.NET, I used to write like this: Application.Startpath & "\". Then how do I make it in C?
Relative to what? On "all relevant platforms", if you use a filename that's not absolute it will be resolved relative to the current directory.
The following is for Windows.
If you are writing a console application, the application will start with the current directory set to whatever the command prompt shows. Without further research I can't tell what the initial current directory for a GUI application will be.
If you want your filename to be relative to the Installation directory you'll have to use something like the Win32 function GetModuleFileNameW() and work your way from there (the function gives you the pathname to the exe file; remove the last component to get the directory, and append whatever path you want to append)-
There is no direct way to say "I want this filename to be used relative to the installation directory".

Access denied to resource file from java servlet

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.

Resources