Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6741231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:42:00+00:00 2026-05-26T11:42:00+00:00

First of all, sorry my poor english, please. I’m trying to write a function,

  • 0

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.

The problem is, that when I opening saved file, 1st symbol of string is disappeared. Don’t shure when axaclty: on write or read file.

Here is my code (It contains some unnesessary operations. It’s for testing):

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

        }

so, here is log in 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,'

…

and so on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:42:01+00:00Added an answer on May 26, 2026 at 11:42 am

    What is this?

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

    That naked fis.read() is going to read the first byte and toss it away. Then you’ll read the rest of the input stream to populate sBuffer. That’s the most likely cause of your missing character.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, sorry about that title. I'm not the best at writing those
First: sorry for my poor English writing. I remember that in VB6.0 days, we
First of all, I'm sorry about the feedback-nature of this question. I'm trying to
First of all, I'm sorry about my English and I hope, at least, you
Ok First have all i'm sorry for my poor english :) Now for my
First of all sorry for my pour english. I'm trying to make virtual world
Well, first of all sorry about this question it must be pretty straight forward
first of all, sorry if that question is dumb but I´m a total newbie
First of all sorry that I could not think of a more descriptive title.
First of all i'm sorry for my english... I've a problem getting phone numbers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.