I suspect you've been having some trouble reading the documentation in
English. I've had the experience of reading API documentation in a
foreign language, so I know it can be difficult. You probably only got
a couple points wrong, but then the confusions piled on top of each
other, and it will be hard to sort out now.

(My apologies if my assessment is incorrect -- I'm just trying to
guess at the kind of help that will help you quickest).

First, let me talk about the problems I see in the code. If you can't
understand, don't worry! Just go to where I suggest what to do now,
and come back to this later...

I think you are expecting the arguments to in.readFully to be the
positions in the file. They are not. They are positions in the array.
The way you are using it, start should always be 0, and end should
always be 102400, unless you are reading the last chunk of the file,
in which case it should be the remaining size.

Another problem: You should never use the String(byte[]) constructor.
You NEVER want to let the system pick a character encoding for you.
And I don't recommend using any encoding other than UTF-8 if you have
a choice. So add "UTF-8" as a second argument there.

You're copying your data an extra time to create the argument string
to sendData. Is there a reason to not rewrite sendData to take a
string for the first part, and a byte[] for the second?

I'm pretty sure your use of in.skip(start) is not what you want,
either! You just skip further and further ahead in the file, first 0
bytes, and (try to) send the next block, then skip the following
block, skip even more next time, then process one block, skip even
more...  I think maybe you think 'skip' positions the input stream at
a particular position?

I'm quite puzzled by the fact that you're incrementing the end by only
102398, but are reading 102400 byte chunks (or trying to). I think
something is wrong there, but I'm not sure what you're intending..

I don't see any reason for using DataInputStream -- and maybe not
in.skip(), except possibly with an argument of 2?

Here's what I think you should do: start over. Keep it as simple as
possible.

In fact, I think you should start with a simpler example -- a simple
text file., and a block of 100 bytes, so you can easily see how things
work.

Just open an InputStream, and read and write, like this:

InputStream in = new FileInputStream(myFile);
try {
  int count = 0;
  byte[] buf = new byte[100];
  while ((count = in.read(buf)) >= 0) {
    String str = new String(buf, 0, count, "UTF-8");
    System.out.print("Read: ");
    System.out.println(str);
  }
} finally {
  in.close();
}

Run that on your computer, don't even bother on Android just yet. Step
through it with the debugger, and watch what happens and how it works.

Note I didn't supply a byte range to in.read(buf) -- you only need to
use those arguments if you only want it to use a portion of the byte
array.

If you really need to skip a couple of bytes, you can do that a couple
of ways -- new String(buf, 2, count-2), or you can do in.skip(2) to
skip two bytes. I suggest using skip(2) may be easier to understand,
if you really are skipping 2 bytes.

Then, once you're satisfied you understand how this works -- I think
you'll be ready to rewrite your code.

I hope this helps.

On Apr 18, 10:41 pm, "pramod.deore" <[email protected]> wrote:
> hello everybody, I am developing one application in that I am reading
> one file in parts (I am reading 102400 bytes at a time ) and sending
> it to server. size of file is more than 5mb. I had write following
> method to read the file
>
>  public void readFile()
>      {
>           try
>           {
>               int size = (int)myFile.length();
>               noOfChunks = (size/102400);
>               noOfChunks = noOfChunks+1;
>               System.out.println ("size of file is "+size);
>
>               System.out.println ("size is"+noOfChunks);
>               FileInputStream fstream = new FileInputStream(myFile);
>
>               DataInputStream in = new DataInputStream(fstream);
>
>               byte[] byteArray = new byte[102400];
>
>               for (int i=1;i!=noOfChunks;i++)
>               {
>
>                   xyz = in.skip(start);
>
>                   System.out.println ("skipped :"+start);
>
>                   in.readFully(byteArray, start, end);//[b]This is
> line no 133 here exception occurs[/b]
>
>                   str = new String(byteArray);
>                   sendData("0213456789_A~addressbook.txt~10~"+str);
>                   start = end;
>                   end = end+102398;
>               }
>
>           }
>           catch (IOException ioe)
>           {
>               ioe.printStackTrace();
>           }
>      }
>
> But when this method throws IndexOutOfboundException at line 133.
> Actually I thought because of "in.readFully(byteArray, start, end);
> "
> It reads from start to end and store it to byteArray, but it is not
> working like that.
>
> Anybody knows what I am doing wrong?
>
> Thanks
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group 
> athttp://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to