A cópia do código é a seguinte:
importar java.io.*;
/**
* Criado por Tang em 14-3-1.
*/
classe pública jsonutils {
// Leia o arquivo json do local fornecido
public static string readjson (caminho da string) {
// Obtenha o arquivo do local fornecido
Arquivo de arquivo = novo arquivo (caminho);
Leitor buffarreder leitor = null;
// Valor de retorno, use StringBuffer
StringBuffer Data = new StringBuffer ();
//
tentar {
leitor = new buffarrederader (new fileReader (arquivo));
// Toda vez que o arquivo é lido em cache
String temp = null;
while ((temp = leitor.readline ())! = null) {
data.append (temp);
}
} catch (filenotfoundException e) {
E.PrintStackTrace ();
} catch (ioexception e) {
E.PrintStackTrace ();
} finalmente {
// Fechar o fluxo de arquivos
if (leitor! = null) {
tentar {
leitor.close ();
} catch (ioexception e) {
E.PrintStackTrace ();
}
}
}
retornar data.toString ();
}
// Dá um arquivo JSON e JSON, armazene -o para o disco rígido
public static void writejson (caminho da string, objeto json, string filename) {
Escritor bufferwriter = null;
Arquivo de arquivo = novo arquivo (caminho + nome do arquivo + ".json");
// Se o arquivo não existir, crie um novo
if (! file.exists ()) {
tentar {
file.createNewFile ();
} catch (ioexception e) {
E.PrintStackTrace ();
}
}
//Escrever
tentar {
writer = new BufferWriter (new FileWriter (File));
writer.write (json.toString ());
} catch (ioexception e) {
E.PrintStackTrace ();
} finalmente {
tentar {
if (writer! = null) {
writer.close ();
}
} catch (ioexception e) {
E.PrintStackTrace ();
}
}
// System.out.println ("A redação de arquivos foi com sucesso!");
}
}