IO文件夹拷贝(文件内含有文件和文件夹)
/**
* 文件夹拷贝(文件内含有文件和文件夹)
*
* @param src
* @param des
*/
private static void copy(String src, String des) {
File file1 = new File(src);
File[] fs = file1.listFiles();
File file2 = new File(des);
if (!file2.exists()) {
file2.mkdirs();
for (File f : fs) {
if (f.isFile()) {
fileCopy(f.getPath(), des + "\\" + f.getName()); // 调用文件拷贝的方法
} else if (f.isDirectory()) {
copy(f.getPath(), des + "\\" + f.getName());
}
}
}
} /**
* 文件拷贝的方法
*/
private static void fileCopy(String src, String des) {
BufferedReader br = null;
PrintStream ps = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(src)));
ps = new PrintStream(new FileOutputStream(des));
String s = null;
while ((s = br.readLine()) != null) {
ps.println(s);
ps.flush();
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (ps != null)
ps.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. 读取文件内容
/**
* 读取文件信息
* @param src 文件路径
* @return String
*/
public static String readCacert(String src) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(src))));
String CacertStr = null;
while (null != (CacertStr = br.readLine())) {
sb.append(CacertStr);
sb.append("\n");
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
4. 写入文件
/**
* 将String型写入文件中
* @param serverCertificate 证书字符串
* @param path 写入路径
*/
public static void writePem(String src, String path) { File file = new File(path);
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(src);
bw.newLine();
bw.flush();
bw.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
5. 创建文件
/**
* 创建文件
*/
public static File createFile(String path, String fileName) {
File f = new File(path);
if (!f.exists()) {
f.mkdirs();// 创建目录
}
File file = new File(path, fileName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
6. 如果不存在就创建新文件, 如果存在就覆盖
/**
* 将String型写入文件中
*
* @param serverCertificate
* 证书字符串
* @param path
* 写入路径
*/
public static void writeFile(String src, String path, String fileName) {
File file = createFile(path, fileName);
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(src);
bw.flush();
bw.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 创建文件
*/
public static File createFile(String path, String fileName) {
File f = new File(path);
if (!f.exists()) {
f.mkdirs();// 创建目录
}
File file = new File(path, fileName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
7. 追加字符串到指定文件中
/**
* 追加字符串到指定文件中
* @param filePath
* @param src
*/
public static void appendStrToFile(String src, String filePath) {
try {
FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
bw.append(sdf.format(d)+"##");
bw.write(src);
bw.write("\n");
bw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
8. 读取文件信息
/**
* 读取文件信息
* @param src
* @return String
*/
public static String readFile(String path) {
StringBuilder sb = new StringBuilder();
File file = new File(path);
if (!file.exists()) {
return null;
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String CacertStr = null;
while (null != (CacertStr = br.readLine())) {
sb.append(CacertStr);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
IO文件夹拷贝(文件内含有文件和文件夹)的更多相关文章
- Java_文件夹拷贝
一.思路 * 文件夹的拷贝 1.递归查找子孙级文件 2.文件复制 文件夹创建 二.代码 package com.ahd.File; import java.io.File; import java.i ...
- java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝
接上一篇的字节流,以下主要介绍字符流.字符流和字节流的差别以及文件复制拷贝.在程序中一个字符等于两个字节.而一个汉字占俩个字节(一般有限面试会问:一个char是否能存下一个汉字,答案当然是能了,一个c ...
- 07 IO流(四)——文件字节流 FileInputStream/FileOutputStream与文件的拷贝
两个类的简述 专门用来对文件进行读写的类. 父类是InputStream.OutputStream 文件读入细节 FileOutputStream流的构造方法:new FileOutputStream ...
- java学习笔记之IO编程—目录和文件的拷贝
进行文件或目录的拷贝时,要先判断处理对象是文件还是目录,如果是文件则直接拷贝,如果是目录还需要拷贝它的子目录及其文件,这就需要递归处理了 import java.io.*; class FileUti ...
- mongoDB整个文件夹拷贝备份还原的坑
现网有一个mongoDB数据库需要搬迁到新服务器,开发那边的要求是先搬迁现在的数据库过去,然后剩下的以后他们用程序同步. 数据库大楷20G左右,现网是主备仲裁的,停掉备点,拷贝了全部文件. 新服务器也 ...
- java实现文件的拷贝以及文件的删除
/** * 将文件拷贝到指定目录 * @param oldAddress 文件所在目录(文件的全路径) * @param newAddress 指定目录(包含复制文件的全名称) * @throws E ...
- java基础 File 递归删除文件夹中所有文件文件夹 目录(包含子目录)下的.java文件复制到e:/abc文件夹中, 并统计java文件的个数
File 递归删除文件夹中所有文件文件夹 package com.swift.kuozhan; import java.io.File; import java.util.Scanner; /*键盘录 ...
- META-INF文件夹是干啥的,META-INF文件夹的作用, META-INF文件夹能删吗
今天有人问到 META-INF文件夹是干啥的,META-INF文件夹的作用, META-INF文件夹能删吗,还有项目的META-INF下面一般会有个MANIFEST.MF 文件,都是干啥的. 百度搜了 ...
- [Java] 通过文件流拷贝文件
package test.stream; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
随机推荐
- od命令 查看二进制文件
od命令用于输出文件的八进制.十六进制或其它格式编码的字节,通常用于显示或查看文件中不能直接显示在终端的字符. 以数值进制格式输出:od [选项] 文件 od -d 文件 --十进制输 ...
- 连接ORACLE客户端工具navicat111.12 for oracle
安装navicat111.12 for oracle后 打开
- ie7下z-index失效问题解决方法
绝对定位元素的“有定位属性(relative或absolute)的父元素”在渲染层次时起到了主要作用,前面的被后面的覆盖了.解决办法就是给有定位属性的父元素设置z-index 解决办法: 父级元素加上 ...
- WebView三个方法区别(解决乱码问题)
最近使用WebView加载中文网页的时候出现乱码问题,网上整理下基本解决方法: 其实我发现这不管是在线还是离线显示都可以使用LoadUrl方法!联网时好像是默认utf-8,离线读取本地时需要设置默认编 ...
- 容器控件JPanel的使用
-----------------siwuxie095 工程名:TestUI 包名:com.siwuxie095.ui 类名:TestPanel.java 工程结构目录如下: 在默认窗体 JFrame ...
- SQL获取时间戳流水号
流水号生成规则: 1:流水号总长度为22位数 2:流水号总共分三部分:标头(2位)+ 时间戳(YYYYMMDDHHmmSSsss共17位)+ 随机码(3位) 举例流水号:SN2015081210240 ...
- opencv3.2 编译安装说明
Create a temporary directory, which we denote as <cmake_binary_dir>, where you want to put the ...
- ROS Learning-015 learning_tf(编程) 编写一个监听器程序 (Python版)
ROS Indigo learning_tf-02 编写一个 监听器 程序 (Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 1 ...
- 杭电acm 1021题
题意是要求能被3整除的数所以为了避免大数据的产生,直接对每个数据求余,然后相加 #include "iostream" using namespace std; int main( ...
- 1.初学c++,比较困惑的问题。
1.c++是一门实用的语言吗? c++是一个实用的工具,它很有用. 在工业软件世界中,c++被视为坚实和成熟的主流工具.它具有广泛的行业支持和好批. 2.面向对象编程在c++中的作用? 我们要开发一个 ...