Byte Stream writes a string to the file and reads the string

Source: Internet
Author: User

1. InputStream (input byte stream)

InputStream is an abstract class that defines the Java streaming byte input pattern, and all methods of that class throw an Ioexceptin exception when an error occurs.

Method Description  

int available () returns the number of input bytes currently readable

void Close () closes the input stream. IOException is generated if read again after closing

void mark (int numbytes) re-enters the stream with the current dot to place a flag.

Boolean marksupported () returns true if the called Stream supports mark ()/reset ()

int read ()

int read (byte buffer[])

int read (byte buffer[],int offset,int numbytes)

void Reset ()

Long Skip (Long numbytes)


2, OutputStream

void Close ()

void Flush ()

void write (int b)

void write (byte buffer[])

void Write (Byte buffer[],int offset,int numbytes)


3. FileInputStream (file input stream)

FileInputStream (String filepath)

FileInputStream (File fileobj)

4. FileOutputStream (file input stream)

FileOutputStream (String filepaht)

FileOutputStream (File fileobj)

FileOutputStream (String Filepath,boolean append)


Package Io;import java.io.*;p ublic class Streamdemo {public static void main (string[] args) {//create a file F = new ("E: \\Java\\iodemo.txt "); Writes data to a file OutputStream out = null;try {out = new FileOutputStream (f);} catch (FileNotFoundException e) {//TODO Auto-gener Ated catch Blocke.printstacktrace ();} String str = "Hello world!!"; byte b[] = Str.getbytes (); try {out.write (b);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace () ;} try {out.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Read file InputStream in = null;try {in = new FileInputStream (f);} catch (FileNotFoundException e) {//TODO auto-generated CATC H Blocke.printstacktrace ();} byte b1[] = new Byte[1024];int i = 0;try {i = In.read (B1);} catch (IOException e) {//TODO auto-generated catch Blocke.pri Ntstacktrace ();}       try {in.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} System.out.println (New String (B1,0,i));}}








Byte Stream writes a string to the file and reads the string

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.