字符读取流缓冲区
/*
字符读取流缓冲区
该缓冲区提供了一个一次读一行的方法readLine,方便于对文本数据的获取。
当返回null时,表示读到文件末尾。
readLine方法返回的时候只返回回车符之前的数据内容,并不返回回车符。
*/
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class BufferedReaderDemo { public static void main(String[] args) throws IOException { //创建一个读取流对象和文件相关联 FileReader fr = new FileReader("buf.txt"); //为了提高效率,加入缓冲技术。 //将字符读取流对象作为参数传递给缓冲区对象的构造函数。 BufferedReader bufr = new BufferedReader(fr); String line = null; while((line=bufr.readLine())!=null) { System.out.println(line); } bufr.close(); } }
/*
缓冲区的出现就是为了提高流的操作效率而出现的。
所以再创建缓冲区之前,必须要先有流对象。
该缓冲区中提供了一个跨平台的换行符
newLine();
*/
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class BufferedWriterDemo { public static void main(String[] args) throws IOException { //创建一个字符写入流对象。 FileWriter fw = new FileWriter("buf.txt"); //为了提高字符写入流效率,加入了缓冲技术。//原理:封装数组,变成对象 //只要将需要被提高效率的流对象作为参数,传递给缓冲区的构造函数即可。 BufferedWriter bufw = new BufferedWriter(fw); for(int x=1;x<5;x++) { bufw.write("abced"+x); bufw.newLine(); bufw.flush(); } // bufw.write("abcde"); // bufw.newLine(); //记住:只要用到缓冲区,就要记得刷新。 //bufw.flush(); //其实关闭缓冲区,就是关闭缓冲区中的流对象 bufw.close(); //newLine跨平台方法 //\r\n Windows // Linus \n } }
通过缓冲区复制一个.java文件。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class CopyTestByBuf { public static void main(String[] args) { BufferedReader bufr = null; BufferedWriter bufw = null; try { bufr = new BufferedReader(new FileReader("BufferedWriterDemo.java")); bufw = new BufferedWriter(new FileWriter("bufWriter_Copy.java")); String line = null; while((line=bufr.readLine())!=null) { bufw.write(line); bufw.newLine(); bufw.flush(); } } catch(IOException e) { throw new RuntimeException("读写失败"); } finally { try { if(bufr!=null) bufr.close(); } catch(IOException e) { throw new RuntimeException("读取关闭失败"); } try { if(bufw!=null) bufw.close(); } catch(IOException e) { throw new RuntimeException("写入关闭失败"); } } } }
/*
字符流:
FileReader
FileWriter
BufferedReader
BufferwdWriter
字节流:
InputStream OutputStream
需求:想要操作图片数据,这时就要用到字节流。
*/
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class FileStream_11 { public static void main(String[] args) throws IOException { readFile_3(); } public static void readFile_3() throws IOException { FileInputStream fis = new FileInputStream("fos.txt"); //int num = fis.available(); byte[] buf = new byte[fis.available()];//定义一个刚刚好的缓冲区,不用再循环了。 fis.read(buf); //System.out.println("num="+num); System.out.println(new String(buf)); fis.close(); } public static void readFile_2() throws IOException { FileInputStream fis = new FileInputStream("fos.txt"); byte[] buf = new byte[1024]; int len = 0; while((len=fis.read(buf))!=-1) { System.out.println(new String(buf,0,len)); } fis.close(); } public static void readFile_1() throws IOException { FileInputStream fis = new FileInputStream("fos.txt"); int ch = 0; while((ch=fis.read())!=-1) { System.out.println((char)ch); } fis.close(); } public static void writeFile() throws IOException { FileOutputStream fos = new FileOutputStream("fos.txt"); fos.write("abcde".getBytes()); //没刷新, 字符流临时存储字节,底层用字节缓冲区,所以需要刷新 fos.close(); } }
字节流
/*
复制一个图片
思路:
1、用字节读取流对象和图片关联
2、用字节写入流对象创建一个图片文件,用于存储获取到的图片数据。
3、通过循环读写,完成数据的存储。
4、关闭资源。
*/
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class CopyPic_12 { public static void main(String[] args) { FileOutputStream fos = null; FileInputStream fis = null; try { fos = new FileOutputStream("2.tmp"); fis = new FileInputStream("1.tmp"); 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("写入关闭失败"); } } } }
/*
演示mp3的复制,通过缓冲区。
BufferedOutputStream
BufferedInputStream
*/
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class CopyMP3_13 { public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); copy_2(); long end = System.currentTimeMillis(); System.out.println((end-start)+"毫秒"); } //自定义一个缓冲区 public static void copy_2() throws IOException { MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("0.mp3")); BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("1.mp3")); int by = 0; //System.out.println("第一个字节:"+bufis.myRead()); while((by=bufis.myRead())!=-1) { bufos.write(by);//write把最低八位写入,强转 } bufos.close(); bufis.myClose(); } //通过字节流的缓冲区完成复制。 public static void copy_1() throws IOException { BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("0.mp3")); BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("1.mp3")); int by = 0; while((by=bufis.read())!=-1) { bufos.write(by); } bufos.close(); bufis.close(); } }
异常日志文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class ExceptionInfo_21 { public static void main(String[] args) throws IOException { try { int[] arr = new int[2]; System.out.println(arr[3]); } catch(Exception e) { try { Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String s = sdf.format(d); PrintStream ps = new PrintStream("exception.log"); ps.println(s); System.setOut(ps); } catch(IOException ex) { throw new RuntimeException("日志文件创建失败"); } e.printStackTrace(System.out); } } } //网络上有使用:log4j 简单方便
字符流:
FileReader
FileWriter
BufferedReader
BufferwdWriter
字节流:
FileInputStream
FileOutputStream
BufferedInputStream
BufferedOutputStream
readLine方法是字符流BufferedReader类中的方法。
//记住!!!键盘录入最常见写法。
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class TransStreamDemo_15 { public static void main(String[] args) throws IOException { //获取键盘录入对象 // InputStream in = System.in; //将字节流对象转成字符流对象。使用转换流。InputStreamReader. // InputStreamReader isr = new InputStreamReader(in); //为了提高效率,将字符流进行缓冲区技术高效操作,使用BufferedReader // BufferedReader bufr = new BufferedReader(isr); //三句话变成一句话 //记住!!!键盘录入最常见写法。 BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); // OutputStream out = System.out; // OutputStreamWriter osw = new OutputStreamWriter(out); // BufferedWriter bufw = new BufferedWriter(osw); BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out)); String line = null; while((line=bufr.readLine())!=null) { if("over".equals(line)) break; //System.out.println(line.toUpperCase()); bufw.write(line.toUpperCase());//也是输出语句。 bufw.newLine(); bufw.flush(); } bufr.close(); } }
欢迎各路侠客多多指教^_^