Common input and output stream case studies in Java

Source: Internet
Author: User
Tags readfile

BYTE input stream:

1.FileInputStream

Purpose: Obtain input bytes from files in file system, often used to read raw byte stream of image, sound, etc., read character flow, consider using FileReader

Detailed constructors and common methods can refer to the API documentation, online already has the Chinese version of the API, I am an e illiterate, hurt

Here's one of the most common ways to do this:

read(byte[] b, int off, int len)
lenreads the most bytes of data from this input stream into a byte array.

->off:b the offset in the byte array

Tip: Array offset, such as a[1,2,3,4,5] array, the default array first should point to a[0], if the offset is 2, point to a[1]

Case:

Import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;public class Test {/* * * @param args */public static void main (string[] args) {//TODO auto-generated method stubtry {int b;//create an input stream FILEINPUTST Ream ReadFile = new FileInputStream ("E:\\test.txt"); byte buffer[] = new byte[2500];//creates a byte array//reads bytes from the input stream and stores it in the buffer array. The longest reads 2,500 bytes, the return value B is the actual read length B = readfile.read (buffer, 1, 2000);//1 is the starting offset in the buffer array, string str = new string (buffer, 0, B, "Defa Ult "); System.out.println (str); System.out.println (b); Readfile.close ();} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();}}}

Not to be continued, there are a lot of commonly used flow operations, will be added later, very late, the bed, every day to take a little time to learn, slowly accumulate, I can do it!!


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.