文件读写备忘【JAVA】

复习了java IO操作,总结以便备忘。

package threeDay;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestFile2 {
	/**
	 * 文件操作
	 * @param args
	 */
	public static void main(String[] args){
		String fileName = "d:\\test.txt";
		String contents = "1234567890\n";
		
		System.out.println("二进制形式读取文件----开始");
		readByBytes(fileName);
		System.out.println("二进制形式读取文件----结束");
		
		System.out.println("以字符形式读入文件,一次对入一个字符----开始");
		readByCharter(fileName);
		System.out.println("以字符形式读入文件,一次对入一个字符----结束");
		
		System.out.println("以字符形式读入文件,一次对入一行字符----开始");
		readLineByCharter(fileName);
		System.out.println("以字符形式读入文件,一次对入一行字符----结束");
		
		System.out.println("将字符内容追加到文件末尾----开始");
		writeContentsByAppend(fileName,contents);
		System.out.println("将字符内容追加到文件末尾----结尾");
		
		System.out.println("将字符内容覆盖原文件----开始");
		writeContentsByClear(fileName,contents);
		System.out.println("将字符内容覆盖原文件----结尾");
		
	}

	/**
	 * 以二进制形式读取文件
	 * @param fileName   
	 */
	public static void readByBytes(String fileName) {
		InputStream in = null;
		try {
			in = new FileInputStream(fileName);
			int i;
			while ((i = in.read()) != -1) {
				System.out.print((char) i);
			}
			System.out.println("\n");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 以字符形式读入文件一次读入一个
	 * @param fileName
	 */
	public static void readByCharter(String fileName) {
		InputStream in = null;
		InputStreamReader reader = null;
		try {
			in = new FileInputStream(fileName);
			reader = new InputStreamReader(in);
			int i;
			while ((i = reader.read()) != -1) {
				System.out.print((char) i);
			}
			System.out.println("\n");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 以字符形式一次读入一行
	 * @param fileName
	 */
	public static void readLineByCharter(String fileName) {
		InputStream in = null;
		BufferedReader reader = null;
		try {
			in = new FileInputStream(fileName);
			reader = new BufferedReader(new InputStreamReader(in));
			String str = null;
			while ((str = reader.readLine()) != null) {
				System.out.println(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 将内容追加到文件末尾
	 * @param fileName
	 * @param contents
	 */
	public static void writeContentsByAppend(String fileName,String contents){
		FileWriter writer = null;
		try {
			writer = new FileWriter(fileName, true);
			writer.write("\n"+contents);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 将内容覆盖到原文件
	 * @param fileName
	 * @param contents
	 */
	public static void writeContentsByClear(String fileName,String contents){
		FileWriter writer = null;
		try {
			writer = new FileWriter(fileName, false);
			writer.write("\n"+contents);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	} 

}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值