Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android Studio中将字符串复制并附加到json文件_Java_Android - Fatal编程技术网

Java 在Android Studio中将字符串复制并附加到json文件

Java 在Android Studio中将字符串复制并附加到json文件,java,android,Java,Android,我有如下json文件列表: 目前,T.json文件是空的。所有其他文件都已包含一些文本。我需要的是创建如下内容: try(FileWriter fw = new FileWriter("T.json", true); BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw)) { out.println("the text"); out.println("m

我有如下json文件列表:

目前,T.json文件是空的。所有其他文件都已包含一些文本。我需要的是创建如下内容:

try(FileWriter fw = new FileWriter("T.json", true);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter out = new PrintWriter(bw))
{
    out.println("the text");
    out.println("more text");
} catch (IOException e) {
    //exception handling left as an exercise for the reader
}
1.在T.json文件的开头添加

{
  "T": [
2.将文本从例如T_Average.json和T_Easy.json复制到T.json文件

String content = getJsonFromAssetFile("T_Difficult.json");
3.在T.json文件末尾添加以下内容:

] }

因此,在程序执行结束时,我需要在我的T.json中包含如下内容:

    {
      "T": [
             text from T_Average.json 
             text from T_Easy.json
      ]
    }
那么,如何将第一步和第三步中的文本添加到文件中? 如何将其他文件中的所有内容复制到T.json文件

我已经尝试过一些类似的解决方案:

try(FileWriter fw = new FileWriter("T.json", true);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter out = new PrintWriter(bw))
{
    out.println("the text");
    out.println("more text");
} catch (IOException e) {
    //exception handling left as an exercise for the reader
}
或者像这样:

try {
    String data = " This is new content";
    File file = new File(FILENAME);
    if (!file.exists()) {
        file.createNewFile();
    }
    fw = new FileWriter(file.getAbsoluteFile(), true);
    bw = new BufferedWriter(fw);
    bw.write(data);
    System.out.println("Done");
} catch (IOException e) {
    e.printStackTrace();
} 
但一直以来,在使用
fw=newfilewriter()
行之后,它直接跳到catch子句

所以再一次: 如何将第一步和第三步中的文本添加到文件中? 如何将其他文件中的所有内容复制到T.json文件? 谢谢:)

试试看

1.将以下方法添加到代码中

2.读取json文件

String content = getJsonFromAssetFile("T_Difficult.json");
3创建最终json(如上所述)

4.将最终json写入文件

writeFile(finalJson.toString().getBytes());
写文件

public static void writeFile(byte[] data, File file) throws IOException {

    BufferedOutputStream bos = null;

    try {
        FileOutputStream fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(data);
    }
    finally {
        if (bos != null) {
            try {
                bos.flush ();
                bos.close ();
            }
            catch (Exception e) {
            }
        }
    }
}
public static String getJsonFromAssetFile(Context context, String jsonFileName) {

    String json = null;
    try {

        InputStream is = context.getAssets().open(jsonFileName);

        int size = is.available ();

        byte[] buffer = new byte[size];

        is.read (buffer);

        is.close ();

        json = new String(buffer, "UTF-8");

    }
    catch(IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

}
getJsonFromAssetFile

public static void writeFile(byte[] data, File file) throws IOException {

    BufferedOutputStream bos = null;

    try {
        FileOutputStream fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(data);
    }
    finally {
        if (bos != null) {
            try {
                bos.flush ();
                bos.close ();
            }
            catch (Exception e) {
            }
        }
    }
}
public static String getJsonFromAssetFile(Context context, String jsonFileName) {

    String json = null;
    try {

        InputStream is = context.getAssets().open(jsonFileName);

        int size = is.available ();

        byte[] buffer = new byte[size];

        is.read (buffer);

        is.close ();

        json = new String(buffer, "UTF-8");

    }
    catch(IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

}

注意:使用
getJsonFromAssetFile
方法读取json资产文件,并在内部/外部存储器上写入文件,并提供
writeFile
方法的正确路径。在运行时,您无法向资产中的文件添加数据,因为资产中的所有内容都是只读的。资产文件夹是应用程序的只读构建时资源。一旦在APK中指定了这些文件,它们就不能在运行时更改。感谢您的回复!我添加了:
File File=new文件(“C:\\Users\\y50-70\\Desktop\\T2\\app\\src\\main\\assets\\T.json”)writeFile调用了writeFile方法(finalJson.toString().getBytes(),file),不幸的是,它在
FileOutputStream fos=newfileoutputstream(file)之后跳转到finally子句。你知道那可能是什么原因吗?顺便说一句,我不知道什么是
ServiceConstants.ENCODING
。那是什么?如何导入它?@Tomas没有提到路径“C:\\Users\\y50-70\\Desktop\\T2\\app\\src\\main\\assets”‌​\\只需使用String content=getJsonFromAssetFile(“T.json”);无需提及路径,您是在android手机上运行的,而不是在台式计算机上按照我提到的步骤运行的。为了更好地理解其他用户,您可以编写:String json=new String(buffer,“UTF8”);