【发布时间】:2013-02-04 02:36:08
【问题描述】:
我正在制作一个 xml 文件并将其保存在我的设备上,代码如下
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xx:xx:xx:xx:yy/LoginAndroid.asmx/login");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String responseBody = EntityUtils.toString(response.getEntity());
//Toast.makeText( getApplicationContext(),"responseBody: "+responseBody,Toast.LENGTH_SHORT).show();
//saving the file as a xml
FileOutputStream fOut = openFileOutput("loginData.xml",MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(responseBody);
osw.flush();
osw.close();
//reading the file as xml
FileInputStream fIn = openFileInput("loginData.xml");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[responseBody.length()];
isr.read(inputBuffer);
String readString = new String(inputBuffer);
文件正在保存,我也可以读取文件,一切正常,但看看这一行
char[] inputBuffer = new char[responseBody.length()];
它正在计算保存文件时保存的字符串长度。我将文件保存在一个活动中并从另一个活动中读取它,我的应用程序将在本地保存文件一次,因此我无法获取每次返回字符串的长度那么有没有办法动态分配char[] inputBuffer的大小?
【问题讨论】:
标签: android xml android-file input-buffer