FileInputStream 文件输入流
BufferedInputStream 缓冲输入流
FileOutputStream 文件输出流
BufferedOutputStream 缓冲输出流
读取(输入)一个文件的内容
File f=new File("E:\\bookimg\\新建文本文档.txt");
FileInputStream fis=new FileInputStream(f);
BufferedInputStream bis=new BufferedInputStream(fis);
byte[] bs=new byte[(int)f.length()];
bis.read(bs);
System.out.println(new String(bs));
bis.close();
fis.close();
文件的复制
E:\\bookimg:路径需要自己建
新建文本文档2.txt:可自动生成
(不写路径会提示文件找不到)
File f=new File("E:\\bookimg\\新建文本文档.txt");
File f1=new File("E:\\bookimg\\新建文本文档2.txt");
//1.先读取f的内容
FileInputStream fis=new FileInputStream(f);
BufferedInputStream bis=new BufferedInputStream(fis);
byte[] bs=new byte[(int)f.length()];
bis.read(bs);
bis.close();
fis.close();
//2.将内容写入f1
FileOutputStream fos=new FileOutputStream(f1);
BufferedOutputStream bos=new BufferedOutputStream(fos);
bos.write(bs);
bos.flush();//强制清空缓冲区
bos.close();
fos.close();
System.out.println("复制ok");
**/
//复制文件夹
File f=new File("E:\\269\\swing_01");
if(f.exists()&&f.isDirectory()) {
copyDirectory(f, "E:\\exx");
}
}
复制文件夹的方法
/**
* 复制文件夹的方法
* @param f 需要复制的文件或者文件夹
* @param path 复制的目标路径
* @throws Exception
*/
public static void copyDirectory(File f,String path) throws Exception{
for (File x : f.listFiles()) {
File y=new File(path+"\\"+x.getName());
if(x.isFile()) {
y.createNewFile();//对应的文件创建出来
/* 此处省略 */
}else {
y.mkdir();//创建文件夹
copyDirectory(x, y.toString());
}
}
}
序列化:把对象变成文件(把程序里的数据保存到本地)
反序列化:把文件变成对象(把本地的数据读写到程序里)