1)InputStream、OutputStream(抽象类)
InputStream抽象了应用程序读取数据的方式
OutputStream抽象了应用程序写出数据的方式
2)EOF = End 读到-1就读到结尾
3)输入流基本方法
int b = in.read();读取一个字节无符号填充到int低八位.-1是 EOF
in.read(byte[] buf)
in.read(byte[] buf,int start,int size)
4)输出流基本方法out.write(int b) 写出一个byte到流,b的低8位
out.write(byte[] buf)将buf字节数组都写入到流
out.write(byte[] buf,int start,int size)
5) InputStream、OutputStream的实现类FileInputStream--->具体实现了在文件上读取数据
FileOutputStream 实现了向文件中写出byte数据的方法
6)BufferedInputStream&BufferedOutputStream
这两个流类位IO提供了带缓冲区的操作,一般打开文件进行写入
或读取操作时,都会加上缓冲,这种流模式提高了IO的性能
从应用程序中把输入放入文件,相当于将一缸水倒入到另一个缸中:
FileOutputStream--->write()方法相当于一滴一滴地把水“转移”过去
DataOutputStream-->writeXxx()方法会方便一些,相当于一瓢一瓢把水“转移”过去
BufferedOutputStream--->write方法更方便,相当于一飘一瓢先放入桶中,再从桶中倒入到另一个缸中,性能提高了
---------------------------------------------------------------------------------------------------------------------
FileInputStream常用构造方法:
public FileInputStream(String name);
FileInputStream(File file);
FileInputStream f=new FileInputStream("E:\\HQL 基础.txt");
FileInputStream f1=new FileInputStream(new File("E:\\HQL 基础.txt"));
-----------------------------------------
FileOutputStream同上
FileOutputStream f=new FileOutputStream("E:\\HQL 基础.txt");
FileOutputStream f1=new FileOutputStream(new File("E:\\HQL 基础.txt"));
package FileInputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class fileinputstream {
/**把文件作为字节流 然后输出
* @param args
* @throws IOException
* @throws IOException
* FileInputStream 为InputSteam(抽象类)的实现类 读取文件
*/
public void printHex() throws IOException{
FileInputStream f=new FileInputStream("E:\\HQL 基础.txt");//随意文件
int b;//相当于一个缓冲
int i = 0;
while((b=f.read())!=-1){
//如果为一位就第一位补0
if(b<0xf){
System.out.print("0");
}
System.out.print(Integer.toHexString(b)+" ");
if(i++%10==0){
System.out.println();
}
}
f.close();
}
/**
* 批量读取,对大文件而言效率高,也是我们最常用的读文件的方式
* @param fileName
* @throws IOException
*/
public static void printHexByByteArray(String fileName)throws IOException{
FileInputStream in =new FileInputStream(fileName);
byte[] buf=new byte[8*1024];//buf大小为8兆
/*int bytes=in.read(buf,0,buf.length);//读取文件字节放到buf中 然后返回的是字节数
int j=0;
for(int i=0;i<bytes;i++){
//byte类型8位 int类型32位 避免字节转换错误 需要把高24位清零
System.out.print(Integer.toHexString((buf[i] & 0xff))+" ");
if(j++%10==0){
System.out.println();
}
}*/
int b;
int j=0;
while((b=in.read(buf,0,buf.length))!=-1){
for(int i=0;i<b;i++){
System.out.print(Integer.toHexString(buf[i] & 0xff)+" ");
if(j++%10==0){
System.out.println();
}
}
}
in.close();
}
/*
* 文件拷贝,字节批量读取
*/
public static void copyfile(File srcFile,File destFile) throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
FileInputStream in=new FileInputStream(srcFile);
FileOutputStream out=new FileOutputStream(destFile);
byte [] buf=new byte[8*1024];//转换
int b=0;
while((b=in.read(buf,0,buf.length))!=-1){
out.write(buf,0, b);
out.flush();
}
in.close();
out.close();
System.out.println("复制成功");
}
/*
* 文件复制 字节批量读取 利用缓冲流
*/
public static void copyfilebuffered(File srcFile,File destFile) throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));
byte [] buf=new byte[8*1024];
int b=0;
while((b=bis.read(buf,0,buf.length))!=-1){
bos.write(buf,0, b);
bos.flush();
}
bis.close();
bos.close();
System.out.println("复制成功");
}
/**
* 单字节,不带缓冲进行文件拷贝 一个一个字节的拷贝
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFileByByte(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
int c ;
while((c = in.read())!=-1){
out.write(c);
out.flush();
}
in.close();
out.close();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//fileinputstream.printHexByByteArray("C:\\HQL 基础.txt");
File file1 =new File("F:\\BaiduYunDownload\\[www.dev1234.com]一头扎进J2SE视频教程\\[www.java1234.com]《一脚踹进J2SE》第三十一讲.mp4");
File file2=new File("F:\\BaiduYunDownload\\[www.dev1234.com]一头扎进J2SE视频教程\\5.mp4");
Long start=System.currentTimeMillis();
fileinputstream.copyfile(file1,file2);//335
// fileinputstream.copyfilebuffered(file1,file2);//747
//fileinputstream.copyFileByByte(file1,file2);//时间太长
Long end =System.currentTimeMillis();
System.out.println(end-start);
}
}