您的位置:首页 > 编程语言 > Java开发

Java基础知识之文件操作(二)——IO流复制文件

2017-03-28 10:26 656 查看
1.复制文件

核心代码

inputStream = new FileInputStream(src);
outputStream = new FileOutputStream(direct);
byte[] buffer = new byte[1024];
// 读取的字节数
int length = 0;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}

具体代码

// srcName为复制文件的路径名, directName为目标文件的路径名,
public static boolean copyFile(String srcName, String directName) {
File src = new File(srcName);
// 如果复制文件不存在
if (!src.exists()) {
System.out.println("复制文件不存在");
return false;
} else if (!src.isFile()) {
// 如果src指定路径不是文件 也无法复制
System.out.println("复制文件不是文件");
return false;
}
File direct = new File(directName);
if (direct.getParentFile() == null) {
// 如果目标文件没有父路径 则在复制在复制文件的路径下
// 组拼目标文件的路径
String dir = src.getParent() + File.separator + directName;
direct = new File(dir);
} else {
// 如果目标文件有父目录,则创建目录文件夹
if (!direct.getParentFile().exists()) {
// 如果创建目录文件夹失败
if (!direct.getParentFile().mkdirs()) {
System.out.println("目标文件路径错误");
return false;
}
}
}
// 判断目标文件是否存在
if (direct.exists()) {
System.out.println("目标文件已存在");
return false;
}
// 输入流
InputStream inputStream = null;
// 输出流
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(src);
outputStream = new FileOutputStream(direct);
byte[] buffer = new byte[1024];
// 读取的字节数
int length = 0;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
// 复制成功 返回true
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
// 关闭流
try {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


2.复制文件夹

遍历子文件夹,是文件则调用文件复制的方法,是文件夹则递归调用自己继续遍历并复制

// srcName为复制文件的路径名, directName为目标文件的路径名
public static boolean copyDirectory(String srcName, String directName) {
File src = new File(srcName);
// 如果文件夹不存在
if (!src.exists()) {
System.out.println("文件夹不存在");
return false;
} else if (!src.isDirectory()) {
// 如果不是文件夹
System.out.println("复制文件不是文件夹");
return false;
}
File direct = new File(directName);
// 如果目标目录名不是以文件分隔符结尾,则加上文件分隔符
if (!directName.endsWith(File.separator)) {
direct = new File(directName + File.separator);
}
if (direct.getParentFile() == null) {
// 如果目标文件没有父路径 则在复制在复制文件的路径下
// 组拼目标文件的路径
String dir = src.getParent() + File.separator + direct.getName();
direct = new File(dir);
} else {
// 如果目标文件有父目录,则创建目录文件夹
if (!direct.getParentFile().exists()) {
// 如果创建目录文件夹失败
if (!direct.getParentFile().mkdirs()) {
System.out.println("目标文件路径错误");
return false;
}
}
}
// 如果目标文件夹存在
if (direct.exists()) {
// 则返回 false
System.out.println("目标文件已存在");
return false;
} else {
// 不存在则创建文件夹
direct.mkdirs();
}
// 判断是否复制成功
boolean flag = true;
// 复制文件 列出文件夹下所有文件
File[] files = src.listFiles();
// 遍历所有文件
for (File file : files) {
// 如果是文件
if (file.isFile()) {
// 调用复制文件的方法
// 因为前面改变过directName,所以在此处使用direct.getAbsolutePath() 拼接路径
flag = copyFile(file.getPath(),
direct.getAbsolutePath() + File.separator + file.getName());
if (!flag) {
break;
}
} else if (file.isDirectory()) {
// 递归调用,继续复制文件夹
flag = copyDirectory(file.getPath(),
direct.getAbsolutePath() + File.separator + file.getName());
if (!flag) {
break;
}
}
}
if (!flag) {
System.out.println("复制失败");
return false;
} else {
return true;
}
}


3.按行读取文本文件内容,并以指定编码写入另一文本文件

读取数据

//path 被读取文本的路径, encoding编码
public static String read(String path, String encoding)
throws IOException {
StringBuffer stringBuffer = new StringBuffer();
File file = new File(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), encoding));
String content = null;
while((content = reader.readLine())!= null) {
//"\r\n" 换行
stringBuffer.append(content + "\r\n");
}
reader.close();
return stringBuffer.toString();
}


写入数据

若需要追加内容则使用  FileOutputStream(Filefile,true)

//path 写入文件的路径,content写入内容,encoding编码
public static void write(String path, String content, String encoding)
throws IOException {
File file = new File(path);
// 有则删除
file.delete();
// 创建新文件
file.createNewFile();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file), encoding));
// 写数据
writer.write(content);
writer.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