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