---------------------- android培训、java培训、期待与您交流! ----------------------
字节流File读写操作
InputStream OutputStream
public class FileInputStream
extends InputStream
FileInputStream 从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。
FileInputStream 用于读取诸如图像数据之类的原始字节流。
public class FileOutputStream
文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。文件是否可用或能否可以被创建取决于基础平台。特别是某些平台一次只允许一个 FileOutputStream(或其他文件写入对象)打开文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造方法将失败。extends OutputStream
FileOutputStream 用于写入诸如图像数据之类的原始字节的流。import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) { InputStream in = System.in; InputStreamReader insr = new InputStreamReader(in); StringBuilder sb = new StringBuilder(); try { int i =0; while(true){ i = insr.read(); if(i == '\r') continue; if(i == '\n'){ String str = sb.toString(); sb.delete(0, sb.length()); if(str.equals("over")) break; System.out.println(str.toUpperCase()); }else sb.append((char)i); } } catch (IOException e) { throw new RuntimeException("读取异常!"); } } }
拷贝图片
public class BufferedInputStream
extends FilterInputStreamBufferedInputStream
为另一个输入流添加一些功能,即缓冲输入以及支持 mark 和 reset 方法的能力。在创建 BufferedInputStream 时,会创建一个内部缓冲区数组。在读取或跳过流中的字节时,可根据需要从包含的输入流再次填充该内部缓冲区,一次填充多个字节。mark 操作记录输入流中的某个点,reset 操作使得在从包含的输入流中获取新字节之前,再次读取自最后一次 mark 操作后读取的所有字节。public class BufferedOutputStream
extends FilterOutputStream
该类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 复制一个图片 */ public class FileStreamDemo2 { public static void main(String[] args) { FileInputStream fins = null; FileOutputStream fouts = null; try { fins = new FileInputStream("d:\\test.jpg"); fouts = new FileOutputStream("e:\\target.jpg"); byte[] value = new byte[1024]; int len = 0; while((len = fins.read(value)) != -1){ fouts.write(value,0,len); } } catch (Exception e) { throw new RuntimeException("文件拷贝异常"); }finally{ if(fins != null){ try { fins.close(); } catch (IOException e) { throw new RuntimeException("读取流关闭异常"); } } if(fouts != null){ try { fouts.close(); } catch (IOException e) { throw new RuntimeException("写入流关闭异常"); } } } } }
小测试:
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 拷贝mp3文件到新的目录 * BufferedInputStream * BufferedOutputStream */ public class CopyMp3 { public static void main(String[] args) { long startTime = System.currentTimeMillis(); copyMp3(); long endTime = System.currentTimeMillis(); System.out.println((endTime - startTime) + "毫秒"); } protected static void copyMp3(){ BufferedInputStream bufins = null; BufferedOutputStream bufouts = null; try { bufins = new BufferedInputStream(new FileInputStream("D:\\test.mp3")); bufouts = new BufferedOutputStream(new FileOutputStream("E:\\target.mp3")); int temp = 0; while((temp = bufins.read()) != -1){ bufouts.write(temp); } } catch (Exception e) { throw new RuntimeException("文件拷贝异常"); }finally{ if(bufins != null){ try { bufins.close(); } catch (IOException e) { throw new RuntimeException("文件读取流关闭异常"); } } if(bufouts != null){ try { bufouts.close(); } catch (IOException e) { throw new RuntimeException("文件写入流关闭异常"); } } } } }
自定义字节流缓冲区
1的二进制表示00000001-1的二进制表示是1的加11111111000000001+------------11111111所以-1的二进制表示是11111111而read()方法读文件的时候读到的都是二进制数据,一字节是8位,当读到二进制八位数恰好都是1的话,read()方法返回的就是-1,就和rea()读到文件末尾返回值相同,读取就会结束。为了解决这个问题返回值由字节型转化为整数型,而整数型是32位,前面的24位默认补位是1,这样如果我们补0那么就可以保留原来的数据不变又可以避免-1的出现,我们可以让原数据和255取与11111111 11111111 11111111 1111111100000000 00000000 00000000 11111111 &--------------------------------------------------
00000000 00000000 00000000 11111111而Write()方法做强转动作,只写有效的后8位数据。import java.io.FileReader; import java.io.IOException; public class MyBufferReaderTest { public static void main(String[] args) { MybufferedReader mybuf = null; try { mybuf = new MybufferedReader(new FileReader("Demo2.txt")); String temp = null; while((temp = mybuf.myReadLine()) != null){ System.out.println(temp); } } catch (Exception e) { throw new RuntimeException("文件读异常"); }finally{ try { if(mybuf != null) mybuf.MyClose(); } catch (IOException e) { throw new RuntimeException("流关闭异常"); } } } } //自定义BufferedReader class MybufferedReader { FileReader fr; MybufferedReader(FileReader fr){ this.fr = fr; } String myReadLine()throws IOException{ //sb是局部变量调用一次myReadLine方法就重新new一次sb StringBuilder sb = new StringBuilder(); int value = 0; while((value = fr.read()) != -1){ if(value == '\r') continue; if(value == '\n') return sb.toString(); else sb.append((char)value); } if(sb.length() != 0) return sb.toString(); return null; } void MyClose()throws IOException{ fr.close(); } }
---------------------- android培训、java培训、期待与您交流! ----------------------