----<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! ------
IO流常用基类-字节流
基本操作与字符流类相同。但它不仅可以操作字符,还可以操作其他媒体文件。
示例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();
- }
- }
P.S.
FileOutputStream、FileInputStream的flush方法内容为空,没有任何实现,调用没有意义。
字节流的缓冲区:同样是提高了字节流的读写效率。
练习:
通过几种方式对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();
- }
- }
IO流小结(掌握)
IO流
|--字节流
|--字节输入流
InputStream
int read():一次读取一个字节
int read(byte[] bys):一次读取一个字节数组
|--FileInputStream
|--BufferedInputStream
|--字节输出流
OutputStream
void write(int by):一次写一个字节
void write(byte[] bys,int index,int len):一次写一个字节数组的一部分
|--FileOutputStream
|--BufferedOutputStream
|--字符流
|--字符输入流
Reader
int read():一次读取一个字符
int read(char[] chs):一次读取一个字符数组
|--InputStreamReader
|--FileReader
|--BufferedReader
String readLine():一次读取一个字符串
|--字符输出流
Writer
void write(int ch):一次写一个字符
void write(char[] chs,int index,int len):一次写一个字符数组的一部分
|--OutputStreamWriter
|--FileWriter
|--BufferedWriter
void newLine():写一个换行符
void write(String line):一次写一个字符串
1:数据操作流(操作基本类型数据的流)(理解)
(1)可以操作基本类型的数据
(2)流对象名称
DataInputStream
DataOutputStream
2:内存操作流(理解)
(1)有些时候我们操作完毕后,未必需要产生一个文件,就可以使用内存操作流。
(2)三种
A:ByteArrayInputStream,ByteArrayOutputStream
B:CharArrayReader,CharArrayWriter
C:StringReader,StringWriter
3:打印流
(1)字节打印流,字符打印流
(2)特点:
A:只操作目的地,不操作数据源
B:可以操作任意类型的数据
C:如果启用了自动刷新,在调用println()方法的时候,能够换行并刷新
D:可以直接操作文件
问题:哪些流可以直接操作文件呢?
看API,如果其构造方法能够同时接收File和String类型的参数,一般都是可以直接操作文件的
(3)复制文本文件
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("b.txt"),true);
String line = null;
while((line=br.readLine())!=null) {
pw.println(line);
}
pw.close();
br.close();
4:标准输入输出流(理解)
(1)System类下面有这样的两个字段
in 标准输入流
out 标准输出流
(2)三种键盘录入方式
A:main方法的args接收参数
B:System.in通过BufferedReader进行包装
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
C:Scanner
Scanner sc = new Scanner(System.in);
(3)输出语句的原理和如何使用字符流输出数据
A:原理
System.out.println("helloworld");
PrintStream ps = System.out;
ps.println("helloworld");
B:把System.out用字符缓冲流包装一下使用
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
5:随机访问流(理解)
(1)可以按照文件指针的位置写数据和读数据。
(2)案例:
A:写数据
B:读数据
C:获取和改变文件指针的位置
6:合并流(理解)
(1)把多个输入流的数据写到一个输出流中。
(2)构造方法:
A:SequenceInputStream(InputStream s1, InputStream s2)
B:SequenceInputStream(Enumeration<? extends InputStream> e)
7:序列化流(理解)
(1)可以把对象写入文本文件或者在网络中传输
(2)如何实现序列化呢?
让被序列化的对象所属类实现序列化接口。
该接口是一个标记接口。没有功能需要实现。
(3)注意问题:
把数据写到文件后,在去修改类会产生一个问题。
如何解决该问题呢?
在类文件中,给出一个固定的序列化id值。
而且,这样也可以解决黄色警告线问题
8:Properties(理解)
(1)是一个集合类,Hashtable的子类
(2)特有功能
A:public Object setProperty(String key,String value)
B:public String getProperty(String key)
C:public Set<String> stringPropertyNames()
(3)和IO流结合的方法
把键值对形式的文本文件内容加载到集合中
public void load(Reader reader)
public void load(InputStream inStream)
把集合中的数据存储到文本文件中
public void store(Writer writer,String comments)
public void store(OutputStream out,String comments)
9:NIO(了解)
(1)JDK4出现的NIO,对以前的IO操作进行了优化,提供了效率。但是大部分我们看到的还是以前的IO
(2)JDK7的NIO的使用
Path:路径
Paths:通过静态方法返回一个路径
Files:提供了常见的功能
复制文本文件
把集合中的数据写到文本文件