Java 实现备份 Windows 锁屏壁纸

Windows10 的锁屏壁纸还是挺好看的,不过这个壁纸文件藏得目录比较深,而且壁纸更新后就会自动删掉原来的壁纸文件。手动备份的话要逐个文件修改后缀名,这不符合懒人的作风,于是就基于 Java 实现了一键备份的功能。如果不想每次手动备份,还可以在 Windows 任务计划程序中以命令行方式定期运行编译后的文件以实现完全自动备份。

运行环境

操作系统:Windows 10

Java环境:JDK-17

具体代码

package demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * 此类用于备份 Windows 锁屏壁纸
 */
public class WallpaperBackup {

	public static void main(String[] args) {
		String srcPath = "C:\\Users\\计算机用户名\\AppData\\Local\\Packages\\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\\LocalState\\Assets";
		String destPath = "D:\\Wallpaper";
		backup(srcPath, destPath);
	}

	/**
	 * 备份 Windows 锁屏壁纸
	 * 
	 * @param srcPath  源目录
	 * @param destPath 备份目录
	 */
	public static void backup(String srcPath, String destPath) {
		File srcDir = new File(srcPath);
		if (!srcDir.exists()) {
			System.out.println("source path not exist.");
			return;
		}
		File destDir = new File(destPath);
		if (!destDir.exists()) {
			System.out.println("destination path not exist.");
			return;
		}

		long minLength = 100 * 1024;
		int index = destDir.listFiles().length;

		for (File srcFile : srcDir.listFiles()) {
			if (srcFile.isFile() && srcFile.length() > minLength) { // 过滤小于 100KB 的文件
				String destName = String.format("wallpaper_%04d.png", ++index);
				System.out.println(destName);
				File destFile = new File(destDir, destName);
				try (FileInputStream fis = new FileInputStream(srcFile);
						FileOutputStream fos = new FileOutputStream(destFile)) {
					byte[] b = fis.readAllBytes();
					fos.write(b);
					fos.flush();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,你可以使用ProcessBuilder类来执行外部命令,以实现Windows下PostgreSQL数据备份与还原。以下是简单的代码示例: 1. 数据备份: ```java import java.io.IOException; public class PostgreSQLBackup { public static void main(String[] args) { String host = "localhost"; String port = "5432"; String database = "your_database"; String username = "your_username"; String password = "your_password"; String backupPath = "C:\\backup.sql"; // 备份文件保存路径 try { ProcessBuilder builder = new ProcessBuilder( "pg_dump", "-h", host, "-p", port, "-U", username, "-F", "c", "-b", "-v", "-f", backupPath, database); builder.environment().put("PGPASSWORD", password); // 设置密码环境变量 builder.redirectErrorStream(true); Process process = builder.start(); process.waitFor(); System.out.println("数据备份完成!"); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 2. 数据还原: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class PostgreSQLRestore { public static void main(String[] args) { String host = "localhost"; String port = "5432"; String database = "your_database"; String username = "your_username"; String password = "your_password"; String restorePath = "C:\\backup.sql"; // 备份文件路径 try { ProcessBuilder builder = new ProcessBuilder( "pg_restore", "-h", host, "-p", port, "-U", username, "-d", database, "-v", restorePath); builder.environment().put("PGPASSWORD", password); // 设置密码环境变量 builder.redirectErrorStream(true); Process process = builder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } int exitCode = process.waitFor(); if (exitCode == 0) { System.out.println("数据还原完成!"); } else { System.out.println("数据还原失败!"); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 以上代码使用了`pg_dump`命令进行备份,`pg_restore`命令进行还原。你需要替换相应的数据库连接信息以及备份文件路径。确保PostgreSQL的可执行文件目录已添加到系统环境变量中。 请注意,执行外部命令需要谨慎处理,确保输入的参数是可信的,以避免安全问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值