---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ---------------------
1.IO流的分类
1.按流的方向分类:输入流和输出流
2.按流的数据单位分为:字节流和字符流
3.按流的功能分为:节点流和处理流
2.IO流的四大抽象类
字符流:Reader(读数据),Writer(写数据)
字节流:InputStream(读数据), OutputStream(写数据)
3.InputStream的基本方法
int read() 读取一个字节以整数形式返回,如果返回-1说明已到输入流末尾。可能会抛出IOException异常
void close() 关闭释放内存资源。可能会抛出IOException异常
long skip(long n)跳过n个字节不读。可能会抛出IOException异常
4.OutputStream的基本方法
void write(int b)向输入流写入一个字节数据。可能会抛出IOException异常
void flush()将输出流中缓冲的数据全部写出到目的地。可能会抛出IOException异常
5.Writer的基本方法
OutputStreamReader
FileWriter:专门用于处理文件的字符写入流对象
void write(int c)向输入流写入一个字符数据。可能会抛出IOException异常
void write(String str)将一个字符串中的字符写入到输出流。可能会抛出IOException异常
void write(String str, int offset, int length)将一个字符串从offset开始的length个字符写入到输出流
void flush()将输出流中缓冲的数据全部写出到目的地。可能会抛出IOException异常
import java.io.FileWriter;
import java.io.IOException;
class Demo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("demo.txt");
fw.write("abcdec");
fw.flush();
fw.write("kkkk");
fw.close();
}
}
对于读取或者写入流对象的构造函数,以及读写方法,还有刷新关闭功能都会抛出
IOException
或其子类。
对于读取或者写入流对象的构造函数,以及读写方法,还有刷新关闭功能都会抛出
IOException
或其子类。
对于读取或者写入流对象的构造函数,以及读写方法,还有刷新关闭功能都会抛出IOException或其子类。
完美的异常处理方式
import java.io.FileWriter;
import java.io.IOException;
class Demo {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt");
fw.write("abcdec");
fw.flush();
fw.write("kkkk");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
if(fw != null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("close:"+e.toString());
}
}
}
}
当指定绝对路径时,定义目录分隔符有两种方式:
1,反斜线 但是一定要写两个。\\ new FileWriter("c:\\demo.txt");
2,斜线 / 写一个即可。 new FileWriter("c:/demo.txt");
6.Reader的基本方法
子类有:inputStreamReader
FileReader:专门用于处理文件的字符读取流对象
int read()读取一个字符以整数形式返回,如果返回-1说明已到输入流末尾。可能会抛出IOException异常
读取一个已有的文本文件,将文本数据打印出来。 一次读一个字符就打印出来,效率不高。
import java.io.FileReader;
import java.io.IOException;
class Demo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("demo.txt");
int ch = 0;
while((ch = fr.read()) != -1) {
System.out.print((char)ch);
}
fr.close();
}
}
读一个字符就存入字符数组里,读完1Kb再打印。
import java.io.FileReader;
import java.io.IOException;
class Demo {
public static void main(String[] args){
FileReader fr = null;
try {
fr = new FileReader("demo.txt");
char[] buf = new char[1024];
int len = 0;
while((len = fr.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.toString());
}finally {
if(fr != null) {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.toString());
}
}
}
}
}
6.字符流的缓冲区
缓冲区的出现提高了对流的操作效率。
原理:其实就是将数组进行封装。
对应的对象:
BufferedWriter:
特有方法: newLine():跨平台的换行符。
BufferedReader:
特有方法: readLine():一次读一行,到行标记时,将行标记之前的字符数据作为字符串返 回。当读到末尾时,返回null。
在使用缓冲区对象时,要明确,缓冲的存在是为了增强流的功能而存在, 所以在建立缓冲区对象时,要先有流对象存在。 其实缓冲内部就是在使用流对象的方法,只不过加入了数组对数据进行了临时存储。为 了提高操作数据的效率。
代码上的体现:
写入缓冲区对象。
建立缓冲区对象必须把流对象作为参数传递给缓冲区的构造函数。
BufferedWriter bufw = new BufferedWriter(new FileWriter("buf.txt"));
bufw.write("abce");//将数据写入到了缓冲区。
bufw.flush();//对缓冲区的数据进行刷新。将数据刷到目的地中。
bufw.close();//关闭缓冲区,其实关闭的是被包装在内部的流对象。
//按照行的形式取出数据。取出的每一个行数据不包含回车符。
while((line=bufr.readLine())!=null) {
System.out.println(line);
}
bufr.close();
通过缓冲区的形式,对文本文件进行拷贝。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
class Demo {
public static void main(String[] args) {
BufferedReader bufr = null;
BufferedWriter bufw = null;
try {
bufr = new BufferedReader(new FileReader("a.txt"));
bufw = new BufferedWriter(new FileWriter("b.txt"));
String line = null;
while((line = bufr.readLine()) != null) {
bufw.write(line);
bufw.newLine();
bufw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(bufw != null) {
try {
bufw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bufr != null) {
try {
bufr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
字节流:
抽象基类:InputStream,OutputStream。
字节流可以操作任何数据。
注意:字符流使用的数组是字符数组。char [] chs
字节流使用的数组是字节数组。byte [] bt
FileOutputStream fos = new FileOutputStream("a.txt");
fos.write("abcde");//直接将数据写入到了目的地。
fos.close();//只关闭资源。
FileInputStream fis = new FileInputStream("a.txt"); //fis.available();//获取关联的文件的字节数。
//如果文件体积不是很大。 //可以这样操作。
byte[] buf = new byte[fis.available()];//创建一个刚刚好的缓冲区。
//但是这有一个弊端,就是文件过大,大小超出jvm的内容空间时,会内存溢出。 fis.read(buf);
System.out.println(new String(buf));
需求:copy一个图片。
BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("1.jpg"));
BufferedOutputStream bufos = new BufferedOutputStream(new FileOutptStream("2.jpg"));
int by = 0;
while((by=bufis.read())!=-1) {
bufos.write(by);
}
bufos.close();
bufis.close();
目前学习的流对象: 字符流: FileReader. FileWriter. BufferedReader BufferedWriter.
字节流: FileInputStream FileOutputStream BufferedInputStream BufferedOutputStream
字节流的read()方法读取一个字节。为什么返回的不是byte类型,而是int类型呢?
因为read方法读到末尾时返回的是-1.
而在所操作的数据中的很容易出现连续多个1的情况,而连续读到8个1,就是-1. 导致读取会提前停止。
所以将读到的一个字节给提升为一个int类型的数值,但是只保留原字节,并在剩余二进制位补0.
具体操作是:byte&255 or byte&0xff
对于write方法,可以一次写入一个字节,但接收的是一个int类型数值。 只写入该int类型的数值的最低一个字节(8位)。
简单说:read方法对读到的数据进行提升。write对操作的数据进行转换。
用字节流复制mp3的几种方法比较:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyMp3Test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
copy_4();
}
// 千万不要用,效率没有!
public static void copy_4() throws IOException {
FileInputStream fis = new FileInputStream("c:\\0.mp3");
FileOutputStream fos = new FileOutputStream("c:\\4.mp3");
int ch = 0;
while ((ch = fis.read()) != -1) {
fos.write(ch);
}
fos.close();
fis.close();
}
// 不建议。
public static void copy_3() throws IOException {
FileInputStream fis = new FileInputStream("c:\\0.mp3");
FileOutputStream fos = new FileOutputStream("c:\\3.mp3");
byte[] buf = new byte[fis.available()];
fis.read(buf);
fos.write(buf);
fos.close();
fis.close();
}
public static void copy_2() throws IOException {
FileInputStream fis = new FileInputStream("c:\\0.mp3");
BufferedInputStream bufis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("c:\\2.mp3");
BufferedOutputStream bufos = new BufferedOutputStream(fos);
int ch = 0;
while ((ch = bufis.read()) != -1) {
bufos.write(ch);
}
bufos.close();
bufis.close();
}
public static void copy_1() throws IOException {
FileInputStream fis = new FileInputStream("c:\\0.mp3");
FileOutputStream fos = new FileOutputStream("c:\\1.mp3");
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
fis.close();
}
}
-------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------