2011-10-31 4 views
0

まず、貧しい私の英語をお願いします。 私はサーバーに鞭打ちビデオに関する情報を送信する関数を作成しようとしています。サーバーが到達不能になると、サーバーにファイルが書き込まれ、後で送信されます。だから、私は関数にファイル名を送り、名前を付けずにファイルを読み込み、名前の配列を作り、それらを一つずつ送ってきます。エラーが表示された場合は、ファイルに書き戻します。ファイルへの読み書き後に最初の記号が消える

保存したファイルを開くと、文字列の1番目のシンボルが消えてしまうという問題があります。 axacltyのときにshureしないでください:ファイルの書き込みまたは読み込み時。ここで

は私のコードは、(。それは、いくつかのunnesessaryの操作が含まれているそれは、テストのためです)です:

 void sendLog (String name) { 
      String FILENAME = "Log"; 
      String strToSave = ""; 
      String logString = null; 
//opening file, just to see what it cantains: 
      FileInputStream fis; 
      try { 
        fis = openFileInput(FILENAME); 
        fis.read(); 
        ByteArrayOutputStream content = new ByteArrayOutputStream(); 
        int readBytes = 0; 
        byte[] sBuffer = new byte[512]; 
        while ((readBytes = fis.read(sBuffer)) != -1) { 
         content.write(sBuffer, 0, readBytes); 
         } 
        logString = new String(content.toByteArray()); 
       fis.close(); 
       Log.d ("Log", "Loaded log: '"+logString+"'"); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      //saving empty file. 
      FileOutputStream fos; 
      try { 
       Log.d ("Log", "Saving log: '"+strToSave+"'"); 
       fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
       fos.write(strToSave.getBytes()); 
       fos.close(); 
      } catch (IOException e) { 
       Log.d ("File WRITE", "can't write Log file"); 
       e.printStackTrace(); 
      } 


       //opening it agane, allready empty. 
       try { 
        fis = openFileInput(FILENAME); 
        fis.read(); 
        ByteArrayOutputStream content = new ByteArrayOutputStream(); 
        int readBytes = 0; 
        byte[] sBuffer = new byte[512]; 
        while ((readBytes = fis.read(sBuffer)) != -1) { 
         content.write(sBuffer, 0, readBytes); 
         } 
        logString = new String(content.toByteArray()); 
        fis.close(); 
        Log.d ("Log", "Loaded log: '"+logString+"'"); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       int cnt; 
       //splitting loaded string to array 
       if (logString.length()>1) { 
        logString = logString+name+","; 
        Log.d ("Log", "spliting log. length: "+logString.length()); 
        log = logString.split(","); 
        long length = logString.length(); 
        long length2 = 0; 
        cnt = 0; 

//sorry for this code. Is there a better solution to know, how many strings in array? 
        for (int i = 0; length2<length; i++) { 
         length2 += playList[i].length()+1; 
         cnt++; 
        } 
       } 
       else { 
        log = new String[1]; 
        log[0] = name+","; 
        cnt = 1; 
       } 
      //sending them one by one 
       for (int i = 0; i<cnt; i++) { //playList[i].substring(2, playList[i].length()-1) 
        Log.d ("Log", "Sending log: '"+log[i].substring(0, log[i].length()-1) + "'"); 

         HttpClient client = new DefaultHttpClient(); 
         HttpGet request = new HttpGet("server link here" 
         +log[i].substring(0, log[i].length()-1)); //need to exclude "," 
         try { 
          HttpResponse response = client.execute(request); 
         } catch (ClientProtocolException e) { 
//adding unsended name to string if any errors 
          strToSave = strToSave+log[i]; 
          e.printStackTrace(); 
         } catch (IOException e) { 
          strToSave = strToSave+log[i]; 
          e.printStackTrace(); 
         } 
       } 
//here should be empty string, but for testing i'm adding some: 
      if (strToSave.length() == 0) strToSave = strToSave+log[0];//strToSave = " "; 
      //saving it to file 
      try { 
       Log.d ("Log", "Saving log: '"+strToSave+"'"); 
       fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
       fos.write(strToSave.getBytes()); 
       fos.close(); 
      } catch (IOException e) { 
       Log.d ("File WRITE", "can't write Log file"); 
       e.printStackTrace(); 
      } 

     } 

ので、ここではログがDDMSである:

10-31 09:23:17.446: DEBUG/Log(2571): Loaded log: 'UB_MENUS,' 
10-31 09:23:17.446: DEBUG/Log(2571): Saving log: '' 
10-31 09:23:17.446: DEBUG/Log(2571): Loaded log: '' 
10-31 09:23:17.446: DEBUG/Log(2571): Sending log: 'UPLOAD_SCREEN' 
10-31 09:23:17.746: DEBUG/Log(2571): Saving log: 'UPLOAD_SCREEN,' 
10-31 09:23:39.386: DEBUG/Log(2571): Loaded log: 'PLOAD_SCREEN,' 
10-31 09:23:39.386: DEBUG/Log(2571): Saving log: '' 
10-31 09:23:39.386: DEBUG/Log(2571): Loaded log: '' 
10-31 09:23:39.386: DEBUG/Log(2571): Sending log: 'APP_BUTTON' 
10-31 09:23:39.646: DEBUG/Log(2571): Saving log: 'APP_BUTTON,' 

...

とそうです。

答えて

0

これはとは何ですか?裸fis.read()は、最初のバイトを読んで、それを離れて投げるしようとしていること

try { 
    fis = openFileInput(FILENAME); 

    //******************************************** 
    fis.read(); // <<-- Danger, Will Robinson !! 
    //******************************************** 

    ByteArrayOutputStream content = new ByteArrayOutputStream(); 
    int readBytes = 0; 
    byte[] sBuffer = new byte[512]; 
    while ((readBytes = fis.read(sBuffer)) != -1) { 
     content.write(sBuffer, 0, readBytes); 
    } 
    logString = new String(content.toByteArray()); 
    fis.close(); 
    Log.d ("Log", "Loaded log: '"+logString+"'"); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

。次に入力ストリームの残りの部分を読み込み、sBufferを入力します。それが不足しているキャラクターの最も可能性の高い原因です。

+0

私のコードで別の愚かな間違い...あなたに多くのおかげで! – SentineL

関連する問題