Java基础回顾 : 字节流操作

字节流主要是操作字节数据,可以处理一切数据 , 例如:图片、音乐、文本。

在Java 之中如果要想操作字节流可以使用两个类完成:OutputStreamInputStream

观察一下OutputStream 类的定义 :
public abstract class OutputStream extends Object implements Closeable, Flushable


★ . OutputStream 是一个抽象类,在这个抽象类之中定义了如下的几个方法:
├ . · 关闭方法:
        public void close() throws IOException
├ . · 强制刷新缓冲区:
        public void flush() throws IOException
├ . · 三个输出方法:
        ① . public void write(byte[] b) throws IOException : 将 b.length 个字节从指定的 byte 数组写入此输出流。
        ② . public void write(byte[] b,int off,int len) throws IOException : 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
        ③ . public abstract void write(int b) throws IOException : 将指定的字节写入此输出流。


★ . 用其子类为其实例化操作 : 如果进行文件的操作可以使用FileOutputStream 子类完成 .

├ . · 直接接收处理的文件:
        ① . public FileOutputStream(File file) throws FileNotFoundException
        ② . public FileOutputStream(File file,boolean append) throws FileNotFoundException 

        ③ . public FileOutputStream(String name) throws FileNotFoundException

        ④ . public FileOutputStream(String name, boolean append) throws FileNotFoundException

eg : 文件输出流的范例:

package example;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
 * 文件的输出流
 * @author Miao
 *
 */
public class TestDemo {
	public static void main(String[] args) throws Exception {
		String msg = "好好学习,天天向上.";
		byte data[] = msg.getBytes();
		//实例化一个目标文件File对象
		File destFile = new File("e:\\msg\\msg.txt");
		if(!destFile.getParentFile().exists()) {
			destFile.getParentFile().mkdirs();
		}
		//如果要以追加的方式写入的话,FileOutputStream(File file,boolean append)
		OutputStream os = new FileOutputStream(destFile); 
		os.write(data);
		os.close();
	}
}
▲ . 上面这个例子需要注意的是 : 在使用字节输出流时,即使没有关闭流,那么内容也可以直接进行输出,因为字节流处理过程之中不会牵扯到缓存的问题,而是直接操作目标文件。


======================================================================================================================


与输出流对应的是字节的输入流:InputStream,来看一下InputStream 类的定义:

public abstract class InputStream extends Object implements Closeable


★ . OutputStream 也是一个抽象类 , 在此重点只观察三个读取方法 :
├ . · 读取单个字节:每当执行一次read()方法,那么都会返回一个字节数据,如果现在数据读取完了,还要继续读取的话,那么就返回-1;
        public abstract int read() throws IOException
├ . · 将内容读取到字节数组之中,返回的是读取的长度,如果现在没有内容,返回的为-1
        public int read(byte[] b) throws IOException
├ . · 将内容读取到部分字节数组之中,返回的是读取的长度,没有内容了,返回为-1
        public int read(byte[] b,int off,int len) throws IOException

★ . 用其子类为其实例化操作 : 如果进行文件的操作可以使用FileInputStream 子类完成 .

        ① . public FileInputStream(File file) throws FileNotFoundException

        ② . public FileInputStream(String name) throws FileNotFoundException

eg : 文件输入流的范例 :

package example;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
/**
 * 文件的输入流
 * @author Miao
 *
 */
public class TestDemo {
	public static void main(String[] args) throws Exception {
		File srcFile = new File("e:\\msg\\msg.txt");
		if(srcFile.exists()) {
			InputStream is = new FileInputStream(srcFile);
			byte buf[] = new byte[1024];
			int len = 0;
			while((len=is.read(buf)) != -1) {
				System.out.println(new String(buf,0,len));
			}
			is.close();
		}
	}
}



猜你喜欢

转载自blog.csdn.net/sinat_18882775/article/details/51533993