import java.io.*; /** * IO流(输入流、输出流) 字节流、字符流 1.字节流 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)FileInputStream--->具体实现了在文件上读取数据 6)FileOutputStream 实现了向文件中写出byte数据的方法 7)DataOutputStream/DataInputStream 对"流"功能的扩展,可以更加方面的读取int,long,字符等类型数据 DataOutputStream writeInt()/writeDouble()/writeUTF() 8)BufferedInputStream&BufferedOutputStream 这两个流类位IO提供了带缓冲区的操作,一般打开文件进行写入 或读取操作时,都会加上缓冲,这种流模式提高了IO的性能 从应用程序中把输入放入文件,相当于将一缸水倒入到另一个缸中: FileOutputStream--->write()方法相当于一滴一滴地把水“转移”过去 DataOutputStream-->writeXxx()方法会方便一些,相当于一瓢一瓢把水“转移”过去 BufferedOutputStream--->write方法更方便,相当于一飘一瓢先放入桶中,再从桶中倒入到另一个缸中,性能提高了 */ public class IOUtil { /** * 读取指定文件内容,按照16进制输出到控制台 * 并且每输出10个byte换行 * @param fileName * 单字节读取不适合大文件,大文件效率很低 */ public static void printHex(String fileName)throws IOException { //把文件作为字节流进行读操作 FileInputStream in = new FileInputStream(fileName); int b ; int i = 1; while((b = in.read())!=-1){ if(b <= 0xf){ //单位数前面补0 System.out.print("0"); } System.out.print(Integer.toHexString(b)+" "); if(i++%10==0){ System.out.println(); } } in.close(); } /** * 批量读取,对大文件而言效率高,也是我们最常用的读文件的方式 * @param fileName * @throws IOException */ public static void printHexByByteArray(String fileName)throws IOException{ FileInputStream in = new FileInputStream(fileName); byte[] buf = new byte[8 * 1024];//建立一个8k大小的数组 /*从in中批量读取字节,放入到buf这个字节数组中, 从第0个位置开始放,最多放buf.length个 返回的是读到的字节的个数*/ /*int bytes = in.read(buf,0,buf.length);//一次性读完,说明字节数组足够大 int j = 1; for(int i = 0; i < bytes;i++){ System.out.print(Integer.toHexString(buf[i] & 0xff)+" "); if(j++%10==0){ System.out.println(); } }*/ int bytes = 0; int j = 1; while((bytes = in.read(buf,0,buf.length))!=-1){ for(int i = 0 ; i < bytes;i++){ System.out.print(Integer.toHexString(buf[i] & 0xff)+" "); if(j++%10==0){//到10个就换行 System.out.println(); } } } in.close(); } /** * 文件拷贝,字节批量读取 * @param srcFile 从这个文件里读 * @param destFile 往这个文件里写 * @throws IOException */ 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 ; while((b = in.read(buf,0,buf.length))!=-1){ out.write(buf,0,b); out.flush();//最好加上 } in.close(); out.close(); } /** * 进行文件的拷贝,利用带缓冲的字节流 * @param srcFile 从这个文件里读 * @param destFile 往这个文件里写 * @throws IOException */ public static void copyFileByBuffer(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)); int c ; while((c = bis.read())!=-1){ bos.write(c); bos.flush();//刷新缓冲区 } bis.close(); bos.close(); } /** * 单字节,不带缓冲进行文件拷贝 * @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(); }}
===================================================================================================
import java.io.File; import java.io.IOException; /** * 测试 */ public class IOUtilTest1 { public static void main(String[] args) { try { IOUtil.printHex("C:\\Users\\hasee\\Desktop\\demo\\海参多少.txt"); IOUtil.printHexByByteArray("C:\\Users\\hasee\\Desktop\\demo\\海参多少.txt"); IOUtil.copyFile(new File("C:\\Users\\hasee\\Desktop\\demo\\文件一.txt"), new File("C:\\Users\\hasee\\Desktop\\demo\\文件二.txt")); IOUtil.copyFileByBuffer(new File("C:\\Users\\hasee\\Desktop\\demo\\文件三.txt"), new File("C:\\Users\\hasee\\Desktop\\demo\\文件四.txt")); } catch (IOException e) { e.printStackTrace(); } /*如果系统报这个错: java.io.FileNotFoundException: C:\Users\hasee\Desktop\demo\海参多少 (系统找不到指定的文件。) * 那就是拓展名的原因,解决办法:电脑工具栏-文件夹选项-查看-高级设置:-隐藏已知文件类型的扩展名(勾选去掉-应用-确定) * */ } }
Java IO流 ---字节流 案例分析
最新推荐文章于 2022-02-17 17:41:34 发布