概念
按照字节为单位,对数据进行输入输出 ,字节流有 分为 输入流和输出流
字节的输入流
把数据从外部磁盘读取到虚拟机的内存 ,是以字节为单位,以字节为单位的流,可以读取任何文件,他是一个二进制的流,他可以读取的文件比如:文本、图片、音频、视频等
在JDK中 针对字节输入流,封装了一个抽象类 :InputStream
下面我们以 FileInputStream 为例 :
InputStream的核心API方法 :
下面我们把 这个磁盘位置的数据 :E:\woniu\57\JavaSE\day19\Hello.java 读取到内存
public class TestInputStream {
public static void main(String[] args) {
//读取E:\woniu\57\JavaSE\day19\Hello.java
File f = new File(“E:/woniu/57/JavaSE/day19/Hello.java”);
InputStream input = null;
//创建一个文件字节输入流
try {
//创建一个IO流对象 用完之后 必须要关闭 因为文件操作是资源操作
input = new FileInputStream(f);
//开始读取数据 使用read方法 read方法 如果读到 -1 说明文件读完了
int i = input.read();
while(i != -1){
System.out.print((char)i);
i = input.read();
}
//input.close(); //在这里关闭 是很不好的
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//在这里关闭资源
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class TestInputStream2 {
public static void main(String[] args) {
//读取E:\woniu\57\JavaSE\day19\Hello.java
File f = new File(“E:/woniu/57/JavaSE/day19/Hello.java”);
InputStream input = null;
try {
input = new FileInputStream(f);
//创建一个字节数组 存储读取到的数据
byte[] bytes = new byte[(int) f.length()];
int read = input.read(bytes);
System.out.println(read);
//利用字节数组 创建一个字符串
String s = new String(bytes);
System.out.println(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class TestInputStream3 {
public static void main(String[] args) {
//读取E:\woniu\57\JavaSE\day19\Hello.java
File f = new File(“E:/woniu/57/JavaSE/day19/Hello.java”);
System.out.println(f.length()); // 172 个字节
InputStream input = null;
try {
input = new FileInputStream(f);
//指定 字节数组就是 32个
byte[] bytes = new byte[32];
//读取数据
int len = input.read(bytes,0,bytes.length);
while(len != -1){
//构造方法 从字节数组中 取 len 个长度 创建字符串
String s = new String(bytes,0,len);
System.out.print(s);
// len 是每次读到多少个字节
len = input.read(bytes,0,bytes.length);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
字节输出流
所有的字节输出流都一个抽象的父类 : OutputStream
我们在使用输出流时 ,使用他的子类 :
下面我们以FileOutputStream 为例 ,做数据输出:
OutputStream的API方法 :
public class TestOutput {
public static void main(String[] args) {
String s = “上海校区蜗牛学院!!!”;
//我要把这个字符串 输出到一个文件 E:\woniu\57\JavaSE\day19\demo.txt
File f = new File(“E:\woniu\57\JavaSE\day19\demo.txt”);
//文件如果不存在 需要新创建这个文件
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//因为我们使用的流 是字节流 所以需要把字符串 转成 字节
byte[] bytes = s.getBytes();
System.out.println(bytes.length);
OutputStream output = null;
try {
//true :在后面添加
//false : 覆盖文件 默认 false
output = new FileOutputStream(f,true);
//按照字节 向文件输出 按字节输出
/for(int i=0;i
}
//一次性输出一个字节数组
//output.write(bytes);
//按照字节数组输出 ,但是可以指定输出的长度
//output.write(bytes, 0, bytes.length);
output.write(bytes, 0, 10);
System.out.println(“写出结束!”);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
利用 字节输入输出流 把 E:/Hello.java 复制到 C:/Hello.java
public class TestCopy {
public static void main(String[] args) {
//利用 字节输入输出流 把 E:/Hello.java 复制到 C:/Hello.java
File inFile = new File(“E:/woniu/57/JavaSE/day19/Hello.java”);
File outFile = new File(“C:/Hello.java”);
if(!outFile.exists()){
try {
outFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//创建 输入 输出流
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(inFile);
out = new FileOutputStream(outFile);
//1.每次读写一个字节
/int len = in.read();
while(len != -1){
out.write(len);//写出去
len = in.read();
}/
//2.一次性读写
/byte[] bytes = new byte[(int) inFile.length()];
in.read(bytes);
out.write(bytes);/
//3.读写指定长度
byte[] bytes = new byte[10];
int len = in.read(bytes, 0, bytes.length);
while(len != -1){
out.write(bytes,0,len);
len = in.read(bytes, 0, bytes.length);
}
System.out.println(“复制完毕!”);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
in.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}