Java中 读-写 文件 BufferedReader & BufferedWriter

本篇文章,为大家带来Java中进行文件读写的一种方式。
我的文件目录:
/Users/gisboy/Desktop/a.txt

Java中,用java.io.BufferedReader 进行文件内容的[读]。

jdk1.7 之前的操作:
package org.thinkingingis;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileExample {
	
	private static final String FILENAME = "/Users/gisboy/Desktop/a.txt";
	
	public static void main(String[] args) throws IOException{
		
		//有很多读取文件的方式, BufferedReader是最简单的方式,也是最常用的方式
		BufferedReader br = null;
		FileReader fr = null;
		
		try {
			
			fr = new FileReader(FILENAME);
			br = new BufferedReader(fr);
			String sCurrentLine;
			
			br = new BufferedReader(new FileReader(FILENAME));
			
			while((sCurrentLine = br.readLine()) != null){
				System.out.println(sCurrentLine);
			}
			
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} finally{
			try {
				if(br != null){
					br.close();
				}
				
				if(fr != null)
					fr.close();
			} catch(IOException ex){
				ex.printStackTrace();
			}

			
			
		}
	}
}

try-catch-resources JDK1.7的语法:
package org.thinkingingis;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileExampleWithJDK17 {
	
	private static final String FILENAME = "/Users/gisboy/Desktop/a.txt";
	
	public static void main(String[] args){
		
		try(BufferedReader br = new BufferedReader(new FileReader(FILENAME))){
			
			String sCurrentLine;
			
			while((sCurrentLine = br.readLine()) != null){
				System.out.println(sCurrentLine);
			}
			
			//try-catch-resource 不用br.close()
			
		} catch(IOException e) {
			e.printStackTrace();
		}
		
	}
}

/********************************************我是分割线********************************************************/
下面进行[写]的操作,Java中通过java.io.BufferedWriter 进行文件写入操作。
package org.thinkingingis;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteFileExample {
	
	private static final String FILENAME = "/Users/gisboy/Desktop/a.txt";
	
	public static void main(String[] args){
		
		BufferedWriter bw = null;
		FileWriter fw = null;
		
		String content = "This is the content writer in to a.txt file.\n";
		String content1 = "这是要写入的内容";
		
		try {
			
			fw = new FileWriter(FILENAME, false); 
			bw = new BufferedWriter(fw);
			bw.write(content);
			bw.write(content1);
			
			System.out.println("完成");
			
		} catch (IOException e) {
			
			e.printStackTrace();
		} finally {
			
				try {
					
					if(bw != null)
						bw.close();
					
					if(fw != null)
						fw.close();
					
				} catch (IOException e) {
					
					e.printStackTrace();
				}
		}
		
	}
	
}

try-catch-resources JDK1.7的语法:
package org.thinkingingis;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteFileExampleWithJDK17 {
	
	private static final String FILENAME = "/Users/gisboy/Desktop/a.txt";

	public static void main(String[] args) {
		
		String content = "This is the content writer in to a.txt file...By try-catch-resources way.\n";
		
		try(BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME, true))){
			
			bw.write(content);
			
			//这种方式不用bw.close()
			
			System.out.println("完成");
			
		} catch(IOException e) {
			e.printStackTrace();
		}

	}

}

new FileWriter(FILENAME, true) 第二个参数表示是否在文件末尾写入内容。
默认为false,即:清除之前的内容,从头写入。

(如遇到问题,请留言给作者,以便共同探讨gis知识。thinkingingis@qq.com

Wechat 公众号:ThinkingInGIS


欢迎大家关注:)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值