I am creating an Android module in react-native
I never worked with Java or writing code in Java
How can I complete the code below?
What I want
1- look and verify if the directory exist if it exist then remove it.
2- recreate the directory.
3- create a json file and add its content.
Here is what I got so far
@ReactMethod
public string write(string content) {
var folder = "NovelManager";
File path = Paths.get(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), folder);
var fullPath = Paths.get(path, "NovelManager.backup.json");
makeDir(path);
File file = new File(path, "NovelManager.backup.json");
if (!file.exists())
file = file.createNewFile();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
out.write(content);
out.close();
return file.getAbsolutePath();
}
private void makeDir(string dirPath){
var dir = new File(dirPath);
if (!dir.exists())
dir.mkdir();
}
Update and solution
After to much hard work this did thing for me.
Here is the complete code for anyone who have similar problem.
// DownloadFileModule.java
package com.novelmanager;
import android.view.View;
import android.app.Activity;
import java.io.BufferedWriter;
import java.io.Console;
import java.io.File;
import java.io.FileWriter;
import android.os.Environment;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
public class DownloadFileModule extends ReactContextBaseJavaModule {
@Override
public String getName() {
return "DownloadFileModule";
}
@ReactMethod(isBlockingSynchronousMethod = true)
public String write(String content) {
if (content == null || content == "")
return "";
try {
String folder = "NovelManager";
String fileName = "NovelManager.backup.json";
String downloadFolderPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.getPath();
String dirPath = compine(downloadFolderPath, folder);
File dir = new File(dirPath);
if (!dir.exists())
dir.mkdir();
String path = compine(downloadFolderPath, folder, fileName);
File file = new File(path);
if (!file.exists())
file.createNewFile();
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
out.write(content);
out.close();
return file.getPath();
} catch (Exception e) {
return e.getMessage();
}
}
private String compine(String... more) {
String url = more[0];
for (int i = 1; i < more.length; i++) {
String str = more[i];
if (str.startsWith("/"))
str = str.substring(1);
if (str.endsWith("/"))
str = str.substring(0, str.length() - 1);
if (url.endsWith("/"))
url = url.substring(0, url.length() - 1);
url = url + "/" + str; // relative url
}
return url; // relative url
}
DownloadFileModule(ReactApplicationContext reactContext) {
super(reactContext);
}
}