Java把这些不同来源和目标的数据都统一抽象为数据流。Java语言的输入输出功能是十分强大而灵活的。在Java类库中,IO部分的内容是很庞大的,有许多不同的方式进行文件读写。
下面简单介绍解中文件的读写方式。
1.BufferedReader/BuffererdWritter
public List<String> readFromFile(String filePath) {
try {
File file = new File(filePath);
//如果文件不存在
if (!file.exists()) {
return null;
}
FileReader fr = new FileReader(filePath);
BufferedReader br = new BufferedReader(fr);
String str;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
//关闭文件
br.close();
return strList;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
//由于当时需要把文字分开,所以传入一个List,如果需要的话可以替换为需要的参数
public boolean writeFile(List<String> strList, String filePath) {
try {
File file = new File(filePath);
//文件不存在
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
if (strList == null) {
bw.close();
return false;
}
for (String str : strList) {
bw.write(str + "\n");
}
bw.flush();
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
2.FileInputStream / FileOutputStream
public List<String> readFromFile(String filePath) {
FileInputStream fip = null;
File file;
try {
file = new File(filePath);
fip = new FileInputStream(file);
if (!file.exists()) {
fip.close();
return null;
}
byte[] strReadByte = new byte[(int) file.length()];
fip.read(strReadByte);
fip.close();
char[] strReadChar = new String(strReadByte).toCharArray();
//格式化
List<String> strList = new ArrayList<String>();
String tmp = "";
for (char c : strReadChar) {
if (c == '\n') {
if (tmp != "") {
strList.add(tmp);
}
tmp = "";
} else {
tmp += c;
}
}
return strList;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public boolean writeFile(List<String> strList, String filePath) {
FileOutputStream fop = null;
File file;
try {
file = new File(filePath);
fop = new FileOutputStream(file);
if (!file.exists()) {
file.createNewFile();
}
if (strList == null) {
fop.close();
return false;
}
for (String str : strList) {
fop.write((str + "\n").getBytes());
}
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
3.FileReader / FileWriter
public List<String> readFromFile(String filePath) {
try {
List<String> strList = new ArrayList<String>();
File file = new File(filePath);
if (!file.exists()) {
return null;
}
FileReader fileReader = new FileReader(filePath);
char[] strReadChar = new char[(int) file.length()];
fileReader.read(strReadChar);
//格式化
String tmp = "";
for (char c : strReadChar) {
if (c == '\n') {
strList.add(tmp);
tmp = "";
} else {
tmp += c;
}
}
fileReader.close();
return strList;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public boolean writeFile(List<String> strList, String filePath) {
try {
//读入文件
File file = new File(filePath);
//若文件不存在则创建
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWritter = new FileWriter(filePath);
if (strList == null) {
fileWritter.close();
return false;
}
for (String str : strList) {
fileWritter.write(str+"\n");
}
fileWritter.flush();
fileWritter.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
return true;
}