944,011 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5077
  • Java RSS
Apr 16th, 2006
0

Noob : Input Stream Trouble

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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.

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crws416 is offline Offline
11 posts
since Mar 2006
Apr 17th, 2006
0

Re: Noob : Input Stream Trouble

Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  1. long length = is.available();
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Apr 18th, 2006
0

Re: Noob : Input Stream Trouble

cheers,

I cant believe i did not see that :o .

Cheers again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crws416 is offline Offline
11 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how do you reverse a linked list using recursion and return it as a string?
Next Thread in Java Forum Timeline: class diagram/uml





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC