【问题标题】:file saving on android在安卓上保存文件
【发布时间】: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


    【解决方案1】:

    您可以在另一个活动中使用以下代码来读取文件。看看BufferedReader 类。

    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 
    
      }
    
    }
    

    编辑

    上面的代码可以正常读取文件,但是如果你只想分配char[] inputBufferdynamicall的大小,那么你可以使用下面的代码。

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

    现在,根据需要使用 inputBuffer。

    【讨论】:

    • 我需要知道有多少个字符没有行 它给了我错误The method hasNext() is undefined for the type BufferedReader
    • 答案已更新,请查看。希望这就是您要问的。
    • 再次出现错误Cannot make a static reference to the non-static method openFileInput(String) from the type Context在线InputStream is = Context.openFileInput("loginData.xml");
    • 如果您不在活动中,则必须使用活动的上下文而不是“上下文”变量,否则您可以直接使用此方法。
    • 如果我更改为上下文,它会给我错误protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.auto_fill_customer_master);
    猜你喜欢
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2011-10-24
    • 2017-07-19
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    相关资源
    最近更新 更多