---------------------- android培训、java培训、期待与您交流! ----------------------
IO流用来处理设备之间的数据传输
流按操作数据分为两种:字节流与字符流。
流按流向分为:输入流、输出流。
字节流的抽象基类:
Inputstream、OutputStream
字符流的抽象基类:
Reader、Writer
注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。如FileInputSteam、FileReader。
利用字符数组通过FileReader类实现读取文件内容的操作
import java.io.*;
public class Test_FileReader1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Test_FileReader1.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileReader fr = null;
try{
fr = new FileReader("demo.txt");
char[] buf = new char[1024];
int num = 0 ;
while((num=fr.read(buf))!=-1){
System.out.print(new String(buf,0,num));
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(fr!=null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void show () throws Exception{
FileReader f = new FileReader("demo.txt");
char []cha = new char[4];
int d = f.read(cha);
System.out.println(new String(cha)+ "...." + d);
int d1 = f.read(cha);
System.out.println(new String(cha,0,d1)+ "...." + d1);
int d2 = f.read(cha);
System.out.println(new String(cha,0,d2)+ "...." + d2);//这里会出现StringIndexOutOfBoundsExcepion异常
f.close();
}
}
缓冲区的出现是为了题号流的操作效率而出现的。
所以子啊创建缓冲区前,必须要有流对象。
该缓冲区中提供了一个跨平台的换行符。
newLine();
通过缓冲区复制一个.java 文件
该缓冲区提供了一个一次赌一行的方法 readLine,方便于对文本数据的获取。
当返回null时。表示独到文件末尾
readLine方法返回的时候只返回回车符之前的数据内容,
并不返回回车符。
import java.io.*;;
public class Copy_Buf_Text {
public static void main(String[] args) {
BufferedReader in = null;
BufferedWriter out = null;
try{
in = new BufferedReader(new FileReader("buf.txt"));
out = new BufferedWriter(new FileWriter("buf1.java"));
String line = null;
while((line=in.readLine())!= null){// 读入中转站,也就是 String line ;
//写入到目的地文件中
out.write(line);
out.newLine();
out.flush();
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(in!=null)
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(out!= null)
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
通过字节流缓冲区复制Mp3
import java.io.*;
public class CopMp3 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
long start = (int)System.currentTimeMillis();
copy_3();
long end = System.currentTimeMillis();
System.out.println((end-start)+"毫秒");
}
//通过字节流的缓冲区完成复制。
public static void copy_1() throws IOException{
BufferedOutputStream buos = new BufferedOutputStream(new FileOutputStream("c:\\3.mp3"));
BufferedInputStream buis = new BufferedInputStream(new FileInputStream("c:\\1.mp3"));//数据源
byte buf[] = new byte[1024];
int len = 0 ;
while((len = buis.read(buf))!=-1){
buos.write(buf,0,len);
}
buos.close();
buis.close();
}
public static void copy_2() throws IOException{
BufferedOutputStream buos = new BufferedOutputStream(new FileOutputStream("c:\\2.mp3"));
BufferedInputStream buis = new BufferedInputStream(new FileInputStream("c:\\1.mp3"));//数据源
int len = 0 ;
while((len = buis.read())!=-1){
buos.write(len);
}
buos.close();
buis.close();
}
}
---------------------- android培训、java培训、期待与您交流! ---------------------
-详细请查看:http://edu.csdn.net/heima