共计 2737 个字符,预计需要花费 7 分钟才能阅读完成。
前言
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,追寻最纯正的技术,享受编程的高兴。
正文完