boywkc1 2013-07-24 04:12 采纳率: 0%
浏览 1027

为什么 函数不执行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);
    }
}
  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-01-09 10:51
    关注

    我看到你的代码中有如下一行:

    httpHandler hfc = new httpHandler();
    

    但是在你的代码中并没有定义 httpHandler 这个类,因此在你调用 hfc.deleteFile(path) 时会出现问题。


    你可以尝试定义这个类或者删除这行代码,来修复这个问题。


    另外,如果你的代码真的需要使用这个类,你可能需要检查你的 classpath 是否设置正确,以及是否已经导入了这个类所在的包。

    评论

报告相同问题?

悬赏问题

  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图