XML parsing in scala - scala

I am parsing XML file in scala using
val data = XML.loadFile("changes.xml")
I have the changes.xml under the src folder. When I run this the code throws a FileNotFoundException. Any idea how to solve it or any insight on how scala looks for the
files in the classpath would be helpful.

See what the current directory is using
new java.io.File(".").getCanonicalPath()
Since you're opening the file with a relative path, it looks for the file in the process's working directory.

Since you're placing your file in the source tree, I assume you want to ship it with the application jar.
Then tell your IDE to copy the file to the ouput folder (maybe this is already happening) so it lies in the classpath. If you place your file in the same folder as the class from which you want to load you can simply do the following. Use in Java/Scala
Class.getResourceAsStream("changes.xml")
Link to API doc.
Edit
You can use XML.load("changes.xml") and I think it will load the file in the same way as Class.getRessourceAsStream. So try putting your xml file into the same folder as the class and make sure the build process copies it into your binary output folder.

Related

How to write a file to "target" directory of Scala SBT project?

I want to create folder and write files in the \target\ sub directory of my project, but the only thing I have been able to accomplish is providing a string of the file name and the file will be created in \.idea\modules\.
There is not enough info on this at all. Everybody only talks about accessing the \resources\ directory.
Can anybody help me out?
Thanks.
UPDATE: I managed to hack a way to accomplish making a folder under the "target" directory. It may look ugly, I wonder if this is the only way.
import java.io.File
import java.nio.file.{Files, Path, Paths}
val targetDir: Path = Paths.get(getClass.getClassLoader.getResource("").toURI).getParent.getParent
val newFolderString = targetDir.toString() + "/myNewFolder"
val directory: File = new File(newFolderString)
directory.mkdir()
If you use the default directory layout of an SBT project (http://www.scala-sbt.org/0.13/docs/Directories.html) then when you compile the project, the files you have under src/main/resources will be copied to target/scala-2.xx.
If you need to keep your resource files in a non-standard location see Nyavro's comment.
To write an output file from within your application, at runtime, you can look at Writing data generated in scala to a text file

Where to put files I want to load with my program in SBT?

In SBT where do I put files I want to load into my program? I know I can use the /test/ directory, but the problem is I have no idea in what context sbt is executing my scala program. From where does it execute so I know how to write a directory string to grab it?
Place them under src/main/resources or src/test/resources as described in Directory structure

Reference txt file from code in Intellij

I have a very simple example in Scala for reading a text file. The example is made in IntelliJ. However when I try to use io.Source.fromFile("..\..\resources\example.txt"), I get a FileNotFoundException. The code works when I type in the absolute path.
Can anyone tell me how to use relative paths for specifying a file in Intellij project?
the directory structure:
Paths are relative to the current/working directory, i.e. the directory from which the java command is executed.
When an application is executed from IntelliJ, this working directory is configured in the Run configuration used to launch the application.
So, go to Run - Edit configurations..., then find your run configuration, edit it, and see what the field "Working directory" contains. By default, the working directory is the root directory of the project.
Note that, unless the String escaping rules are different in Scala from Java, the path must be separated by slashes:
"../../resources/example.txt"
, or the backslashes must be escaped:
"..\\..\\resources\\example.txt"
Also not that if your goal is to bundle the text file inside the jar of your application, you shouldn't use file IO to read it, but use the ClassLoader.getResourceAsStream() method.

Where to put a file in the netbeans?

In which location I should put any file in Netbeans project directory structure?
Also in the code how should I give the path(absolute or relative) to that file?
Thanks
If it is about Java,
I would suggest you to put it in default package , It would be directly available in classes folder after building, so you can directly access it from classpath.
Here is sample

how to put .properties file in classpath of an Eclipse plug-in

I have an application which is making use of a .jar file and a .properties file which must reside in the same directory as where the .jar file lies. On a normal java application, this works fine, however I'm building an Eclipse plug-in. I've tried attaching the .properties everywhere, in the classpath, build path, putting them in the same folder and calling the jar file from there (this gives an obsure error) and I've even tried putting the .properties file inside the .jar file even.... but no luck. Any ideas how this could be done please?
Thanks and regards,
Krt_Malta
Perhaps the FileLocator class will help you. The FileLocator provides a find method where you can specify the Bundle and a resource to look for.
Ingo