字节流与字符流操作文件的读写的方式
字节流文件操作:主要用于读取图片,MP3,AVI视频等文件。
字符流文件操作:主要用于读取处理纯文本数据。
缓冲流操作文件的读取和写入
字节流操作文件的读取和写入:
字节流文件操作:主要用于读取图片,MP3,AVI视频等文件。
使用InputStream与OutputStream的子类实现为:FileInputStream与FileOutputStream。
字节流向文件中写入数据(字节流FileOutputStream )
public static void testFileOutputStream() {
try (FileOutputStream fileOutputStream = new FileOutputStream("E:\\document\\io\\d.txt");
){
String str = "FileOutputStream测试数据";
fileOutputStream.write(str.getBytes());
String str2 = "\r\n";
fileOutputStream.write(str2.getBytes());
String str3 = "FileOutputStream测试再次添加数据";
fileOutputStream.write(str3.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
字节流从文件的读取(字节流FileInputStream)
// read()
public static void testFileInputStream() {
try (FileInputStream fileOutputStream = new FileInputStream("E:\\document\\io\\d.txt");// 内容:FileOutputStream测试数据\r\nFileOutputStream测试再次添加数据
){
// read()
int read1 = fileOutputStream.read();
System.out.println((char) read1);
int read2 = fileOutputStream.read();
System.out.println((char) read2);
} catch (IOException e) {
e.printStackTrace();
}
}
// read(byte b[]) new String(a