Hello all!

I have some troubles in saving a file by byte chunks, (will use in saving large file size.). My goal is to chunk the file bytes first into a certain size before writing.

I have this code:

FileConnection fc = (FileConnection) Connector.open("file.ext",
					Connector.READ);
InputStream fis = (InputStream) fc.openInputStream();
byte[] fileData = new byte[(int) fc.fileSize()];
fis.read(fileData);

int MAX = 1024;

while(saved < fSize)
{
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	if((fSize - saved) < MAX){
		MAX = fSize - saved;
	}
	for(int i = saved; i<MAX; i++){
		baos.write(fileData[i]);
	}
	fout.write(baos.toByteArray());
	saved += MAX;
}

A file is saved but not complete. I think only 1024 bytes are written. Any suggestions? Explanations?

Your help will be highly appreciated...

Thank you!

Recommended Answers

All 4 Replies

Are you doing a Huffman Coding project? If not, I don't see the purpose of what you're doing. (In other words, if you aren't doing Huffman Coding, then what do you mean by 'chunk the file bytes into a certain size before writing'? .... I don't see how you'd know what characters they represent once you wanted to open the file for reading again.)

hi,

Thanks for the reply, I have no idea about huffman coding. But, i'll try to explain again what I wana do.

For example, in a certain webserver, there is a limit of filesize that could be uploaded one at a time and you have a file that is greater than this limit.

This is how it looks like:

size of the file you wana send = 2050bytes
limit to webserver upload per transaction = 1024bytes

I wish to send the file with 2050bytes of size, but I cant do it in one time, since there is a limit of 1024bytes.
so,
I wish to send 1024bytes of data first. then,
1024 bytes again and,
2bytes remaining.

How will I do this without any data loss?

Thank you again...

hi,

Thanks for the reply, I have no idea about huffman coding. But, i'll try to explain again what I wana do.

For example, in a certain webserver, there is a limit of filesize that could be uploaded one at a time and you have a file that is greater than this limit.

This is how it looks like:

size of the file you wana send = 2050bytes
limit to webserver upload per transaction = 1024bytes

I wish to send the file with 2050bytes of size, but I cant do it in one time, since there is a limit of 1024bytes.
so,
I wish to send 1024bytes of data first. then,
1024 bytes again and,
2bytes remaining.

How will I do this without any data loss?

Thank you again...

We can see that you solved your problem. Your problem explanation was a solution of what you are trying to say.

fSize isn't defined (as far as I can see).
The problem's probably in the way the loops are constructed, but because you posted your code without tags and proper indentation it's too hard to see.[code=java] tags and proper indentation it's too hard to see.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.