---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------

缓冲区的出现时为了提高流的操作而出现的

1.建立缓冲区之前,必须要先有流对象;
FileWriter 变量 = new FileWriter("文件名");

2.为了提高字符写入效率,需要加入缓冲技术;
BufferedWriter变量 = new BuffererdWriter(流对象的变量名);
变量.write(\r\n);//其中“\r\n”是用于换行的;

3.数据读写完成之后记得刷新关闭;
变量.flush();
变量.close();

4.代码示例:19_01

5.读取流;
FileReader 变量 = new FileReader("文件名");

6.将读取流就如到缓冲区;
BufferedReader 变量 = new BufferedReader("读取流对象的变量名");

7.缓冲区提供了一个一次读一行的方法readerLine,当返回为null时表示已经读到文件末尾;

1)readerLine 方法的原理:无论是读一行,还是读取多个字符,其实最终都是在硬盘中一个一个读取,
所以最终使用的还是read方法一次都一个的方法;

8.关闭数据流;
变量.close();

9.代码示例:19_02

10.创建一个功能类似于readLine功能的函数;代码示例;19_03
package earth;
import java.io.*;
public class Write_Read_1902 {
public static void main(String[] args)throws IOException{
FileReader fr = new FileReader("ok02.txt");
MyReadline br = new MyReadline(fr);
String num1 = null;
while((num1 = br.myRead()) != null ){
System.out.println(num1);
}

br.MyClose();
}

}
//创建一个类似于readLine功能的类;
class MyReadline{

private FileReader fw1;

public MyReadline(FileReader fw1) {
this.fw1 = fw1;
}

public String myRead()throws IOException{

//构建一个存储容器;
StringBuilder sb = new StringBuilder();
int num = 0;
while((num = fw1.read()) != -1){
if(num == '\r'){
continue;
}
if(num == '\n'){
return sb.toString();
}else{
sb.append((char)num);
}
}
if(sb.length() != 0){
return sb.toString(); 
}
return null;
}
public void MyClose() throws IOException{
fw1.close();
}
}

11.装饰设计模式原理;当想要对已有的对象进行功能增强时,可以定义类,将已有对象传入。基于已有的功能,并提供加强功能。

12.MyReader:专门用读取数据的类;
a.MyTextReader
b.MyMediaReader

13.LineNumberReader用于读取行号;

代码示例;
FileReader fr = new FileReader(文件名);
LineNumberReader lnr = new LineNumberReader(fr);
String num = null;
while((num = lnr.Linread()) = -1){
System.out.print(lnr.getLineNumberReader() + num);
}
lnr.close();

14.Read的使用,并显示行号;

代码示例:
package earth;
import java.io.*; 
public class Writer_Reader_1903{
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("ok02.txt");
Read01 re = new Read01(fr);
String num = null;
while((num = re.MyReader()) != null){
System.out.println( ":"+ num);
}
}
}

//装饰函数;
class Read01{

private Reader r;//Reader是FileReader的父类;
private int num01;

public Read01(Reader r){
this.r =r;
}

public void setNum01(int num01){
this.num01 = num01;
}
public int getNum01(){
return num01;
}

public String MyReader() throws IOException{
num01++; //进行计数;
StringBuilder sb = new StringBuilder();
int num = 0;
while((num = r.read()) != -1){
if(num == '\r'){
continue;
}
if(num == '\n'){
return sb.toString();
}else{
sb.append((char)num);
}
}
if(sb.length() != 0){
return sb.toString(); 
}
return null;
}

//关闭数据流;
public void Myclose() throws IOException{
r.close();
}

}

15.字节流:

a.关于FileInputStream与FileOutoutStream代码示例:

package earth;
import java.io.*;
public class File_I_0_01 {
public static void main(String[] args) throws IOException{
Filo();
Fili();
}
//使用FileOutputStream
public static void Filo() throws IOException{
FileOutputStream fs = new FileOutputStream("ok03.txt"); //创建一个字节流的文件;
fs.write("asdfg".getBytes());//向文件中写如数据;
fs.close(); //关闭数据流;
}

public static void Fili() throws IOException{
FileInputStream fi = new FileInputStream("ok03.txt");

byte[] lan = new byte[1024];
int num = 0;
while((num = fi.read(lan)) != -1){
System.out.println(new String(lan, 0, num));
}

}

}

16.关于available的使用,示例代码;

