IO流的三种分类方式
1.按流的方向分为:输入流和输出流
2.按流的数据单位不同分为:字节流和字符流
3.按流的功能不同分为:节点流和处理流二、IO流的四大抽象类:字符流:ReaderWriter字节流:InputStream(读数据)OutputStream(写数据)
示例:实现一个文件拷贝的功能
public static boolean copyFile(String filePath,String toFilePath) {
FileOutputStream fileOutputStream = null;InputStreamReader read = null ;
try {
String encoding = "UTF-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
//读待拷贝的文件
read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
File tofile = new File(toFilePath);
//拷贝过去的位置 存在同名文件
if(tofile.exists()){
//System.out.print("同名文件存在");
throw new Exception("同名文件存在");
}else{
//拷贝
fileOutputStream = new FileOutputStream(toFilePath);
while ((lineTxt = bufferedReader.readLine()) != null) {
String content = lineTxt;
// 写
fileOutputStream.write(content.getBytes("UTF-8"));
}
}
return true;
} else {
//System.out.println("找不到文件");
throw new Exception("404-找不到文件");
}
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
try {
fileOutputStream.close();
read.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}