IO

创建文件
import java.io.*;

class Demo2 {
	public static void main(String[] args) {
		FileWriter fw = null;
		try {
			fw = new FileWriter("demo.txt");
			fw.write("xxxx");			
		}
		catch (IOException e) {
				System.out.println(e.toString());
		}		
		finally {
			try {
				if (fw != null)		//关键
					fw.close();
			}
			catch (IOException e) {
				System.out.println(e.toString());
			}
		}
	}
}

 

续写

import java.io.*;

class Demo3 {
	public static void main(String[] args) {
			FileWriter fw = null;
			try {
				fw = new FileWriter("demo.txt",true);			
				fw.write("hahah");
			}
			catch (IOException e) {
				System.out.println(e.toString());				
			}
			finally {
				try {
					if(fw != null)
						fw.close();
				}
				catch (IOException e) {
					System.out.println(e.toString());				
				}
			}
	}
}


 

读取

import java.io.*;

class DemoReader {
	public static void main(String[] args) throws IOException {
			FileReader fr = new FileReader ("demo.txt");
			int ch = 0;
			while ((ch = fr.read())!=-1) {
				System.out.println((char)ch);
			}
	}
}

import java.io.*;

class ReaderDemo {
	public static void main(String[] args)throws IOException {
			FileReader fr =new FileReader("Demo.java");
			char[] buf = new char [1024];				//将字符读入数组
			int num = 0;
			while((num = fr.read(buf))!=-1) {
				System.out.print(new String(buf,0,num));		//从0开始 取num个
			}
			fr.close();
	}
}
 

拷贝

import java.io.*;

class CopyDemo {
	public static void main(String[] args)  {
			FileReader fr = null;
			FileWriter fw = null;
			try {
				fr = new FileReader("demo.java");
				fw = new FileWriter("democopy.txt");
				char[] buf = new char [1024];
				int num = 0;
				while ((num=fr.read(buf)) !=-1) {
					String st = new String (buf,0,num);				
					fw.write(st);
					fw.flush();		
				}
			}
			catch(IOException e) {
				throw new RuntimeException("失败");			
			}
			finally {
				if(fr!=null)
					try {
					fr.close();
					}
					catch(IOException e) {
						System.out.println(e.toString());
					}
				if(fw!=null)
					try {
					fw.close();
					}
					catch(IOException e) {
						System.out.println(e.toString());
					}			
			}
	}
}

缓冲区

import java.io.*;

class BufferedWriterDemo {
	public static void main(String[] args) throws IOException {
			FileWriter fw = new FileWriter("xxx.txt");
			BufferedWriter bf = new BufferedWriter(fw);			//	缓冲区
			bf.write("xxxxx");
			bf.newLine();			//换行
			bf.write("xxxxx");
			bf.flush();
			bf.close();		//关闭缓冲区就是在关闭缓冲区的流对象 不需要fw.close()
	}
}

import java.io.*;

class BufferedReaderDemo {
	public static void main(String[] args) throws IOException {
			FileReader fr = new FileReader ("xxx.txt");
			BufferedReader br = new BufferedReader(fr);
			String s = null;
			while((s = br.readLine()) != null) {		//读一行							
				System.out.println(s);
			}
			br.close();
	}
}

复制

import java.io.*;

class CopyDemo2 {
	public static void main(String[] args) {
			BufferedReader br = null;
			BufferedWriter bw = null;
			try {
				br = new BufferedReader (new FileReader("RandomDemo.java"));
				bw = new BufferedWriter (new FileWriter("Cpzz.txt"));
				String s = null;
				while ((s = br.readLine()) !=null) {
					bw.write(s);
					bw.newLine();
					bw.flush();
				}
			}
			catch(IOException e) {
				throw new RuntimeException("shibai");
			}
			finally {
				try {
					if(br != null)
						br.close();
				}
				catch(IOException e) {
					throw new RuntimeException("shibai");
				}
				try {
					if(bw != null)
						bw.close();
				}
				catch(IOException e) {
					throw new RuntimeException("shibai");
				}
			}
	}
}
import java.io.*;

class Demo {
	public static void main(String[] args) throws IOException {
		FileReader fr = new FileReader ("demo.java");
		LineNumberReader lr = new LineNumberReader(fr);
		String s = null;
		lr.setLineNumber(3);			//从3开始
		while ((s =lr.readLine()) != null) {
						System.out.println(lr.getLineNumber()+ "" +s);
		}
		lr.close();
	}
}

。。

import java.io.*;

class MyLineNumberReader {
	private Reader r;
	private int lineNum;
	MyLineNumberReader (Reader r) {
		this.r = r;
	}
	public String myReadLine () throws IOException {		//读一行方法
		lineNum++;
		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);
		}
		if(sb.length() != 0)
			return sb.toString();
		return null;
	}

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

	public void setLineNumber (int lineNum) {
		this.lineNum = lineNum;
	}
	public int getLineNumber () {
		return lineNum;
	}
}
class MyLineDmeo {
	public static void main(String[] args) throws IOException {
			
			FileReader fr = new FileReader("demo.java");
			MyLineNumberReader mr = new MyLineNumberReader(fr);
			String s = null;
			mr.setLineNumber(1);
			while((s = mr.myReadLine()) != null) {
				System.out.println(mr.getLineNumber()+":"+s);
			}
			mr.myClose();
	}
}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值