本篇文章,为大家带来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