概述:摘录下统计的常用的字节流操作情况,很全面,值得看看!
一.IO流(字节流File读写文件)
1 import java.io.*; 2 public class FileStream { 3 4 public static void main(String[] args) throws IOException{ 5 6 } 7 8 public static void readFile_1() throws IOException{ 9 FileInputStream fis=new FileInputStream("fos.txt"); 10 int ch=0; 11 while((ch=fis.read())!=-1){ 12 System.out.println((char)ch); 13 } 14 fis.close(); 15 } 16 17 public static void readFile_2() throws IOException{ 18 FileInputStream fis=new FileInputStream("fos.txt"); 19 byte[] buf=new byte[1024]; 20 int len=0; 21 while((len=fis.read(buf))!=-1){ 22 System.out.println(new String(buf,0,len)); 23 } 24 fis.close(); 25 } 26 27 public static void readFile_3() throws IOException{ 28 FileInputStream fis=new FileInputStream("fos.txt"); 29 byte[] buf=new byte[fis.available()]; 30 fis.read(buf); 31 System.out.println(new String(buf)); 32 } 33 34 public static void writeFile() throws IOException{ 35 FileOutputStream fos=new FileOutputStream("fos.txt"); 36 fos.write("abcde".getBytes()); 37 fos.close(); 38 } 39 40 }
二.IO流(拷贝图片)
1 import java.io.*; 2 public class CopyPic { 3 4 public static void main(String[] args) { 5 FileOutputStream fos=null; 6 FileInputStream fis=null; 7 try { 8 fos=new FileOutputStream("c:\\2.bmp"); 9 fis=new FileInputStream("c:\\1.bmp"); 10 byte[] buf=new byte[1024]; 11 int len=0; 12 while((len=fis.read(buf))!=-1){ 13 fos.write(buf, 0, len); 14 } 15 } catch (IOException e) { 16 // TODO: handle exception 17 throw new RuntimeException("复制文件失败!"); 18 } finally{ 19 try { 20 if(fis!=null){ 21 fis.close(); 22 } 23 } catch (IOException e2) { 24 // TODO: handle exception 25 throw new RuntimeException("读取关闭失败!"); 26 } 27 try { 28 if(fos!=null){ 29 fos.close(); 30 } 31 } catch (IOException e2) { 32 // TODO: handle exception 33 throw new RuntimeException("写入关闭失败!"); 34 } 35 } 36 } 37 38 }
三.IO流(字节流的缓冲区)
1 import java.io.*; 2 public class CopyMp3 { 3 4 public static void main(String[] args) { 5 6 } 7 8 public static void copy_1() throws Exception{ 9 BufferedInputStream bufis=new BufferedInputStream(new FileInputStream("c:\\0.mp3")); 10 BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("c:\\1.mp3")); 11 int by=0; 12 while((by=bufis.read())!=-1){ 13 bufos.write(by); 14 } 15 bufos.close(); 16 bufis.close(); 17 } 18 19 }
四.IO流(自定义字节流的缓冲区)
1 import java.io.*; 2 public class MyBufferedInputStream { 3 4 private InputStream in; 5 6 private byte[] buf=new byte[1024]; 7 8 private int pos=0,count=0; 9 10 MyBufferedInputStream(InputStream in) { 11 this.in=in; 12 } 13 14 //一次读一个字节,从缓冲区(字节数组)获取 15 public int myRead() throws IOException{ 16 //通过in对象读取硬盘上的数据,并存储到buf中 17 if(count==0){ 18 count=in.read(buf); 19 if(count<0){ 20 return -1; 21 } 22 pos=0; 23 byte b=buf[pos]; 24 count--; 25 pos++; 26 return b&255; 27 }else if(count>0){ 28 byte b=buf[pos]; 29 count--; 30 pos++; 31 return b&255; 32 } 33 return -1; 34 } 35 36 public void myClose() throws IOException{ 37 in.close(); 38 } 39 40 }
五.IO流(读取键盘录入)
1 import java.io.*; 2 public class ReadIn { 3 4 public static void main(String[] args) throws IOException{ 5 InputStream in=System.in; 6 StringBuilder sb=new StringBuilder(); 7 while(true){ 8 int ch=in.read(); 9 if(ch==‘\r‘) 10 continue; 11 if(ch==‘\n‘){ 12 String s=sb.toString(); 13 if("over".equalsIgnoreCase(s)){ 14 break; 15 } 16 System.out.println(s.toUpperCase()); 17 sb.delete(0, sb.length()); 18 } 19 else 20 sb.append((char)ch); 21 } 22 } 23 }
六.IO流(读取,写入转换流)
1 import java.io.*; 2 public class TransStreamDemo { 3 4 public static void main(String[] args) throws IOException{ 5 //InputStream in=System.in; 6 7 //将字节流转换成字符流对象 8 //InputStreamReader isr=new InputStreamReader(in); 9 //使用缓冲区提高读取效率 10 //BufferedReader bufr=new BufferedReader(isr); 11 //键盘录入常用写法 12 BufferedReader bufr= 13 new BufferedReader(new InputStreamReader(System.in)); 14 15 //OutputStream out=System.out; 16 //OutputStreamWriter osw=new OutputStreamWriter(out); 17 //BufferedWriter bufw=new BufferedWriter(osw); 18 BufferedWriter bufw= 19 new BufferedWriter(new OutputStreamWriter(System.out)); 20 21 String line=null; 22 while((line=bufr.readLine())!=null){ 23 if("over".equalsIgnoreCase(line)) 24 break; 25 //System.out.println(line.toUpperCase()); 26 bufw.write(line); 27 bufw.newLine(); 28 bufw.flush(); 29 } 30 bufr.close(); 31 } 32 }
七.IO流(流操作规律1)
- 通过三个明确来完成
1.1 明确源和目的
源:输入流:InputStream,Reader
目的地:输出流:OutputStream,Writer
1.2 操作的数据是否是纯文本
是:字符流
否:字节流
1.3 当体系明确后,再明确要使用哪个具体对象
通过设备来进行区分:
源设备:内存,硬盘,键盘
目的地设备:内存,硬盘,控制台
1.4 是否需要提高读取效率
-----------------技术改变生活,知识改变命运!
时间: 2024-10-19 00:29:24