前言
Java文件读写可以分为字节流和字符流,之前一直没有好好归纳以下,借此机会做一个小笔记。
目录
字节流
字节流就是一个字节一个字节的传输,最常用的就是FileInputStream和FileOutputStream,代码如下
public static void readByFIS(String filepath) {
File file = new File(filepath);
try {
FileInputStream fis = new FileInputStream(file);
byte [] bytes = new byte[fis.available()];
while (fis.read(bytes, 0, bytes.length) != -1) {
System.out.println(new String(bytes));
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeByFIS(String filepath) {
File file = new File(filepath);
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(file, true);
fos.write("从此无心爱良夜,任他明月下西楼\n".getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
注意在读的的时候我们运用了available去获取一个和文件大小刚好的空间,这存在一个问题,如果文件过大可能会导致内存溢出。
使用BufferedInputStream 和BufferedOutputStream如下
public static void readByBIS(String filepath) {
File file = new File(filepath);
if(file.exists()) {
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[bis.available()];
bis.read(bytes);
System.out.println(new String(bytes));
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void writeByBIS(String filepath) {
File file = new File(filepath);
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filepath));
bos.write("从此无心爱良夜,任他明月下西楼".getBytes());
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
字符流
运用FileWriter和FileReader
public static void writeByFR(String filepath) {
File file = new File(filepath);
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileWriter writer = new FileWriter(filepath, true);
writer.write("从此无心爱良夜,任他明月下西楼");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readByFR(String filepath) {
File file = new File(filepath);
if(file.exists()) {
try {
FileReader reader = new FileReader(file);
char[] chars = new char[100];
reader.read(chars);
System.out.println(chars);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运用BufferedWriter和BufferedReader
ublic static void readByBR(String filepath) {
File file = new File(filepath);
String line;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
System.out.println(line);;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeByBR(String filepath) {
File file = new File(filepath);
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
writer.write("从此无心爱良夜,任他明月下西楼");
writer.flush();
writer.newLine();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
最后
点赞就是最大的支持,更多文章和资料可以关注公众号QStack,追寻最纯粹的技术,享受编程的快乐。