2013-03-27 73 views
-1

因此,我的應用程序從肥皂服務器接收大型xml。我希望將其保存在一個文件中,以備後用。我設法做到這一點,並閱讀文件。但結果(閱讀後)是一個亂碼xml!來自xml後半部分的大部分文本(412個字符)被複制並粘貼到我的xml結尾,我無法弄清楚爲什麼會發生這種情況。 我已經嘗試了2種方法來編寫文件和2種方法來讀取文件,沒有骰子! (將在下面發佈代碼)注意:xml大5000-20000個字符,所以我使用方法來防止eclipse返回內存錯誤。保存/從文件中讀取大型xml字符串

底線:

- 輸入XML文件是正確的

- 輸出XML文件不正確

-tried 2保存方法

-tried 2讀取方法

- WTF?

保存代碼1:

    InputStream is = new ByteArrayInputStream(string.getBytes()); 

        FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE); 

         byte[] buffer = new byte[1024]; 
         int length; 
         while ((length = is.read(buffer))>0){ 
          fos.write(buffer, 0, length); 
         } 

         fos.flush(); 
         fos.close(); 
         is.close(); 

保存代碼2:

   InputStream is = new ByteArrayInputStream(string.getBytes()); 

        BufferedReader reader = new BufferedReader(new InputStreamReader(
          is, "iso-8859-1"), 8); 
        FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE); 
        StringBuilder sb = new StringBuilder(); 
        String line = null; 

        while ((line = reader.readLine()) != null) { 
         Log.e("stuff is good", "line: "+line); 
         sb.append(line); 

         if (sb.toString().length() > 10000) { 
          fos.write(sb.toString().getBytes()); 
          fos.flush(); 
          sb = new StringBuilder(); 

         } 

        } 

        fos.write(sb.toString().getBytes()); 
        fos.flush(); 
        is.close(); 
        fos.close(); 

讀取的代碼1:

  FileInputStream fis = openFileInput("caca"); 
        int c; 
        StringBuilder fileContent = new StringBuilder(); 

        while((c=fis.read())!=-1) 
          { 
         fileContent.append((char)c); 

          } 
        fis.close(); 

        Log.e("TEST TEST", "XML length = " 
          +String.valueOf(fileContent.length())); 
        Log.e("TEST TEST", "XML = " 
          +fileContent); 

讀碼2:

 FileInputStream fis; 
        fis = openFileInput("caca"); 
        StringBuffer fileContent = new StringBuffer(""); 
        byte[] buffer = new byte[1024]; 
        int i =1; 
        while (fis.read(buffer) != -1) { 
         fileContent.append(new String(buffer)); 
         Log.v("TEST"+ String.valueOf(i), new String(buffer)); 
         i++; 
        } 
        Log.e("TEST TEST", "XML length = " 
          +String.valueOf(fileContent.length())); 
        Log.e("TEST TEST", "XML = " 
          +fileContent); 

保存到文件中的代碼:

     File myFile = new File("/sdcard/mysdfile.txt"); 
         myFile.createNewFile(); 
         FileOutputStream fOut = new FileOutputStream(myFile); 
         OutputStreamWriter myOutWriter = 
               new OutputStreamWriter(fOut); 
         myOutWriter.append(fileContent); 
         myOutWriter.close(); 
         fOut.close(); 
         Toast.makeText(getBaseContext(), 
           "Done writing SD 'mysdfile.txt'", 
           Toast.LENGTH_SHORT).show(); 

對不起,長職位,但3天后,我在我束手無策。任何輸入都會很好,謝謝!

+0

對不起,323go,一個從我逃走了。我刪除了其他6個或7不當Log.e的我之前發佈的代碼。 – Sebek 2013-03-28 09:31:13

回答

0

ok ....我修好了,我不知道它爲什麼起作用。

保存代碼:

public static void Save(String filename, String string, 
      Context ctx) { 
      Log.e("stuff is good", "xml length b4 save= "+String.valueOf(string.length())); 

       try { 
        FileOutputStream fOut = ctx.openFileOutput(filename, Context.MODE_PRIVATE); 

        OutputStreamWriter myOutWriter = 
              new OutputStreamWriter(fOut); 
        myOutWriter.append(Login.messagesXmlDump); 
        myOutWriter.close(); 
        fOut.close(); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

      } 

讀取的代碼:

    Save("LOL", messagesXmlDump, getApplicationContext()); 

       try { 

        FileInputStream fis = openFileInput("LOL"); 
        int c; 
        StringBuilder fileContent = new StringBuilder(); 

        while((c=fis.read())!=-1) 
          { 
         fileContent.append((char)c); 

          } 
        fis.close(); 

託管到讀/寫一個70K字符長XML。也許這種挽救它的遞歸方法並不是最好的主意。認爲我過分複雜了一件簡單的事情。

對不起,浪費你的時間:(

0

我更喜歡使用Apache Commons IO此:

DefaultHttpClient client = new DefaultHttpClient(); 
HttpGet httpGet = new HttpGet(url + id); 

try { 
    HttpResponse response = client.execute(httpGet); 
    InputStream content = response.getEntity().getContent();   
    StringWriter writer = new StringWriter(); 
    IOUtils.copy(content, writer, "utf-8"); 
    return writer.toString(); 

} catch (ClientProtocolException e) { 
    Log.e(tag, "client problem:" + e.getMessage()); 
    throw new RuntimeException("client problem",e); 
} catch (IOException e) { 
    Log.e(tag, "IO problem:" + e.getMessage()); 
    throw new RuntimeException("IO problem",e); 
} 

然後就寫出來的字符串如常。

+0

會盡量讓你知道,thx很多爲你的答案。 – Sebek 2013-03-28 09:31:40

+0

好吧,我很困惑。這應該怎麼幫助我?我已經有我想寫的字符串,問題發生時,我寫字符串的文件,然後讀取它再次。 – Sebek 2013-03-31 16:55:13