黑马程序员--第十九天:io流第二天

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

 

import java.io.*;

class BufferedWriterDemo 
{
	public static void main(String[] args) throws IOException
	{
		FileWriter fw = new FileWriter("but.txt");

		BufferedWriter bw = new BufferedWriter(fw);

		bw.write("abc");

		bw.flush();

		bw.close();
		System.out.println("Hello World!");
	}
}

/*
sum:
	new BufferedWriter(fw);
	bw.write("abc");
	bw.flush();
	bw.close();
*/

//19-2
import java.io.*;

class BufferedReaderDemo 
{
	public static void main(String[] args) throws IOException
	{
		FileReader fr = new FileReader("buf.txt");

		//	返回:
		//  包含该行内容的字符串,不包含任何 行终止符(\r \n),如果已到达流末尾,则返回 null 
		BufferedReader br = new BufferedReader(fr);

		String line = null;

		while((line = br.readLine())!=null)//因为 String型 是个对象,当没有String对象时 返回null 
										   //而 int 型的是数字,所以当标记没有长度是,用-1.
			System.out.println(line);

		br.close();
	}
}

/*
sum:
	new BufferedReader(fr);
	br.readLine();
*/


//19-3
import java.io.*;

class BufferedCopy 
{
	public static void main(String[] args) 
	{
		BufferedReader br = null;
		BufferedWriter bw = null;

		try
		{
			 br = new BufferedReader(new FileReader("buf.txt"));
			 bw = new BufferedWriter(new FileWriter("buf-copy.txt"));

			 String line = null;
			 while ((line = br.readLine())!=null)
			 {
				 bw.write(line);
				 bw.newLine();
				 bw.flush();
			 }
		}
		catch (IOException e)
		{
			throw new RuntimeException("读写失败");
		}
		finally{
			try
			{
				if (br != null)
					br.close();
			}
			catch (IOException e)
			{
				throw new RuntimeException("读取失败");
			}

			try
			{
				if (bw != null)
					bw.close();
			}
			catch (IOException e)
			{
				throw new RuntimeException("写入失败");
			}
		}

		System.out.println("Hello World!");
	}
}


/*
sum:
	bw.newLine();
*/


/*19-6
装饰设计模式:
当想要对已有的对象进行功能增强时,
可以定义类,将已有对象传入,基于已有的功能,并提供加强功能。
那么自定义的该类称为 装饰类。

装饰类通常会通过构造函数接收被装饰类的对象,
并基于被装饰的对象的功能,提供更强的功能。
*/

/*19-7
装饰者设计模式与继承的比较

MyReader//专门用于读取数据-----继承的结构
	|--MyTextReader
		|--MyBufferTextReader
	|--MyMediaReader
		|--MyBufferMediaReader
	|--MyDataReader
		|--MyBufferDataReader

class MyBufferReader
{
	MyBufferReader(MyTextReader text){
	}

	MyBufferReader(MyMediaReader text){
	}

	MyBufferReader(MyDataReader text){
	}
}
该类的扩展性很差,
找到其参数的共同类型,通过多态形式通过扩展性

class MyBufferReader extends MyReader		//继承公共父类,即MyReader,而不像继承结构中-继承MyReader的子类
{
	MyBufferReader(MyReader r){
	}
}

MyReader//专门用于读取数据的类-----装饰模式的结构
	|--MyTextReader
	|--MyMediaReader
	|--MyDataReader
	|--MyBufferReader     //组合结构

*/


//19-5
import java.io.*;

class MyBufferedReader extends Reader
{
	private Reader fr = null;
	MyBufferedReader(Reader fr){
		this.fr = fr;
	}

	public String myReadLine() throws IOException{
		StringBuilder sb = new StringBuilder();   //StringBuilder();
		int ch = 0;
		while ((ch = fr.read())!= -1)
		{
			if (ch=='\r')
				continue; // the keyword continue.
			if(ch == '\n')
				return sb.toString();
			sb.append((char)ch);
		}
		if (sb.length() != 0)
			return sb.toString(); //important to return the last line.
		
		return null;
	}

	public int read (char[] buf, int off, int len) throws IOException{
		return read (buf, off, len);		//调用对象方法
	}

	public void close() throws IOException{
		fr.close();			//调用对象方法
	}

	public void myClose() throws IOException{
		fr.close();
	}

}

class MyBufferedReaderDemo{

	public static void main(String[] args) throws IOException
	{
		FileReader fr = new FileReader("buf.txt");
		MyBufferedReader mr = new MyBufferedReader(fr);
		String line = null;
		while ((line = mr.myReadLine()) != null)
		{
			System.out.println(line);
		}
		System.out.println("Hello World!");
	}
}

/*
sum:
	continue;
	new StringBuilder();
	sb.append((char)ch);
*/

/*
sum:
	装饰者设计模式与继承的比较;
	多态调用对象方法;
	继承父类要复写抽象方法;
*/

//19-9
import java.io.*;

class LineNumberReaderDemo 
{
	public static void main(String[] args) throws IOException
	{
		FileReader fr = new FileReader("buf.txt");
		LineNumberReader lnr = new LineNumberReader(fr);

		String line = null;
		lnr.setLineNumber(10000000);
		while ((line = lnr.readLine()) != null)
		{
			System.out.println(lnr.getLineNumber()+":"+line);
		}
		
		lnr.close();
	}
}

