IO复制文件粗略版

需求:读取input文件下的所有内容,并复制到output文件下。例如:

E:\input

E:\output

           

1.业务层

package CopyFile;

import java.io.File;

import java.io.IOException;

 

public class CopyService {

/***

扫描二维码关注公众号,回复: 2985427 查看本文章

     * 1.获取输入输出路径

     */

    public static void SendCopyPath() {

            String separator = File.separator;

            File directory = new File("");

            String inputPath = directory.getAbsolutePath() + "/input";

            String outputPath = directory.getAbsolutePath() + "/output";

            

            File inFile = new File(inputPath);

            File outFile = new File(outputPath);

System.out.println(inFile);

System.out.println(outFile);

 

try {

getFile(inFile, outFile);

} catch (IOException e) {

System.out.println("文件创建失败,请检查路径");

}

    }

   

 

/**

*  2.逻辑判断

 * @param fileIn 输入路径o

 * @param fileOut 输出路径

 * @throws IOException

 */

public static void getFile(File inFile, File outFile) throws IOException  {

if(!inFile.exists()){

inFile.mkdirs();

}

if(!outFile.exists()){

outFile.mkdirs();

}

//1.得到input中所有的文件

File[] listFiles = inFile.listFiles();

if(listFiles.length>0){

//2.遍历文件

for (int i = 0; i < listFiles.length; i++) {

//输出路径

File inFile2 = listFiles[i];

System.out.println("+输入:+++"+inFile2);

//文件名

String name = inFile2.getName();

System.out.println("文件名====="+name);

//输出路径

File outFile2 = new File(outFile,name);

System.out.println("-输出:---"+outFile2);

 

//1.1文件--->遍历

if(inFile2.isFile()){

outFile2.createNewFile();

CopyDao.CopyOldFile(name, inFile2, outFile2);

}

//1.2文件夹-->递归

else{

getFile(inFile2, outFile2);

}

}

}else{

System.out.println("空文件夹");

}

}

 

 

}

 

2.Dao层,

package CopyFile;

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.util.ArrayList;

import java.util.List;

 

public class CopyDao {

/**

 * 根据文件名判断

 * @param name

 * @param fileIn2

 * @param fileOut2

 */

public static void CopyOldFile(String name, File fileIn2, File fileOut2) {

//cpp处理逻辑

if ((name.endsWith(".cpp") || name.endsWith(".CPP"))) {

List<String> list = readToList(fileIn2);

for (int i = 0; i < list.size(); i++) {

System.out.println(list.get(i));

if (list.get(i).startsWith("extern \"C\"")) {

list.add(i, RandomString.CPPrandom());

++i;

}

}

writer(fileOut2, list);

System.out.println(".cpp处理over");

}

 

//.h处理逻辑

else if (name.endsWith(".h") || name.endsWith(".H")) {

List<String> list = readToList(fileIn2);

if (list.get(list.size() - 1).startsWith("@end")) {

for (int i = 0; i < 200; i++) {

list.add(list.size() - 1, RandomString.HRandom());

}

}

writer(fileOut2, list);

System.out.println(".h处理over");

}

 

else if (name.endsWith(".m") || name.endsWith(".M")) {

List<String> list = readToList(fileIn2);

if (list.get(list.size() - 1).startsWith("@end")) {

for (int i = 0; i < 200; i++) {

list.add(list.size() - 1, RandomString.MRandom());

}

}

writer(fileOut2, list);

System.out.println(".m处理over");

}

 

 else {

writer(fileOut2, readToList(fileIn2));

}

 

}

 

/**

 * 2.读取文件--->放入集合

 * @param fileIn2

 * @return

 */

public static ArrayList<String> readToList(File fileIn2) {

BufferedReader br = null;

String line = null;

ArrayList<String> list = new ArrayList<String>();

StringBuffer buf = new StringBuffer();

 

try {

br = new BufferedReader(new InputStreamReader(new FileInputStream(fileIn2), "UTF-8"));

 

while (((line = br.readLine()) != null)) {

if (!line.equals("")) {

buf.append(line);

list.add(buf.toString());

buf.delete(0, buf.length());

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

br.close();

 

} catch (IOException e) {

e.printStackTrace();

}

}

return list;

}

 

 

/**

*        3.读取集合--->写出内容

*/

public static void writer(File fileOut, List<String> content) {

BufferedWriter bw = null;

try {

bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileOut), "UTF-8"));

for (int i = 0; i < content.size(); i++) {

bw.write(content.get(i));

bw.newLine();

}

 

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

 

 

3.业务处理层

package CopyFile;

 

import java.util.LinkedList;

import java.util.Random;

 

public class RandomString {

static LinkedList<String> llist = new LinkedList<String>();

static int count=-1;

/**

 * .H文件拼接

 * @return

 */

public static String HRandom() {

String funcName = RString(15);

llist.add(funcName);

String sb = RString(10);

StringBuffer sb2 = new StringBuffer();

sb2.append("-(NSArray*)" + funcName + ":(NSArray*)" + sb + ";");

return sb2.toString();

}

/**

 * .M文件拼接

 * @return

 */

public static String MRandom() {

++count;

String sb = RString(10);

StringBuffer sb2 = new StringBuffer();

for (int i = 0; i < llist.size(); i++) {

sb2.append("-(NSArray*) " +llist.get(count)+ ":(NSArray*)" + sb + "{\r\n"+ "\treturn " + sb + ";\r\n}");

}

return sb2.toString();

}

 

 

/**

  * .cpp

 */

public static String CPPrandom() {

String s = RString(10);

StringBuffer sb2 = new StringBuffer();

sb2.append("extern \"C\"  void " + s + "(){\r\n" + "\treturn;\r\n}");

return sb2.toString();

}

 

/**

 * .py

 */

public static String PyRandom() {

return RString(20);

}

 

/**

 * 生成随机数

 * @param length

 * @return

 */

public static String RString(int length) {

String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM";

Random random = new Random();

StringBuffer sb = new StringBuffer();

for (int i = 0; i < length; i++) {

int number = random.nextInt(52);

sb.append(str.charAt(number));

}

return sb.toString();

}

 

}

猜你喜欢

转载自blog.csdn.net/qq_42035966/article/details/82086464