The InputStream class and Fileinputstream__java of JAVA learning

Source: Internet
Author: User
I. Inputstram class 1. Class DescriptionFirst of all, there is an understanding of Java input data, Java is used in the form of objects to use and process the input data, but our InputStream is an abstract class, and can not directly create objects to save data, But we can create an object by using the form of a subclass of InputStream, because the object of the subclass can also be an object of the parent class, so you can use the object of the subclass by the method of the InputStream class. Also note that the declaration by the Read () method indicates that this method throws an exception of the IOException type, so be sure to do exception handling.

2. Class method Description

The main method has two is read (), the function is reads the next character from the input stream, here should notice, reads the data from the standard input stream, not each input one character to form an input flow, but enters the carriage return, an entire line forms an input flow.

The other is the close () feature that closes the input stream and frees the resource.

3. Using the example

Import Java.io.InputStream;
Import java.io.IOException;
Class echo 
{public
	echo (InputStream in)//here uses InputStream as a formal parameter in
	{
		try<span style= white-space: Pre ">		</span>//system.in is an instance object of the subclass PrintStream of InputStream, passing in
		{while
			(true) as an argument
			{
				int temp;
				Temp=in.read ();
				if (temp==-1) break;
				System.out.print ((char) temp);
			}
		catch (IOException e)
		{
			System.err.println ("Something is Worge");
}} public class Myinputstream 
{public
	static void Main (String args[])
	{
		new echo (system.in);
	}
}

two. FileInputStream class 1. Class Description This class is not an abstract class, you can create objects directly to achieve data storage and processing. 2. Class methods This class is mainly composed of three important methods, the most important is the construction method, the parameter is the file to read the data, the other two methods are read () and the Close () method and 3. Using the example

Import Java.io.FileInputStream;
Import java.io.IOException;
public class J_fileread {public
	static void Main (String args[])
	{
		try
		{
			
			FileInputStream f=new FileInputStream ("Input.txt");
			int b;
			B=f.read ();
			while (true)
			{
				System.out.print ((char) b);
				B=f.read ();
				if (b==-1) break;
			System.out.println ();
			F.close ();
		}
		catch (Exception e)
		{
			System.out.println ("Something is wrong!");
			System.exit (0);
		}}}



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.