import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
public class BisandBos_test {
File file;
FileInputStream fis;
FileOutputStream fos;
BufferedOutputStream bos;
BufferedInputStream bis;
//准备资源
@Before
public void creatFile() throws Exception {
file = new File("resource//bis1.txt");
}
//写
@Test
public void write() throws Exception {
fos = new FileOutputStream(file,true);
bos = new BufferedOutputStream(fos);
bos.write("I love java".getBytes());
}
//读
@Test
public void read() throws Exception {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
byte[] bytes = new byte[1024];
bis.read(bytes);
System.out.println(new String(bytes));
}
//复制
@Test
public void copyFile() throws Exception {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
fos = new FileOutputStream("resource//bis_copy.txt");
bos = new BufferedOutputStream(fos);
byte[] bytes1 = new byte[1024];
int i = -1;
while ((i = bis.read(bytes1)) != -1) {
bos.write(bytes1);
}
}
//关闭资源
@After
public void closeAll() throws Exception {
if (bos != null) bos.close();
if (bis != null) bis.close();
if (fos != null) fos.close();
if (fis != null) fis.close();
}
}
IO-Stream – Kopieren von Dateiinhalten
Supongo que te gusta
Origin blog.csdn.net/weixin_46048259/article/details/124334193
Recomendado
Clasificación