import java.io.*; class FileInputStreamDemo { public static void main(String[] args)throws IOException { //读取该文件中的数据 FileInputStream fis = new FileInputStream("f:\\myfile\\fos.txt"); int b = 0; while((b = fis.read())!=-1) { System.out.print((char)b); } //此处不适用于刷新,而是用于关闭资源 fis.close(); } }
import java.io.*; class FileOutputStreamDemo { public static void main(String[] args)throws IOException { //创建字节流文件并写入数据 FileOutputStream fos = new FileOutputStream("f:\\myfile\\fos.txt"); String str = "abcde"; byte b[] = str.getBytes(); fos.write(b); //此处不适用于刷新,而是用于关闭资源 fos.close(); } }
import java.io.*; class FileStream { public static void main(String[] args)throws IOException { //WriteFile(); ReadFile1(); ReadFile2(); ReadFile3(); } public static void WriteFile()throws IOException { //创建字节流文件并写入数据 FileOutputStream fos = new FileOutputStream("f:\\myfile\\fs.txt"); String str = "xyzabcd123"; byte b[] = str.getBytes(); fos.write(b); //此处不适用于刷新,而是用于关闭资源 fos.close(); } //方法一:一个一个字节的读取 public static void ReadFile1()throws IOException { //读取该文件中的数据 FileInputStream fis = new FileInputStream("f:\\myfile\\fs.txt"); int b = 0; while((b = fis.read())!=-1) { System.out.println((char)b); } //此处不适用于刷新,而是用于关闭资源 fis.close(); } //方法二:先指定长度的数组,再将数据读读入数组,最后从数组中读出数据内容。 public static void ReadFile2()throws IOException { //读取该文件中的数据 FileInputStream fis = new FileInputStream("f:\\myfile\\fs.txt"); byte[] buf = new byte[1024]; int num = 0; while((num = fis.read(buf))!=-1) { System.out.println(new String(buf,0,num)); } //此处不适用于刷新,而是用于关闭资源 fis.close(); } //方法三:通过available()方法可以获取文件中数据的总个数,以此个数设置为数组长度刚刚好,剩下步骤与方法二相同。 public static void ReadFile3()throws IOException { //读取该文件中的数据 FileInputStream fis = new FileInputStream("f:\\myfile\\fs.txt"); byte[] buf = new byte[fis.available()]; fis.read(buf); System.out.println(new String(buf)); //此处不适用于刷新,而是用于关闭资源 fis.close(); } }
import java.io.*; class CopyPicture { public static void main(String[] args) { FileOutputStream fos = null; FileInputStream fis = null; try { fos = new FileOutputStream("F:\\myfile\\2.jpg"); fis = new FileInputStream("F:\\myfile\\1.jpg"); byte[] buf = new byte[1024]; int len = 0; while((len = fis.read(buf))!=-1) { fos.write(buf,0,len); } } catch(IOException e) { throw new RuntimeException("文件复制失败!"); } finally { try { if(fis!=null) fis.close(); } catch(IOException e) { throw new RuntimeException("读取流关闭异常!"); } try { if(fos!=null) fos.close(); } catch(IOException e) { throw new RuntimeException("写入流关闭异常!"); } } } }
import java.io.*; class CopyMp3 { public static void main(String[] args) { long start = System.currentTimeMillis(); copy(); long end = System.currentTimeMillis(); System.out.println((end-start)+"毫秒"); } //通过字节流缓冲区完成复制 public static void copy() { BufferedInputStream bufis = null; BufferedOutputStream bufos = null; try { bufis = new BufferedInputStream(new FileInputStream("F:\\myfile\\b01.mp3")); bufos = new BufferedOutputStream(new FileOutputStream("F:\\myfile\\b02.mp3")); int by = 0; while((by = bufis.read())!=-1) { bufos.write(by); } } catch(IOException e) { throw new RuntimeException("文件复制失败!"); } finally { try { if(bufis!=null) bufis.close(); } catch(IOException e) { throw new RuntimeException("读取流关闭异常!"); } try { if(bufos!=null) bufos.close(); } catch(IOException e) { throw new RuntimeException("写入流关闭异常!"); } } } }
import java.io.*; class CopyAvi { public static void main(String[] args) { FileOutputStream fos = null; FileInputStream fis = null; try { fos = new FileOutputStream("F:\\myfile\\a02.avi"); fis = new FileInputStream("F:\\myfile\\a01.avi"); byte[] buf = new byte[4096]; int len = 0; while((len = fis.read(buf))!=-1) { fos.write(buf,0,len); } } catch(IOException e) { throw new RuntimeException("文件复制失败!"); } finally { try { if(fis!=null) fis.close(); } catch(IOException e) { throw new RuntimeException("读取流关闭异常!"); } try { if(fos!=null) fos.close(); } catch(IOException e) { throw new RuntimeException("写入流关闭异常!"); } } } }
import java.io.*; class MyBufferedStream { private InputStream in; private byte[] buf = new byte[1024]; private int pos = 0,count = 0; MyBufferedStream(InputStream in) { this.in = in; } //一次读一个字节,从缓冲区(字节数组)获取 public int myread() throws IOException { //通过in对象来读取硬盘上的字节数据,存放在字节数组中。 if(count==0) { count = in.read(buf); if(count<0) return -1; pos=0; byte b = buf[pos]; count--; pos++; return b&255;//byte字节提升为整型int型 } else if(count>0) { byte b = buf[pos]; count--; pos++; return b&0xff;//byte字节提升为整型int型+-++++ } return -1; } public void myclose()throws IOException { in.close(); } } class MyBufferedStreamDemo { public static void main(String[] args)throws IOException { long start = System.currentTimeMillis(); copymp3(); long end = System.currentTimeMillis(); System.out.println((end-start)+"毫秒"); } public static void copymp3()throws IOException { MyBufferedStream mys = new MyBufferedStream(new FileInputStream("F:\\myfile\\b01.mp3")); BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("F:\\myfile\\b03.mp3")); int num = 0; //System.out.println("第一个字节是:"+mys.myread()); while((mys.myread())!=-1)//要区别开-1是数据读完时的-1,还是读取的字节数据内容为-1,所以要用&操作. { bufos.write(num); } mys.myclose(); bufos.close(); } }
Java:IO流之字节流InputStream、OutputStream详解
原文:http://www.cnblogs.com/XYQ-208910/p/4917818.html