——- android培训、java培训、期待与您交流! ———-
概述
基本操作与字符流类相同。但它不仅可以操作字符,还可以操作其他媒体文件。
示例
示例1:
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamDemo{
public static void main(String[] args) throws IOException {
demo_write();
}
public static void demo_write() throws IOException {
//1、创建字节输出流对象,用于操作文件
FileOutputStream fos = new FileOutputStream( "bytedemo.txt");
//2、写数据,直接写入到了目的地中
fos.write( "abcdefg".getBytes());
//关闭资源动作要完成
fos.close();
}
}
运行结果
示例2:
import java.io.FileInputStream;
import java.io.IOException;
public class ByteStreamDemo{
public static void main(String[] args) throws IOException {
demo_read1();
System.out.println( "---------------");
demo_read2();
System.out.println( "---------------");
demo_read3();
}
//读取方式一
public static void demo_read1() throws IOException {
//1、创建一个读取流对象,和指定文件关联
FileInputStream fis = new FileInputStream("bytedemo.txt" );
//打印字符字节大小,不过要少用,文件太大,可能内存溢出
byte[] buf = new byte[fis.available()];
fis.read(buf);
System.out.println( new String(buf));
fis.close();
}
//读取方式二
public static void demo_read2() throws IOException {
FileInputStream fis = new FileInputStream("bytedemo.txt" );
//建议使用这种读取数据的方式
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1){
System.out.println( new String(buf,0,len));
}
fis.close();
}
//读取方式三
public static void demo_read3() throws IOException {
FileInputStream fis = new FileInputStream("bytedemo.txt" );
//一次读取一个字节
int ch = 0;
while((ch = fis.read()) != -1){
System.out.print(( char)ch);
}
fis.close();
}
}
运行结果图:
FileOutputStream、FileInputStream的flush方法内容为空,没有任何实现,调用没有意义。
字节流的缓冲区:同样是提高了字节流的读写效率。
练习
练习1
通过几种方式对MP3的进行拷贝,比较它们的效率。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyMp3Test{
public static void main(String[] args) throws IOException {
copy_1();
copy_2();
}
public static void copy_1() throws IOException {
FileInputStream fis = new FileInputStream("0.mp3" );
FileOutputStream fos = new FileOutputStream("1.mp3" );
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1){
fos.write(buf,0,len);
}
fis.close();
fos.close();
}
public static void copy_2() throws IOException {
FileInputStream fis = new FileInputStream("0.mp3" );
BufferedInputStream bufis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("2.mp3" );
BufferedOutputStream bufos = new BufferedOutputStream(fos);
int ch = 0;
while((ch = bufis.read()) != -1){
bufos.write(ch);
}
bufis.close();
bufos.close();
}
}
运行结果图:
练习2
读取一个键盘录入的数据,并打印在控制台上。
键盘本身就是一个标准的输入设备。对于java而言,对于这种输入设备都有对应的对象。
import java.io.IOException;
import java.io.InputStream;
public class ReadKey{
public static void main(String[] args) throws IOException {
readKey();
}
public static void readKey() throws IOException {
InputStream in = System.in;
int ch = in.read();//阻塞式方法
System.out.println(ch);
ch = in.read(); //阻塞式方法
System.out.println(ch);
ch = in.read(); //阻塞式方法
System.out.println(ch);
in.close();
}
}
运行结果图
小结:
1、获取键盘录入数据,然后将数据流向显示器,那么显示器就是目的地。
通过System类的setIn,setOut方法可以对默认设备进行改变。
System.setIn(new FileInputStream(“1.txt”));//将源改成文件1.txt。
System.setOut(new PrintStream(“2.txt”));//将目的改成文件2.txt
因为是字节流处理的是文本数据,可以转换成字符流,操作更方便。
BfferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw =
new BufferedWriter(new OutputStreamWriter(System.out));
2、默认的输入和输出系统不需要关,它会随着系统的结束而消失。
练习3
获取用户键盘录入的数据并将数据变成大写显示在控制台上,如果用户输入的是over,结束键盘录入。
思路:
1. 因为键盘录入只读取一个字节,要判断是否是over,需要将读取到的字节拼成字符串。
2. 那就需要一个容器:StringBuilder。
3. 在用户回车之前将录入的数据变成字符串判断即可。
import java.io.IOException;
import java.io.InputStream;
public class ReadKey{
public static void main(String[] args) throws IOException {
readKey();
}
public static void readKey() throws IOException {
//1、创建容器
StringBuilder sb = new StringBuilder();
//2、获取键盘读取流
InputStream in = System.in;
//3、定义变量记录读取到的字节,并循环获取
int ch = 0;
while((ch = in.read()) != -1){
//在存储之前需要判断是否是换行标记,因为换行标记不存储
if(ch == '\r' )
continue;
if(ch == '\n' ){
String temp = sb.toString();
if("over" .equals(temp))
break;
System.out.println(temp.toUpperCase());
sb.delete(0,sb.length());
} else{
//将读取到的字节存储到StringBuilder中
sb.append(( char)ch);
}
}
}
}