Io stream-related knowledge points in Java

Source: Internet
Author: User

Package zdbio;

Import java.io.File;
Import java.io.FileNotFoundException;
Import java.io.FileOutputStream;
Import java.io.IOException;
Import java.io.OutputStream;

public class OutputStreamDemo1 {

/**
* @throws IOException
* @throws IOException
*
*/

  Use OutputStream byte output stream below for write operations


public static void main (string[] Args) throws ioexception{

/**
* Specific steps for using IO stream:
* 1. Use file to locate the files to be manipulated
* 2. (instantiate inputstream, outstream, reader, writer) using a byte stream or a subclass of character stream
* 3. Read and write operations
* 4. Close the stream, except BufferedReader exception
*/

File File = new file ("f:" +file.separator+ "zdb1.txt");//files found to be manipulated
OutputStream out = null;
out = new FileOutputStream (file,true);//instantiate with subclasses of OutputStream
String str = "xxx 10 years of life planning, must have a plan so that your life will have a clear direction to not be lost." ";//the information to be output
byte b[] = str.getbytes ();//change str to byte array
Out.write (b);//write Data
Out.close ();//close Stream

}

}

use the InputStream byte stream below to read the operation:


Package zdbio;

Import java.io.File;
Import java.io.FileInputStream;
Import java.io.InputStream;

/**
* Read operation via InputStream Byte stream
*
*/
public class Inputstreamdemo {
public static void main (string[] Args) throws Exception {
Note This file must exist otherwise a java.io.FileNotFoundException exception will occur
File.separator represents the delimiter, which is represented in Windows \, represented on unix/, which can cross-platform
File File = new file ("f:" +file.separator+ "zdb1.txt");
InputStream input = null;
input = new FileInputStream (file);
byte b[] = new byte[1024];//a piece of memory to store the read content
int len = input.read (b);//read The file into the character array
Where new String (byte[]bytes,int offset,int length),
Indicates that a string will be created, starting with offset and length
System.out.println (new String (b,0,len));
Input.close ();

}

}






















































Io stream-related knowledge points in Java

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.