package earth;
import java.io.*;
public class File_I_0_01 {
public static void main(String[] args) throws IOException{
Filo();
Fili();
tst();
}
//使用FileOutputStream
public static void Filo() throws IOException{
FileOutputStream fs = new FileOutputStream("ok03.txt"); //创建一个字节流的文件;
fs.write("asdfg".getBytes());//向文件中写如数据;
fs.close(); //关闭数据流;
}

public static void Fili() throws IOException{
FileInputStream fi = new FileInputStream("ok03.txt");

byte[] lan = new byte[1024];
int num = 0;
while((num = fi.read(lan)) != -1){
System.out.println(new String(lan, 0, num));
}

}

//测试available的使用;
public static void tst() throws IOException{
FileInputStream fis = new FileInputStream("ok03.txt");
int num = fis.available();
System.out.println(num);
}

}

17.通过上面的测试,我们知道available是用来统计字节数,
   所以我们可以用它来定义读取文件内容中,数组的大小,
   同时我们阅读文件时不要通过循环来依次读取;

package earth;
import java.io.*;
public class File_I_0_01 {
public static void main(String[] args) throws IOException{
Filo();
Fili();
tst();
}
//使用FileOutputStream
public static void Filo() throws IOException{
FileOutputStream fs = new FileOutputStream("ok03.txt"); //创建一个字节流的文件;
fs.write("asdfg".getBytes());//向文件中写如数据;
fs.close(); //关闭数据流;
}

public static void Fili() throws IOException{
FileInputStream fi = new FileInputStream("ok03.txt");

byte[] lan = new byte[1024];
int num = 0;
while((num = fi.read(lan)) != -1){
System.out.println(new String(lan, 0, num));
}

}

//同过available来读取数组中存储的数据;
public static void tst() throws IOException{
FileInputStream fis = new FileInputStream("ok03.txt");
int num = fis.available();
System.out.println(num);
}

}

18.字符流不能拷贝媒体文件,容易出现错误;

19.用字节流拷贝媒体文件,示例代码;

package earth;
import java.io.*;
public class Pc_copy {
public static void main(String[] args){

//用try..catch来抛出异常;
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("hongmi.png");
fos = new FileOutputStream("xiaomi.png");

byte[] by = new byte[1024];
int num = 0;
while ((num = fis.read(by)) != -1){
fos.write(by,0,num);
}
}
catch(IOException e){
throw new RuntimeException("读取失败");
}
finally{
try{
if(fos != null){
fos.close();
}
}
catch(IOException e){
throw new RuntimeException("00");
}
try{
if(fis != null){
fis.close();
}
}
catch(IOException e){
throw new RuntimeException("");
}
}

}
}

20.“1”的二进制值为 000-001;
而“-1”的而进制值为“1”的二进制相反数再加1,即:111-110 + 1 = 111-111;

21.对音频文件的复制;
package earth;
import java.io.*;
import java.nio.CharBuffer;
public class Music_Copy {
public static void main(String[] args){

}
}

//手写数据的复制;
class Ms_copy{
private Reader r;

private byte[] by = new byte[1024 * 4];
private int post = 0, count = 0;
public Ms_copy(Reader r){
this.r = r;

}
public int MyRead() throws IOException{
if(count == 0){
count = r.read(by);
if(count < 0){
return -1;
}
post = 0;
byte b = by[post];
count--;
post++;
return b & 255;
}
else if(count > 0){
byte b = by[post];
count--;
post++;
return b & 0*ff;
}
}
//自定义关闭数据流;
public void Myclose() throws IOException{
r.close();
}
}

22.字符流:FileReader,FileWriter,BufferedReader,BufferedWriter

23.字节流:FileInputStream,FileOutputStream,BufferedInputStream,BufferedOutputStream

24.键盘录入:InputStream in = System.in;

25.示例代码:
package earth;
import java.io.*;
public class Key_In {
public static void main(String[] args) throws IOException{
InputStream is = System.in;//从键盘录入数值;
StringBuilder sb = new StringBuilder();//随意字符长度;

while(true){

int ch = is.read();//将读取的数据进行存储;
if(ch == '\r'){
continue;
}
if(ch == '\n'){
String s = sb.toString();
if("over".equals(s)){
break;
}
System.out.println(s.toUpperCase());
sb.delete(0, sb.length());

}else{
sb.append((char)ch);
}
}

}
}

26.字节流与字符流转换的桥梁:InputStreamReader;

27.示例代码:
package earth;
import java.io.*;
public class Key_In01 {
public static void main(String[] args) throws IOException{
//首先从键盘获取字节流数据;
InputStream is = System.in;

//将字节流通过转换桥梁InputStreamReader来进行转换;
InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String be = null;
while((be = br.readLine()) != null){
if("over".equals(be)){
break;
}
System.out.println(be);

}
br.close();

}
}

28.equals与“==”的区别;
equals是比较两个变量引用的对象是否相同,比较的是对象的内容;
“==”比较的是两个对象的地址;
29.“toUpperCase”是将字母转化成大写;

