java高级特性与实战项目 ——第三章: File I/O

1.使用BufferedReader和BufferedWriter复制C:\target.txt查看文件内容和文件带大小是否一致。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Buffered {

	public static void main(String[] args) throws IOException {
		File f = new File("C:\\Users\\lenovo\\Desktop\\Java\\source.txt");  //创建一个新的对象 指定文件
		File on = new File("D:\\target.txt");
		FileWriter fw = null;
		FileReader fr = null;
		BufferedReader br =null;
		BufferedWriter bw =null;
		String str=null;
		StringBuffer sb = new StringBuffer();
		try {
			fw = new FileWriter(on);
			fr = new FileReader(f);
			br = new BufferedReader(fr);
			bw = new BufferedWriter(fw);
			while((str=br.readLine())!=null) {
				bw.write(str);
				bw.newLine();
				sb.append(str);
			}
			System.out.println("源文件内容为:"+sb);
			System.out.println("源文件大小:"+f.length()+"字节");
		} catch (Exception e) {
			e.printStackTrace();;
		}finally {
			try {
				bw.close();
				br.close();
				fr.close();
				fw.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
			FileReader fr1 =null;
			BufferedReader br1 =null;
			String str1;
			StringBuffer sbf =null;
			try {
				fr1 = new FileReader(on);
				br1 = new BufferedReader(fr1);
				sbf = new StringBuffer();
            while((str1 =br1.readLine())!=null) {
            	sbf.append(str1);
            	System.out.println("新的文件内容:"+sbf);
            	System.out.println("源文件内容大小为:"+on.length()+"字节");
            }
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				try {
					br1.close();
					fr1.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
	}
}

2.编写一个JAVA程序读取Windows目录下的win.ini文件,并输出其内容
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Windows {

	public static void main(String[] args) {
		FileInputStream inputStream = null;
		try {
			File file = new File("C:\\Windows\\win.ini"); //创建一个新的对象 指定文件
			inputStream = new FileInputStream(file);  
			byte [] buffer = new byte[1024];
			int len = 0;
			StringBuffer sb = new StringBuffer();
			while((len = inputStream.read(buffer))!=-1){
			String str = new String(buffer,0,len);
			sb.append(str);
		}
		System.out.println(sb.toString());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				inputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
第二种方法:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Test1 {

	public static void main(String[] args) throws IOException {
		Reader fr = null;
		BufferedReader br = null;
		StringBuffer sb = new StringBuffer();
		 try {
			fr = new FileReader("C:\\Windows\\win.ini");
			br = new BufferedReader(fr);
			String line = br.readLine();
			while (line!=null){
				sb.append(line);
				System.out.println(line);
				line=br.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {	
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
}



你可能感兴趣的:(java高级特性与实战项目 ——第三章: File I/O)