English 中文(简体)
第1号符号在文件书写和阅读后消失
原标题:1st symbol disappearing after writing-and-reading string to the file
  • 时间:2011-10-31 04:05:49
  •  标签:
  • android
  • file

First of all, sorry my poor english, please. I m trying to write a function, that sends an information about whatching video to the server. When server is unreachable, it writes it to the file and sends it later. So, i m sending file name to my function, loading file with unsended names, makeing an array of names and sending all of them one by one. If any error appear, writing them back to file.

问题在于,当我开始保存档案时,发现的首个象征就消失了。 axaclty:在书面或读物上。

我的法典 (其中包括一些非驻地行动。) 用于测试:

     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, 

......

等等。

最佳回答

是什么?

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

这部光彩的<代码>fis.read(>)将读一读并删除。 然后,请将输入流的其余部分改为:sBuffer。 这是你失踪的最可能原因。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签