2

I am making a xml file and saving it on my device code follows

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);

FIle is saving I can also read the file every thing is ok but look at this line

char[] inputBuffer = new char[responseBody.length()];

it is calculating string length which is saved at the time of Saving the file.I am saving the file in one Acivity and reading it from another activity and my application will save the file locally once so I could not be able to get the length of that return string every time So is there any way to allocate the size of char[] inputBuffer dynamically?

A J
  • 4,542
  • 5
  • 50
  • 80

1 Answers1

0

you can use the below code in your another activity to read the file. Have a look at BufferedReader class.

InputStream instream = new FileInputStream("loginData.xml");

// if file the available for reading
if (instream != null) {
  // prepare the file for reading

  InputStreamReader inputreader = new InputStreamReader(instream);
  BufferedReader buffreader = new BufferedReader(inputreader);

  String line;

  // read every line of the file into the line-variable, on line at the time
  while (buffreader.hasNext()) {
     line = buffreader.readLine();
    // do something with the line 

  }

}

Edit:

The above code is working fine for reading a file, but if you just want to allocate the size of char[] inputBuffer dynamicall then you can use the below code.

InputStream is = mContext.openFileInput("loginData.xml");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while ((int bytesRead = is.read(b)) != -1) {
   bos.write(b, 0, bytesRead);
}
byte[] inputBuffer = bos.toByteArray();

Now , make use inputBuffer as you want.

Moin Ahmed
  • 2,898
  • 21
  • 33
  • I need to know how many char not line then **And it giving me error `The method hasNext() is undefined for the type BufferedReader`** – A J Feb 04 '13 at 06:08
  • answer updated, please have a look. Hope this is what you are asking. – Moin Ahmed Feb 04 '13 at 07:36
  • againg error `Cannot make a static reference to the non-static method openFileInput(String) from the type Context` on line `InputStream is = Context.openFileInput("loginData.xml");` – A J Feb 04 '13 at 07:58
  • you have to use activity's context instead of the "Context" variable if you are not in activity else you can use this method directly. – Moin Ahmed Feb 04 '13 at 08:09
  • If I change to Context it giving me error `protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.auto_fill_customer_master);` – A J Feb 04 '13 at 08:25
  • This is not the exact error message, hard to understand. But if you are jst facing problem in using 'openFileInput' method, have a look at its documentation about using it. I think you had already used that method, i can see it in your question. – Moin Ahmed Feb 04 '13 at 08:31
  • If your question was only jst to allocate dynamic size to input array then it should be solved by my answer. If you have any further query which is not related to the actual question, you can start a new thread. If my answer helped you then you can accept it by clicking on the tick-mark which is left to the answer. Thanx. – Moin Ahmed Feb 04 '13 at 08:35
  • can u tell me how can I know length the file `FileInputStream fIn = openFileInput("loginData.xml");` – A J Feb 04 '13 at 08:37