新建一个工具类FileUtils,用来读取文件内容
public class FileUtils {
public String ReadFile(String Path) {
BufferedReader reader = null;
String laststr = "";
try {
FileInputStream fileInputStream = new FileInputStream(Path);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "gbk");
reader = new BufferedReader(inputStreamReader);
String tempString = null;
while ((tempString = reader.readLine()) != null) {
laststr += tempString;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return laststr;
}
}
JSon文件显示
public class JsonUtils {
public static void main(String[] args) {
String JsonContext = new FileUtils().ReadFile("F:\\1.txt");
//将读取的数据转换为JSONObject
System.out.println("String:"+JsonContext);
JSONArray jsonArray = JSONArray.fromObject(JsonContext);
System.out.println("jsonArray:"+jsonArray);
int size = jsonArray.size();
//System.out.println("Size: " + size);
for (int i = 0; i < size; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//System.out.println(jsonObject);
//System.out.println("[" + i + "]id=" + jsonObject.get("id"));
//System.out.println("[" + i + "]ip=" + jsonObject.get("ip"));
//System.out.println("[" + i + "]zname=" + jsonObject.get("zname"));
//List list = (List) jsonObject.get("pais");
//System.out.println("[" + i + "]pais=" + list);
}
}