Use of inputstream in java

Source: Internet
Author: User

Use of inputstream in java

Inputstream in java is a byte-oriented stream abstract class. It derives various classes based on specific applications.

For example, FileInputStream is an object that inherits from InputStream and is used to read file streams. Its specific inheritance Structure



We found that it is inherited from the abstract class InputStream.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ztLDx7zM0Pi/logs/wrzPwsHLoaM8L3A + CjxwPjxwcmUgY2xhc3M9 "brush: java;"> package com. itbuluoge. test; import java. io. fileInputStream; public class ByteInputFile {public static String read () throws Exception {FileInputStream fit = new FileInputStream ("test.txt"); int c; String sb = ""; while (c = fit. read ())! =-1) {sb + = (char) c;} return sb ;} /*** @ param args * @ throws Exception */public static void main (String [] args) throws Exception {// TODO Auto-generated method stubSystem. out. println (read ());}}

Output result




Note that InputStream is a byte-oriented stream. Therefore, each operation is for one byte. Therefore, it cannot process Chinese characters and garbled characters may occur when reading and writing data. The inherited FileInputStream is the same. So here we test that only English characters are used

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.