黑马程序员--IO流(19天)

---------------------- 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培训、期待与您交流! ----------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值