IO流之字节流知识总结

时间:2022-08-10 17:09:36

     IO流分为字符流和字节流。

          字节流;可以读取任何文件,电脑以字节的方式储存

           字符流:用来读取字符。

 

  下面是我总结的思维导图。

    IO流之字节流知识总结

 

相关练习代码

public class Demo {

@Test
public void fun() throws IOException {
FileInputStream fis = new FileInputStream("zzz.txt");
/* int read = fis.read();//从文件中读取一个字节
System.out.println(read);
int read2 = fis.read();//读取下一个
System.out.println(read2);
int read3 = fis.read();//读取下一个
System.out.println(read3);//结果是-1说明文件的结束标记是-1*/

FileOutputStream fos = new FileOutputStream("xxx.txt");//如果没有文件,会自动创建
int b;
while ((b = fis.read()) != -1) {
fos.write(b);//将字节流写入文件
System.out.println(b);//通过循环读取
}

fis.close();//关闭流
fos.close();
}

//文件的追加
@Test
public void fun2() throws IOException {
FileOutputStream fos = new FileOutputStream("zzz.txt", true);//文件的追加
fos.write(98);
fos.write(99);
fos.close();
}

//文件的拷贝一个字节一个字节的拷贝
@Test
public void fun3() throws IOException {
FileInputStream fis = new FileInputStream("592.jpg");//创建输入流
FileOutputStream fos = new FileOutputStream("make/make/m/2.jpg", true);//输出流
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
}
fis.close();
fos.close();
}

//利用字节数组拷贝
//使用的是available方法获取文件长度
//大文件都读取到自己数组,内存溢出
@Test
public void fun4() throws IOException {
FileInputStream fis = new FileInputStream("592.jpg");
FileOutputStream fos = new FileOutputStream("make/make/m/copy.jpg");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fos.write(bytes);
fis.close();
fos.close();

}

//标准小数组文件读取方式
@Test
public void fun5() throws IOException {
FileInputStream fis = new FileInputStream("592.jpg");//输入流
FileOutputStream fos = new FileOutputStream("make/make/m/copy2.jpg");//输出流
byte[] bytes = new byte[1024 * 8];//小数组
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}

fis.close();
fos.close();
}


//缓冲区文件读写
@Test
public void fun6() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("592.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("make/make/m/copy3.jpg"));
int len;
while ((len = bis.read()) != -1) {
System.out.println(len);
bos.write(len);
}

bis.close();
bos.close();
}

//close方法包含刷新功能,关闭之前先将缓冲区写到文件中
@Test
public void fun7() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("592.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("make/make/m/copy4.jpg"));
int len;
while ((len = bis.read()) != -1) {
bos.write(len);
bos.flush();//刷新功能
}
bis.close();
bos.close();
}

//字节流读写中文
@Test
public void fun8() throws IOException {
FileOutputStream fos = new FileOutputStream("zzz.txt");
fos.write("你好我是胡少君".getBytes());
fos.close();
}

//标准的异常处理1.6及以前版本
@Test
public void fun9() throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;

try {
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("zzz.txt");
byte[] bytes = new byte[1024 * 8];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} finally {
try {
if (fis != null)
fis.close();
} finally {
if (fos != null)
fos.close();
}
}
}

//1.7及以上版本
@Test
public void fun10() throws IOException {
try (
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("zzz.txt");
) {
byte[] bytes = new byte[1024 * 8];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
}
}

@Test
public void fun11() throws IOException{

BufferedInputStream bis=new BufferedInputStream(new FileInputStream("make/make/m/jm.jpg"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("make/make/m/encode.jpg"));
int len;
while ((len=bis.read())!=-1){
bos.write(len^123);//异或两次为本身
}
bis.close();
bos.close();
}
}