Create new .json file in specific directory

Good Morning every one, 

I would like to know, if there is a way to create a JSON file and insert it in a specific directory, like using template files to create java classes? 

and another point is the fact that, how can I create it in a specific directory like (res/raw/test.json)

thank you in advance

6 comments

Good morning. You can create file templates for any kind of file you want using File -> Settings -> Editor -> File and Code Templates. There already exist several samples of JSON file templates:

As about creating them in a specific directory, could you please clarify a bit what do you need exactly - to show corresponding file templates only under particular directories, or to create the full hierarchy of nested directories containing this file, or to automatically generate the file in some certain scenario?

Thanks in advance for any further info.

0

Thanks for your hints. 

In particular, I started to develop an android studio plugin to automatically generate some implementation code and add it to the developer application. the work that I need to do in one part of this plugin is to create a JSON file like the following:

{
"User_name":"test",
"https_required": true,
....
}

so, I would like to know, How can I create this programmatically, and then I need to put this file in a resource directory of the application as the one you can see in the attached image (res\raw)

Indeed, I need to create the JSON file programmatically and then create a sub-directory inside res directory with the name of i.e test and insert the JSON file there. 

thanks in advance for your help 

0

If you want to add a file template from your plugin, you can register a template in your plugin.xml:

<internalFileTemplate name="my_json"/>

You place your template under "fileTemplates\internal" in your resources directory, with name like "my_json.json.ft". (And add one more file "my_json.json.html" if you want to provide a description.)

And then you may use com.intellij.ide.actions.CreateFileFromTemplateAction#createFileFromTemplate() in your action invocation. Use FileTemplateManager.getInstance(project).getInternalTemplate to get the template object.

 

Now it depends only on how you want to invoke your template.

Do you want to have an action in some context menu, or to expand it when the project is being created?

0

Actually, I have an action that in some context menu. so, I would like this functionality as well, in the aforementioned part. so, How can I inject it into the specified directory by using the aforementioned method? 

Regards. 

0

>Actually, I have an action that in some context menu.

In this case, just invoke CreateFileFromTemplateAction#createFileFromTemplate inside you action, it allows to specify the directory where your file is to be created.

You may also override isAvailable for your action, if you want it to be shown only in specific contexts.

0

thank you very much for your help. I will try that. 

 

0

Please sign in to leave a comment.