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