我有一个JSON对象,我正在尝试将其写入json文件。我认为OutputStreamWriter对象的write()方法负责写入json文件,但是如果我不添加最后一行“ urlConnection.getInputStream();”更改不会生效。请解释此行的作用以及为什么需要它,因为据我所知,getInputStream()用于从打开的连接读取输入流,而不写入打开的连接。
URL database_url = new URL("https://example.firebaseio.com/posts.json");
URLConnection urlConnection = database_url.openConnection();
urlConnection.setRequestProperty("X-Requested-With", "Curl");
urlConnection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
out.write(obj.toJSONString());
out.close();
urlConnection.getInputStream();