1.File类,java.io包下。
2.io流
// 文本等类型使用字符流,Reader输入流,Writer输出流
// 视频,图片等使用字节类,InputStream输入流,OutputStream输出流
/*
* 字节流,处理图片视频等 FileInputStream
*/
@Test
public void test1(){
File file1 = new File("2050550.jpg");
File file2 = new File("复制的2050550.jpg");
FileInputStream fileInputStream;
FileOutputStream fileOutputStream;
try {
fileInputStream = new FileInputStream(file1);
fileOutputStream = new FileOutputStream(file2);
// 复制过程
byte[] butter = new byte[10];
int len;
while ((len= fileInputStream.read(butter))!=-1){
fileOutputStream.write(butter,0,len);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
if (fileInputStream != null)
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
if (fileOutputStream != null)
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/*
* 缓冲字节流(处理流)BufferedInputStream
*/
@Test
public void test2(){
BufferedInputStream bi = null;
BufferedOutputStream bo = null;
try {
bi = new BufferedInputStream(new FileInputStream(new File("2050550.jpg")));
bo = new BufferedOutputStream(new FileOutputStream(new File("缓冲字节流2050550.jpg")));
byte[] buffer = new byte[1024];
int len;
while ((len = bi.read(buffer)) != -1){
bo.write(buffer,0,len);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bo != null) {
try {
bo.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (bi != null){
try {
bi.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
/*
* 缓冲字符流
*/
@Test
public void test3(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("2050550.txt")));
bw = new BufferedWriter(new FileWriter(new File("缓冲字符流2050550.txt")));
char[] buffer = new char[1024];
int len;
while ((len = br.read(buffer)) != -1){
bw.write(buffer,0,len);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (br != null){
try {
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (bw != null){
try {
bw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
3.转换流。字节流和字符流之间的转换。
InputStreamReader:将InputStream转换为Reader
OutputStreamWriter:将Writer转换为OutputStream