public String getTxt(String filePath) {
    BufferedReader reader = null;
    File file = new File(filePath);
    try {
        if (file.exists()) {
            reader = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream(file), "UTF-8"
                    ));

            String ans = "";
            String getLine;
            while ((getLine = reader.readLine()) != null) {
                ans += getLine;
            }
            return ans;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return "";
}


public void writeTxt(String str,String filePath) {
    FileOutputStream writer = null;
    File file = new File(filePath);

    try {
		File fileParent = file.getParentFile();
		if(!fileParent.exists()){
    		fileParent.mkdirs();
		}
		file.createNewFile();//在不存在的文件夹下创建文件
        writer = new FileOutputStream(file);
        writer.write(str.getBytes("UTF-8"));
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

从文件读写对象


public static void main(String[] args) throws Exception {
    KaWa k=new KaWa();
    k.setDd("gdhjs");
    k.setId(5);
    try (ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream("F:\\others\\Desktop\\666\\set.sav"))) {
        writer.writeObject(k);
        writer.close();
    }
    System.out.println(k);
    try (ObjectInputStream reader = new ObjectInputStream(new FileInputStream("F:\\others\\Desktop\\666\\set.sav"))) {
        KaWa k2 = (KaWa) reader.readObject();
        reader.close();
        System.out.println(k2);
    }
}

删除文件和文件夹


/**
 * 根据路径删除指定的目录或文件,无论存在与否
 *
 * @param sPath 要删除的目录或文件
 * @return 删除成功返回 true,否则返回 false。
 */
public boolean DeleteFolder(String sPath) {
    boolean flag = false;
    File file = new File(sPath);
    // 判断目录或文件是否存在
    if (!file.exists()) {  // 不存在返回 false
        return flag;
    }
    // 判断是否为文件
    if (file.isFile()) {  // 为文件时调用删除文件方法
        return deleteFile(sPath);
    } else {  // 为目录时调用删除目录方法
        return deleteDirectory(sPath);
    }
}


/**
 * 删除单个文件
 *
 * @param sPath 被删除文件的文件名
 * @return 单个文件删除成功返回true,否则返回false
 */
public boolean deleteFile(String sPath) {
    boolean flag = false;
    File file = new File(sPath);
    // 路径为文件且不为空则进行删除
    if (file.isFile() && file.exists()) {
        file.delete();
        flag = true;
    }
    return flag;
}

/**
 * 删除目录(文件夹)以及目录下的文件
 *
 * @param sPath 被删除目录的文件路径
 * @return 目录删除成功返回true,否则返回false
 */
public boolean deleteDirectory(String sPath) {
    //如果sPath不以文件分隔符结尾,自动添加文件分隔符
    if (!sPath.endsWith(File.separator)) {
        sPath = sPath + File.separator;
    }
    File dirFile = new File(sPath);
    //如果dir对应的文件不存在,或者不是一个目录,则退出
    if (!dirFile.exists() || !dirFile.isDirectory()) {
        return false;
    }
    boolean flag = true;
    //删除文件夹下的所有文件(包括子目录)
    File[] files = dirFile.listFiles();
    for (int i = 0; i < files.length; i++) {
        //删除子文件
        if (files[i].isFile()) {
            flag = deleteFile(files[i].getAbsolutePath());
            if (!flag) break;
        } //删除子目录
        else {
            flag = deleteDirectory(files[i].getAbsolutePath());
            if (!flag) break;
        }
    }
    if (!flag) return false;
    //删除当前目录
    if (dirFile.delete()) {
        return true;
    } else {
        return false;
    }
}

hhhhh