Questions about the available () method in InputStream

Source: Internet
Author: User

enumeration E=filesystemregistry.listroots ();//Get File path
String root = (string) e.nextelement ();
String url= "file:///" +root+filename;
fileconnection conn=null;
try{
conn= (fileconnection) Connector.open (Url,connector.read); Open a text file
DataInputStream In=conn.opendatainputstream (); Open Data input stream
int size=in.available (); Check text file size
System.out.println (size);
byte[] Buffer=new byte[size]; Creating a text buffer
in.read (buffer); Read all content into the buffer
string Content=new string (buffer,encoding); Create a string with the contents of the buffer
content=content.substring (1);
Buffer=null; Releasing buffers
In.close (); Turn off the data entry stream
Conn.close (); Close a text file
In=null;
           .....

int size=in.available (); Gets the size of 0, but if you manually set the buffer size, is able to read the data of the file, then the input stream should be normal, but why in.available () can not get the size of the input stream,


if you just want to get the file size. can be used directly with int size= (int) conn.filesize () ; Inputsteam available () method returns 0 forever if not overridden. FileInputStream realized the available () method, you can get the value

Questions about the available () method in InputStream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.