IO文件夹拷贝(文件内含有文件和文件夹)

时间:2023-02-25 13:36:05
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();
}