file类方法
public File(String pathname) :通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
public File(String parent, String child) :从父路径名字符串和子路径名字符串创建新的 File实例。
public File(File parent, String child) :从父抽象路径名和子路径名字符串创建新的 File实例。
public String getAbsolutePath() :返回此File的绝对路径名字符串。
public String getPath() :将此File转换为路径名字符串。
public String getName() :返回由此File表示的文件或目录的名称。
public long length() :返回由此File表示的文件的长度。流是计算机中数据的流动
流
流是计算机数据的流动
为了提高效率一个流配有一个缓冲区
字节流:处理字节数据
字符流:处理字符数据
基本输出输入类
InputStream与OutputStream
常用方法
public int read()
public int red(byte[] b)
public int availble()
puiblic voidc write(int b)
文件拷贝
public class main {
public static void main(String[] args) {
String dir = "C:\\Users\\ycl\\Desktop";
File file = new File(dir+"\\a");
//拿到所有文件
File [] files = file.listFiles();
for(File from: files){
String fileName = from.getName();
copy(from.getAbsolutePath(),dir+"\\b\\"+fileName);
}
System.out.println("ssss");
}
public static void copy(String from,String to){
try{
//拿到上一层目录
File targetDir = new File(new File(to).getParent());
if (!targetDir.exists()){
targetDir.mkdir();
}
FileInputStream fis = new FileInputStream(from);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(to);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] buf = new byte[1024];
int size = 0;
while ((size = bis.read(buf))!=-1){
bos.write(buf);
}
bis.close();
bos.close();
}catch (Exception e){
}
}
}