Creating files again

Answered

I'm trying to create a file and have a hard time understanding what exactly I'm doing wrong before I receive null when trying to extract a parent. It's very likely that I'm doing some dumb stuff, so I will clarify the task: I need to create a file in my project root folder and have it visible right away. Maybe I need something completely different. You can see the code below. Thank you!

JSONObject storyJson = constructJsonObject();
ApplicationManager.getApplication().runWriteAction((Runnable) () -> {
File file = new File(".story.json");
try (FileWriter writer = new FileWriter(file)) {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
final JsonNode tree = objectMapper.readTree(storyJson.toJSONString());
writer.write(objectMapper.writeValueAsString(tree));
writer.flush();
final VirtualFile newParent = VfsUtil.createDirectories(file.getParent());
if (newParent != null) {
final VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
VfsUtilCore.copyFile(this, virtualFile, newParent);
}
} catch (IOException e) {
e.printStackTrace();
}
});
3 comments
Comment actions Permalink

please show the full source code, so it's clear _where_ this is invoked

0
Comment actions Permalink

Hello, Yann! This is a dialog:

public class StoryCreationDialog extends DialogWrapper

// the data is collected from the input fields

@NotNull
private JSONObject constructJsonObject() {
final String title = titleField.getText();
final int readingTime = Integer.parseInt(readingTimeField.getText());
final String storyDescription = descriptionField.getText();
final String language = languageField.getText();
JSONObject storyJson = new JSONObject();
storyJson.put("title", title);
storyJson.put("readingTime", readingTime);
storyJson.put("description", storyDescription);
storyJson.put("program_lang", language);
return storyJson;
}

// and then tries to save it upon submission

@Override
protected void doOKAction() {
JSONObject storyJson = constructJsonObject();
ApplicationManager.getApplication().runWriteAction((Runnable) () -> {
File file = new File(".story.json");
try (FileWriter writer = new FileWriter(file)) {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
final JsonNode tree = objectMapper.readTree(storyJson.toJSONString());
writer.write(objectMapper.writeValueAsString(tree));
writer.flush();
final VirtualFile newParent = VfsUtil.createDirectories(file.getParent());
if (newParent != null) {
final VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
VfsUtilCore.copyFile(this, virtualFile, newParent);
}
} catch (IOException e) {
e.printStackTrace();
}
});
dispose();
}
0
Comment actions Permalink

What is

File file = new File(".story.json")

exactly? You're not specifying the parent directory where the file is created, but try to access file.getParent() later
0

Please sign in to leave a comment.