io流

5 篇文章 0 订阅

BufferedWriter:

public static void main(String[] args) throws IOException {
		//创建一个字符写入流对象
		FileWriter fw = new FileWriter("buf.txt");
		//为了提高字符写入流速率,加入了缓冲技术,只需要将需要被提高效率的流对象作为参数传递
		//给缓冲区的构造函数即可。
		BufferedWriter bufw = new BufferedWriter(fw);
		for(int x = 0 ;x < 5;x++) {
			bufw.write("adc"+x);
			bufw.newLine();
			bufw.flush();
		}
		/*bufw.write("abc\r\ndr");
		//记住只要用到缓冲区一定得刷新
		bufw.flush();*/
		//其实关闭缓冲区就是关闭缓冲区中的流对象。
		bufw.close();
	}
}

	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);
			}
//			String s1 = bufr.readLine();
//			System.out.println("s1="+s1);
//			String s2 = bufr.readLine();
//			System.out.println("s2="+s2);
			bufr.close();
		}
}

复制一个java文件:

public static void main(String[] args) {	
			BufferedReader bufr = null;
			BufferedWriter  bufw = null;				
			try {	
				bufr = new BufferedReader(new FileReader("EachFor.java"));
				bufw = new BufferedWriter(new FileWriter("Copy_java.txt"));
				String line = null;
				while((line = bufr.readLine())!=null) {
					//System.out.println(line);
					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("写入关闭失败");
				}
			}
		}
}

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

演示MyReadLine:

public static void main(String[] args)throws IOException  {
		FileReader fr = new FileReader("buf.txt");
		MyBufferedReader myBuf = new MyBufferedReader(fr);
		String line = null;
		while((line = myBuf.myReadLine())!=null) {
			System.out.println(line);
		}
		myBuf.myClose();
	}
}
class MyBufferedReader{
	private FileReader r;
	MyBufferedReader(FileReader r){
		this.r = r;
	}
	//可以一次读一行数据的方法
	public String myReadLine() throws IOException {
		//定义一个临时容器原BufferReader封装的是字符数组
		//为了演示定义一个StringBuilder容器,因为最终还是要将数据变成字符串。
		StringBuilder sb = new StringBuilder();
		int ch = 0;
		while((ch=r.read())!=-1) {
			if(ch=='\r') {
				continue;
			}
			if(ch=='\n') {
				return sb.toString();
			}
			else {
				sb.append((char)ch);
			}
		}
//如果这一行是\n结尾的会读取到最后一行,但是不会打印,添加一个if判断是为了打印最后一行,如果有数据虽然是\n结尾但是也会打印
        if(sb.length!=0) {
			return sb.toString();
		}
		return null;
	}
	public void myClose()throws IOException {
		r.close();
	}
}

public static void main(String[] args) {
		Person p = new Person();
		SuperPerson sp = new SuperPerson(p);
		sp.SuperChifan();
	}
}
class Person{
	public void chifan() {
		System.out.println("吃饭");
	}
}
class SuperPerson{
	private Person p;
	SuperPerson(Person p){
		this.p = p;
	}
	public void SuperChifan() {
		System.out.println("开胃酒");
		p.chifan();
		System.out.println("甜点");
		System.out.println("来一根");	
	}
}

myLineNumberReader:

	StringBuilder sb = new StringBuilder();
		System.out.println(sb.toString());
		
		FileReader fr = new FileReader("Copy_java.txt");
		MyLineNumberReader mylnr = new MyLineNumberReader(fr);
		String line = null;
		while((line=mylnr.myReaderLine())!=null) {
			System.out.println(mylnr.getLineNumber()+"::"+line);
		}
		mylnr.myClose();
	}
}
class MyLineNumberReader{
	private Reader r;
	private int lineNumber;
	MyLineNumberReader(Reader r){
		this.r=r;
	}
	public String myReaderLine() throws IOException {
		lineNumber++;
		StringBuilder sb = new StringBuilder();
		int ch = 0;
		while((ch=r.read())!=-1) {
			if(ch=='\r')
				continue;
			if(ch=='\n')
				return sb.toString();
			else {
				sb.append((char)ch);//这里一定要添加进去,否则只会打印多少行
				//sb.toString();
			}
		}
		if(sb.length()!=0) {
			return sb.toString();
		}
		return null;
	}
	public void setLineNumber(int lineNumber) {
		this.lineNumber = lineNumber;
	}
	public int getLineNumber() {
		return lineNumber;
	}
	public void myClose()throws IOException {
		r.close();
	}
}

复制图片:

	
	public static void main(String[] args){			
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try {		
			fis = new FileInputStream("D:\\1.png");
			fos = new FileOutputStream("D:\\2.png");
			
//			fis = new FileInputStream("D:\\1.png");		
//			fos = new FileOutputStream("‪‪‪D:\\2.png");

			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("读取关闭失败");	
					}
				}
		}
	}

将一个java文件改变大写并打印到控制台:

public static void main(String[] args) throws IOException {
		BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("EachFor.java")));  //BufferedReader bufr = new BufferedReader(new InputStreamReader((System.in));从控制台打印
		BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter((System.out)));
//BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStreamWriter(“文件名称”));输入到控制台的东西打印到文件夹中
		String line = null;
		while((line = bufr.readLine())!= null) {
			if("over".equals(line)) {
				break;
			}
			bufw.write(line.toUpperCase());	
			bufw.newLine();
			bufw.flush();		
		}
		bufw.close();
	}	
}

异常日志信息:

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("execption.log");
				ps.println(s);
				System.setOut(ps);
			}
			catch(Exception ex) {
				throw new RuntimeException("日志文件创建失败");
			}
			e.printStackTrace(System.out);
		}
	}
}

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值