Reading files using InputStream

Source: Internet
Author: User

In Java, you can use InputStream to read a file, which is the input of a byte stream. When reading the contents of the file into the program, you need to use a byte array to store, so there are the following two questions:

1. How to create a byte array of the appropriate size, if the size of the input stream is known.

2. If you do not know the size of the input stream, it is necessary to create a large byte array, then there is likely to be empty content in byte, then how to correctly fit the contents of the byte array in the output?

Look first: The solution is to get the size of the input stream and create a byte array of this size. The code looks like this: View Plaincopy to Clipboardprint?
Use InputStream to read data from a file and establish an appropriate storage byte array in case of known file size
Import Java.io.File;
Import Java.io.InputStream;
Import Java.io.FileInputStream;
public class InputStreamDemo01
{
public static void Main (String args[]) throws exception{
File F = new file ("E:" +file.separator+ "Java2" +file.separator+ "Streamdemo" +file.separator+ "test.txt");
InputStream in = new FileInputStream (f);
BYTE b[]=new byte[(int) f.length ()]; Create an array of appropriate file sizes
In.read (b); Reads the contents of the file into the b[] array
In.close ();
System.out.println (New String (b));
}
}
Use InputStream to read data from a file and establish an appropriate storage byte array in case of known file size
Import Java.io.File;
Import Java.io.InputStream;
Import Java.io.FileInputStream;
public class InputStreamDemo01
{
public static void Main (String args[]) throws exception{
File F = new file ("E:" +file.separator+ "Java2" +file.separator+ "Streamdemo" +file.separator+ "test.txt");
InputStream in = new FileInputStream (f);
BYTE b[]=new byte[(int) f.length ()]; Create an array of appropriate file sizes
In.read (b); Reads the contents of the file into the b[] array
In.close ();
System.out.println (New String (b));
}
}

The second problem: the solution to the problem is to get the end of the input stream, which is the trailing index position in byte. Can be implemented through the Read () method, and read () returns the read byte content, which returns 1 when the content is empty. This feature can be used to solve a second problem. The code is as follows:

View Plaincopy to Clipboardprint?
Read the file at the end of the same judgment file
Import Java.io.File;
Import Java.io.InputStream;
Import Java.io.FileInputStream;
public class InputStreamDemo02
{
public static void Main (String args[]) throws exception{
File F = new file ("E:" +file.separator+ "Java2" +file.separator+ "Streamdemo" +file.separator+ "test.txt");
InputStream in = new FileInputStream (f);
byte b[] = new byte[1024];
int len = 0;
int temp=0; All read content is received using temp
while ((Temp=in.read ())!=-1) {//When not finished reading, continue reading
b[len]= (byte) temp;
len++;
}
In.close ();
System.out.println (New String (B,0,len));
}
}

Reading files using 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.