30.从键盘读取数据优化:BuffreredReader br = new BufferedReader(new InputStreamReader(System.in));

31.输入流:InputStream, Reader;
   输出流:OutputStream, Writer;

32.改变设备输入源:System.setIn(“输入文件名”);

33.改变设备的输出源:System.setOut("输出文件名");

34.怎么讲将系统运行是出现的错误信息输出到文件中;
代码示例;
package earth;
import java.io.*;
public class Syserr01 {
public static void main(String[] args) throws IOException{
try{
int[] a = new int[3];
System.out.println(a[4]);
}
catch(Exception e){
e.printStackTrace(new PrintStream("ok04.txt"));
}
}
}

35.将错误的信息输出到文件中保存,并附上保存的时间;
示例代码:
package earth;
import java.io.*;
import java.util.*;
import java.text.*;

public class Syserr01 {
public static void main(String[] args) throws IOException{
try{
int[] a = new int[3];
System.out.println(a[4]);
}
catch(Exception e){
//让错误信息输出时带上时间;
try{
Date d = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ti = sf.format(d);

PrintStream ps = new PrintStream("ok04.log");
ps.println(ti);
System.setOut(ps);

}
catch(IOException ex){
throw new RuntimeException("日志文件创建失败");
}
e.printStackTrace(System.out);
}
}
}

---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------

黑马程序员--IO流(19天)相关推荐

  1. 黑马程序员————IO流3(day20)

    ----------------------ASP.Net+Android+IOS开发----------------------期待与您交流! IO流3 l  File概述 l  File类常见方法 ...

  2. 黑马程序员————IO流2(day19)

    ----------------------ASP.Net+Android+IOS开发----------------------期待与您交流! IO流2 l  BufferedWriter l  B ...

  3. 黑马程序员————IO流1(day18)

    ----------------------ASP.Net+Android+IOS开发----------------------期待与您交流! IO流1 l  其它对象(System) l  其它对 ...

  4. 黑马程序员 IO流

       ------- android培训.java培训.期待与您交流! ----------  IO流 java.io包中的stream类根据它们操作对象的类型是字符还是字节可分为两大类: 字符流和字 ...

  5. 黑马程序员——IO流

    ------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...

  6. 黑马程序员————IO流4(day21)

    ----------------------ASP.Net+Android+IOS开发----------------------期待与您交流! IO流4 l  对象的序列化 l  管道流 l  Ra ...

  7. 黑马程序员——IO 流总结

    ------<a href="http://www.itheima.com"target="blank">Java培训.Android培训.iOS培 ...

  8. 黑马程序员---IO流

    ---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net ...

  9. 黑马程序员————IO流------(3)

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- File类: 流只能操作数据,若想要用流操作被文件封装的数据信息,必须用file对象. > ...

最新文章

  1. 最近面试Java后端开发的感受
  2. python caffe报错:No module named google
  3. matplotlib.pyplot 和opencv 的结合画图
  4. OpenCASCADE绘制测试线束:几何命令之展示
  5. wxWidgets:wxCustomBackgroundWindow<W> 类模板用法
  6. 23个机器学习项目,助你成为人工智能大咖
  7. MFC中使用SDL播放音频没有声音的解决方法
  8. 也读《人月神话》:没有银弹的软件工程
  9. 江苏省计算机考试Python用书,【关注】Python列入高考内容以及全国计算机等级考试!...
  10. 移除inline-block间隙
  11. Membership学习(二)membership入门[xgluxv]
  12. python爬取下拉列表数据_python 抓取下拉列表控制的表格的所有数据
  13. 2018款macbook pro如何安装windows双系统
  14. 串口协议和RS-232标准,以及RS232电平与TTL电平的区别,“USB/TTL转232“模块(以CH340芯片模块为例)的工作原理。
  15. What Makes a Great Maintainer of Open Source Projects?
  16. linux indent添加,linux indent格式化代码
  17. 淘宝/天猫店铺订单数据导出、销售报表、数据分析
  18. Flex Frameworks
  19. 两种典型频谱仪架构介绍
  20. mysql中mvcc解决不可重复读

热门文章

  1. vb怎么自动连接服务器,vb如何连接云服务器数据库(云服务器和云数据库)
  2. 数据库中的blob是什么类型?
  3. 【程序源代码】毕业设计源码推荐
  4. IOS开发之——硬件开发-蓝牙(07)
  5. WLAN用户接入流程
  6. 上奇产业通:全国人工智能产业分析报告
  7. nodejs的下载安装
  8. python的科学计算库有哪些_python科学计算:带你初探scipy库的常量模块和函数模块...
  9. RK3399平台开发系列讲解(电源管理篇)11.7、PM callback
  10. 适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