/*
sum:
	new LineNumberReader(fr);
	lnr.setLineNumber(10000000);
	lnr.getLineNumber();
*/

//19-13
import java.io.*;

class CopyMp3 
{
	public static void main (String[] args) throws IOException
	{
		long start = System.currentTimeMillis ();		//学过的方法需要巩固,忘了就用不出来了
		copy_1();
		long end = System.currentTimeMillis();
		System.out.println(end-start);
	}

	public static void copy_1() throws IOException{		//注意static的用法
		MyBufferedInputStream bis = new MyBufferedInputStream(new FileInputStream("F:\\s.mp3"));
		BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream("F:\\s-copy.mp3"));

		int b = 0;
		while ((b = bis.myRead())!=-1)
		{
			bos.write(b);
		}

		bis.myClose();
		bos.close();
	}
}


/*
sum:
	//学过的方法需要巩固,忘了就用不出来了
	//注意static的用法
*/

//19-14

import java.io.*;

class  MyBufferedInputStream
{
	private InputStream is;
	private byte[] buf = new byte[1024];	//Byte与byte是不同的2个属性
	int count = 0, pos = 0;

	MyBufferedInputStream(InputStream is){
		this.is = is;
	}

	public int myRead () throws IOException{	//函数有返回值
		if (count == 0)
		{
			count = is.read(buf);
			pos=0;
			if (count < 0)
				return -1;			
		}
		
		 if (count > 0)
		{
			byte b = buf[pos];
			pos++;
			count--;
			return b&0xff;	//重点原因,1111-1111 byte大小为-1,与返回值的-1冲突,
							//因此通过&0xff改变返回值大小,但不改变返回值的最后一个字节		
		}
		return -1;
	}

	public void myClose() throws IOException{
		is.close();
	}
/*
	public static void main(String[] args) 
	{
		System.out.println("Hello World!");
	}
	*/
}

/*
sum:
	//函数有返回值
	//重点原因,1111-1111 byte大小为-1,与返回值的-1冲突,
	//因此通过&0xff改变返回值大小,但不改变返回值的最后一个字节
*/

//19-15

import java.io.*;

class  ReadIn
{
	public static void main(String[] args) throws IOException
	{
		InputStream is = System.in;//疑问: System.in 是ImputStream 类,是抽象类,为何可以实例化?
		BufferedInputStream bis = new BufferedInputStream (is);
		int ch = 0;
	/*	while ((ch = bis.read())!=1)
		{
			System.out.println(ch);  //用cmd 的ctrl+c 能退出System.in的read。
		}	*/
		StringBuilder sb = new StringBuilder();
		while ((ch = bis.read())!=1)
		{
			if (ch == '\r')
				continue;
			if (ch == '\n')
			{
				String s = sb.toString();
				if ("over".equals(s))//equals的用法
					break;
				System.out.println(s.toUpperCase());//toUpperCase
				sb.delete(0,sb.length());//清除String数组
			}
			else sb.append((char)ch);
		}

		System.out.println("Hello World!");
	}
}


/*
sum:
	"over".equals(s)//equals的用法
	sb.delete(0,sb.length());//清除String数组
	//疑问: System.in 是ImputStream 类,是抽象类,为何可以实例化?
	//用cmd 的ctrl+c 能退出System.in的read。
	s.toUpperCase();//toUpperCase
*/

//19-16_17
import java.io.*;
import java.util.*;
class TransStreamDemo 
{
	public static void main(String[] args) throws IOException
	{
/*		InputStream is = System.in;

		InputStreamReader isr = new InputStreamReader(is);		//字节流转换字符流

		BufferedReader br = new BufferedReader (isr);			*/

		//System.setIn(new FileInputStream("F:\\t.txt"));
		//System.setOut(new PrintStream("F:\\t-copy.txt"));
		//键盘录入常见写法
		BufferedReader br = new BufferedReader (new InputStreamReader (System.in));

		BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System.out));	//字符流转化字节流

		String s = null;
		while (!(s = br.readLine()).equals("over"))//over可以直接放在这里,这样可以减少代码
		{
			//if (s.equals("over"))
			//	break;  
			bw.write(s);
			bw.newLine();
			bw.flush();
		}
		System.out.println("Hello World!");

		br.close();
	}
}

/*
sum:
	System.setIn(new FileInputStream("F:\\t.txt"));
	System.setOut(new PrintStream("F:\\t-copy.txt"));
	new InputStreamReader(is);		//字节流转换字符流
	new OutputStreamWriter (System.out);	//字符流转化字节流
	FileReader 的编码表是固定的,要用其他的码表就要用InputStreamReader
*/

//19-21
import java.io.*;
import java.util.*;
import java.text.*;
class  Exceptioninfo
{
	public static void main(String[] args) 
	{
		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("Exceptioninfo.txt");
			ps.println(s);//
			System.setOut(ps);				
			}
			catch (Exception ex)
			{
				throw new RuntimeException ("写入失败");
			}
			e.printStackTrace(System.out);//
		}
		System.out.println("Hello World!");
	}
}


/*
sum:
	ps.println(s);
	e.printStackTrace(System.out);
*/
//log4j 日志工具包


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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值