您的位置:首页 > 职场人生

黑马程序员——JAVA基础------IO流(二)----字节流

2015-07-14 11:35 666 查看
——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——

一、IO流概述



IO流:输入输出流(Input/Output)

流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据传输特性将流抽象为各种类,方便更直观的进行数据操作。

IO流的分类

根据处理数据类型的不同分为:字符流和字节流

根据数据流向不同分为:输入流和输出流

字节流分为字节输出流(OutputStream)和字节输入流(InputStream)

字节输出流(OutputStream)定义

public abstract class OutputStream extands Object implements Closable,Flushable


此类抽象类是表示输出字节流的所有类的超累。输出流接受输出字节并将这些字节发送到某个接收器。





代码演示:

package com.joe.io;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 字节输出流格式:
* OutputStream out = new FileOutputStream()
* try{}
* catch{}
* finally{}
*
* 思路:
* 1、创建输出流对象
* 2、创建字节数组接收写入数据
* 3、将数组中的数据遍历输出
* 4、关闭流对象
*
* 注意事项:一定要关闭流
* @author joe
*
*/

public class OutputStreamDemo {

public static void main(String[] args) {
// write1();
write2();
System.out.println("success");
}

/**
* 字节输出流的方式一:每次输出一个字节
*/
public static void write1() {
OutputStream out = null;
try {
// 创建一个文件字节输出流对象
out = new FileOutputStream("d:\\1.txt");

String info = "hello,IO";
byte[] bytes = info.getBytes();
for (int i = 0; i < bytes.length; i++) {
// 向文件中输出
out.write(bytes[i]);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭流
if (out != null)
out.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}

/**
* 字节输出流的方式二:每次输出指定大小的字节
*/
public static void write2() {
OutputStream out = null;
try {
// 创建一个文件字节输出流对象(参数true表示追加输出)
out = new FileOutputStream("d:\\1.txt", true);

String info = "i love java!";
byte[] bytes = info.getBytes();
out.write(bytes);// 输出一个字节数组
// out.write(bytes,0,5);//输出一个字节数组中的指定范围的字节

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}


字节输入流(InputStream)定义

public abstract class InputStream extends Object implements Closeable






代码演示:

package com.joe.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
* 字节输入流格式: InputStream in = new FileInputStream()
*              try{}
*              catch{}
*              finally{}
*
* 思路:
* 1、创建输入流对象
* 2、定义一个字节,用来保存实际读取的字节数,-1表示没有数据
* 3、创建一个字节数组,用来每次读取数据
* 4、创建一个StringBuilder缓冲区,用来接收数据
* 5、输出读取数据
* 6、关闭流对象
*
* 注意事项:一定要关闭流
*
* @author joe
*
*/

public class InputStreamDemo {

public static void main(String[] args) {
read1();
read2();
read3();
}

/**
* 字节输入流的读取方式一:每次读取一个字节
*/
public static void read1() {
InputStream in = null;
try {
// 构造一个字节输入流对象
in = new FileInputStream("d:\\1.txt");
int b = -1;// 用于保存实际读取字节数,-1表示没有数据
while ((b = in.read()) != -1) {
System.out.print((char) b);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流
try {
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 字节输入流的读取方式二:一次性读取所有字节
*/
public static void read2() {

File f = new File("d:\\1.txt");
// 构造一个字节输入流对象
InputStream in = null;
try {
in = new FileInputStream(f);
// 根据文件的大小构造字节数组
byte[] bytes = new byte[(int) f.length()];
int len = in.read(bytes);
System.out.println(new String(bytes));
System.out.println("len=" + len);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流
try {
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 字节输入流的读取方式三:每次读取指定大小的字节(常用)
*/
public static void read3() {
File f = new File("d:\\1.txt");
// 构造一个字节输入流对象
InputStream in = null;
try {
in = new FileInputStream(f);
// 指定每次要读取的字节数组
// 创建一个长度为10的数组,每次至多只能存进10个字节
byte[] bytes = new byte[10];
int len = -1;// 每次读取的实际长度
StringBuilder sb = new StringBuilder();
while ((len = in.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len));
}
// 输出
System.out.println(sb);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流
try {
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java-io