Noob : Input Stream Trouble

Reply

Join Date: Mar 2006
Posts: 11
Reputation: crws416 is an unknown quantity at this point 
Solved Threads: 0
crws416 crws416 is offline Offline
Newbie Poster

Noob : Input Stream Trouble

 
0
  #1
Apr 16th, 2006
Hi all,

I have taken some code to read a file into an Byte array, which is then returned to another part of my code, below is what i have.

  1. public void jButton6_actionPerformed(ActionEvent e) {
  2. String Filelocation = "c:\\temp\\hello.txt";
  3.  
  4. try{
  5. InputStream is = new FileInputStream(Filelocation);
  6.  
  7. //get the size of the file
  8. long length = Filelocation.length();
  9.  
  10. //range check to make sure length is not larger than int max val
  11. if(length > Integer.MAX_VALUE){
  12. XML(ErrorLog,"ERROR",2,0,"File trying to be sent is too large");
  13. }
  14.  
  15. //create byte array
  16. byte[] DataStore = new byte[(int)length];
  17.  
  18. //read
  19. int offset =0;
  20. int numread =0;
  21. while(offset < DataStore.length && (numread = is.read(DataStore,offset,DataStore.length-offset))>=0){
  22. offset += numread;
  23. }
  24.  
  25. //close the input stream
  26. is.close();
  27.  
  28. //call the set method of store the data
  29. try{
  30. ds.setdata(DataStore);
  31. }catch(Exception ex){
  32. XML(ErrorLog,"ERROR",2,0,"Error storing data : "+ex.toString());
  33. System.err.println("Error storing data");
  34. }
  35. }catch(Exception x){
  36. XML(ErrorLog,"ERROR",2,0,"Error serialising : "+x.toString());
  37. System.err.println("Error serialising");
  38. }
  39. }

I am also using the following to display the byte array.

  1. public void jButton7_actionPerformed(ActionEvent e) {
  2. byte[] Data = ds.getdata();
  3.  
  4. try{
  5. FileOutputStream os = new FileOutputStream("c:\\temp\\HOE.txt");
  6.  
  7. os.write(Data);
  8. os.close();
  9.  
  10. }catch(Exception ex){
  11. XML(ErrorLog,"ERROR",2,0,"Error saving download file : "+ex.toString());
  12. }
  13.  
  14. }

this is my problem, i think the read method is not reading all of the file, i say this as i put a test file in which had "this is a test file" written in. When it came to reading the byte array out it read "this is a test fi" and it seems odd that it is cutting of at 16 characters.

Is there something i am missing.

Any help would be greatly appreciated

Cheers.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Noob : Input Stream Trouble

 
0
  #2
Apr 17th, 2006
  1. String Filelocation = "c:\\temp\\hello.txt";
  2. //get the size of the file
  3. long length = Filelocation.length();

Get the size of what? File? Looks more like a String to me. A 16 character length string to be exact. Try this instead:

  1. long length = is.available();
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 11
Reputation: crws416 is an unknown quantity at this point 
Solved Threads: 0
crws416 crws416 is offline Offline
Newbie Poster

Re: Noob : Input Stream Trouble

 
0
  #3
Apr 18th, 2006
cheers,

I cant believe i did not see that :o .

Cheers again.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC