| | |
Noob : Input Stream Trouble
![]() |
•
•
Join Date: Mar 2006
Posts: 11
Reputation:
Solved Threads: 0
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.
I am also using the following to display the byte array.
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.
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)
public void jButton6_actionPerformed(ActionEvent e) { String Filelocation = "c:\\temp\\hello.txt"; try{ InputStream is = new FileInputStream(Filelocation); //get the size of the file long length = Filelocation.length(); //range check to make sure length is not larger than int max val if(length > Integer.MAX_VALUE){ XML(ErrorLog,"ERROR",2,0,"File trying to be sent is too large"); } //create byte array byte[] DataStore = new byte[(int)length]; //read int offset =0; int numread =0; while(offset < DataStore.length && (numread = is.read(DataStore,offset,DataStore.length-offset))>=0){ offset += numread; } //close the input stream is.close(); //call the set method of store the data try{ ds.setdata(DataStore); }catch(Exception ex){ XML(ErrorLog,"ERROR",2,0,"Error storing data : "+ex.toString()); System.err.println("Error storing data"); } }catch(Exception x){ XML(ErrorLog,"ERROR",2,0,"Error serialising : "+x.toString()); System.err.println("Error serialising"); } }
I am also using the following to display the byte array.
Java Syntax (Toggle Plain Text)
public void jButton7_actionPerformed(ActionEvent e) { byte[] Data = ds.getdata(); try{ FileOutputStream os = new FileOutputStream("c:\\temp\\HOE.txt"); os.write(Data); os.close(); }catch(Exception ex){ XML(ErrorLog,"ERROR",2,0,"Error saving download file : "+ex.toString()); } }
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.
•
•
Join Date: Mar 2004
Posts: 763
Reputation:
Solved Threads: 38
Java Syntax (Toggle Plain Text)
String Filelocation = "c:\\temp\\hello.txt"; //get the size of the file 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)
long length = is.available();
![]() |
Similar Threads
- Tutorial: User Input: Strings and Numbers [C++] (C++)
- overloaded input stream help (C++)
- noob char input problem (C++)
- Question:: reading from an input stream. (C++)
- reading input ... quick C++ question ... (C++)
Other Threads in the Java Forum
- Previous Thread: how do you reverse a linked list using recursion and return it as a string?
- Next Thread: class diagram/uml
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows





