2 boywkc1 boywkc1 于 2013.07.24 12:12 提问

为什么 函数不执行hfc.delete(path)函数;
public static void updateLog(String code, String record, String fileName) {
    try {
        String content = code + " " + record
                + System.getProperty("line.separator");
        File file = new File(fileName);
        // file.mkdir();

        // if file doesn't exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(content);
        bw.close();

        System.out.println("Log File updated");

    } catch (IOException e) {
        e.printStackTrace();
    }

    List list = new ArrayList();
    try {
        BufferedReader bw = new BufferedReader(new FileReader(fileName));
        String line = null;
        try {
            while ((line = bw.readLine()) != null) {
                list.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    int max = list.size();// TXT lines
    if (max > 10) {
        one_key();
    }
}

public static void one_key() {
    // delete the first line
    String filePath = "C:\\LOG";
    String fileName = "AddLog.txt";
    del(filePath, fileName);

    // delete file
    httpHandler hfc = new httpHandler();
    String path = "C:\\LOG\\AddLog.txt";
    hfc.deleteFile(path);

    // copy the file
    // File files = new File("C:\\LOG\\test\\");
    // File file[] = files.listFiles();// 获得目录中的文件及子目录信息
    // int i = (int) (Math.random() * file.length);
    // fun(file, i);

}

public static void del(String filePath, String fileName) {

    RandomAccessFile readFile = null;
    try {
        readFile = new RandomAccessFile(filePath + "\\" + fileName, "r");
        readFile.readLine();
        readFile.readLine();
        readFile.readLine();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return;
    }
    RandomAccessFile writeFile = null;
    try {
        writeFile = new RandomAccessFile(filePath + "\\test\\" + fileName,
                "rw");
        String tempLine = null;
        while ((tempLine = readFile.readLine()) != null) {
            writeFile.writeBytes(tempLine);
            writeFile.writeBytes("\r\n");
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        readFile.close();
        writeFile.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public boolean deleteFile(String sPath) {
    boolean flag = false;
    File file = new File(sPath);
    // 路径为文件且不为空则进行删除
    if (file.isFile() && file.exists()) {
        file.delete();
        flag = true;
    }
    return flag;
}

public static void fun(File file[], int i) {
    if (file[i].exists()) {// 如果文件存在
        String name = file[i].getName();// 获取文件名
        if (file[i].isFile() && name.endsWith(".txt")) { // 如果是文件并且后缀名为.txt
            File dest = new File("c:\\LOG\\" + file[i].getName());
            file[i].renameTo(dest);
        } else {
            int j = (int) (Math.random() * file.length);
            fun(file, j);
        }
    } else {
        int j = (int) (Math.random() * file.length);
        fun(file, j);
    }
}
Csdn user default icon
上传中...
上传图片
插入图片