In a completely fresh project, I want to create a single file myFile.json inside the src/main/resources/ folder at run time.
For reading a file, I need to do some config in the build.gradle.kts file, but I can't find anything on what to do for creating a file.
Assuming the directory src/main/resources/ exists:
val f = File("src/main/resources/myFile.json")
withContext(Dispatchers.IO) {
f.createNewFile() // This is the answer to the question
f.printWriter().use { out ->
out.println("{}")
}
}
#Endzeit has asked what have you tried so far. Please share the code.
Like #cyberbrain says - are you sure you want to write to resources folder?
Here is code that writes back to where the source resources folder is:
fun main(args: Array<String>) {
// Let's assume you want your project to be portable, so you don't
// want to use absolute file paths.
// Find out where your IDE will launch the project from. Normally this is
// the root folder of the whole project. Find out with this: the `canonicalPath` will help:
val workingFolder = File(".")
println("workingFolder=${workingFolder.canonicalPath}")
// Define the folder you want to write in to
// this will vary especially if you have a nested project structure
// IntelliJ under the Edit > Copy Path menu option will help you find the resources
// relative location
val parentFolder = File("src/main/resources")
println("parentFolder=${parentFolder.canonicalPath}")
require(parentFolder.exists())
val outFile = File(parentFolder, "test.txt")
outFile.printWriter(StandardCharsets.UTF_8).use {
it.println("Hello world")
}
println("Wrote to ${outFile.canonicalPath}")
}
Related
I have a similar code like below. But since the logics are same for my other projects I wanted to keep this in a single file and "apply" in every project. So I tried using the "apply" command, but it won't work. Nothing complaining about the build but my logic is not executing in the build process.
Question - Why?
lateinit var projectConfigurer: (ProjectDescriptor, ProjectDescriptor) -> Unit
projectConfigurer = { _, project ->
project.name = project.name.replace("/", "-")
// Some logic to validate project naming convention
project.children.forEach { child ->
projectConfigurer(project, child)
}
}
rootProject.children.forEach { project ->
projectConfigurer(rootProject, project)
}
Here is what I wanted to do.
apply(from = "common/common.settings.gradle.kts")
And all the code I needed to move to "common.settings.gradle.kts", which was in a common folder reletive to "settings.gradle.kts"
Having problems with my code, So I am currently trying to create a new directory and also store a text file within that folder I have created, I looked at a couple of examples but it seems like they only focus on a specific thing like how to create a file or a folder but never how to utilise both. How can I achieve this? I keep hitting exception errors when I try doing different methods, thanks!
val newFile : Int = 1
val fileString = "nameData"
//so we are creating variable to store the directory information
val folderDir = File("G:\\Random Projects\\JVM\\database\\Collection 1")
//we use that variable to create a File class which will create a folder called nameData
//this will also be stored in another variable called f
val f = File(folderDir, "nameData")
//this will create the actual folder based on the variable information
f.mkdir()
//creating file
try {
val fo = FileWriter(fileString, true)
fo.write(a)
fo.close()
} catch (ex:Exception) {
println("Something Went Wrong When Creating File!!")
}
The problem is that you probably don't have the whole folder structure created, that's why you usually use the mkdirs (note the s at the end) function. You can then use the writeBytes function to write the content:
val fileString = "nameData"
val folderDir = File("myfolder")
val f = File(folderDir, "nameData")
f.parentFile.mkdirs()
f.writeBytes(fileString.toByteArray())
I'm using cinterops to link a dynamic library in a mingw binary executable. Everything is working fine, except that the .dll name that the executable asks is different from the one declared at the .def file.
I don't know where this different name is coming from!
This is from my gradle.build.kts:
mingwX86("mingw"){
compilations["main"].cinterops{
val scape2 by creating {
val cafmSrc = "C:/Software/SCAP E2/CAFM_src"
val scapSrc = "C:/Software/SCAP E2/TO/ETME2"
val modifiedSrc = "C:/Software/SCAP E2/Modified CAFM files"
includeDirs.headerFilterOnly(cafmSrc, scapSrc, modifiedSrc)
extraOpts.add("-verbose")
}
}
binaries {
executable()
}
}
This is my scape2.def file:
headers = scape2.h
headerFilter = scape2.h \ GEO_API_SCAPTO.h
linkerOpts.mingw = -LC:/Users/lscarmin/git/calculation-module4 -lscape2
I was expecting that the dll name to be scape2.dll.
But when I run the executable, it asks for ETME2.dll!
If I rename scape2.dll to ETME2.dll, the code works. (editado)
Well, it seems that the file name used is the one defined inside the dll. I have renamed the dll file, but the name used will be the original one.
I didn't know that this info was kept inside the dll
I am currently accessing the assets directory for a project as follows (where it has type Project):
SourceDirectorySet resources = it.sourceSets.main.resources
File file = resources.srcDirs.find { it.name == "assets" }
if (file != null) { ... }
Is this guaranteed to be correct? Or is there a better way (probably through android)?
In fact the code in the question doesn't work correctly. Instead I needed
it.android.sourceSets.main.assets
I am looking to access all files in a local directory in a JBoss application. I can put the directory anywhere in my war including WEB-INF if necessary. I then want to access each file in the directory sequentially. In a normal application if the directory was in the run location I could do something like:
File f = new File("myDir");
if(f.isDirectory && f.list().length != 0)
{
for(String fileName : f.list())
{
//do Read-Only stuff with fileName
}
}
I'm looking for a best-practices solution, so if I'm going about this wrong then please point me to the right way to access an unknown set of resources.
First thing to note: you're only going to get this to work if you have an exploded WAR, or possibly if the servlet container explodes the WAR for you.
With that caveat in mind, you could use ServletContext.getRealPath() as your starting point. You'd need to know the name of at least one file in the webapp's root directory, and go from there:
String knownFilePath = servletContext.getRealPath("knownFile");
File webAppRootDir = new File(knownFilePath).getParentFile();
// and then as per the question
File f = webAppRootDir ;
if(f.isDirectory && f.list().length != 0)
{
for(String fileName : f.list())
{
//do Read-Only stuff with fileName
}
}
Getting hold of ServletContext is left as an exercise for the reader.