用BAT批处理文件启动我的Java小程序
前段时间用Java写了两个小工具,打包成jar文件后,为了方便打开,于是写了一个简单的批处理文件来进行操作。
两个小工具一个是用来方便生成博客的md文件,另一个用来进行文件加解密。代码如下:
BlogGenerator
package main;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class Generator {
static String formatStr = "yyyy-MM-dd HH:mm:ss";
public static void main(String[] args) {
System.out.println("Input your title: ");
Scanner in = new Scanner(System.in);
String title = in.nextLine();
String filename = "D:/Dropbox/Hexo/blog/source/_posts/"+title+".md";
System.out.println("categories: ");
String categories = in.nextLine();
System.out.println("tags:");
System.out.printf("- ");
String tag;
List tags = new ArrayList<>();
while ((tag=in.nextLine())!=null&&tag.length()>0) {
tags.add(tag);
System.out.printf("- ");
}
StringBuilder builder = new StringBuilder();
builder.append("---\n");
builder.append("title: "+title+"\n");
builder.append("date: "+new SimpleDateFormat(formatStr).format(new Date())+"\n");
builder.append("categories: "+categories+"\n");
builder.append("tags:\n");
for (String str : tags) {
builder.append("- "+str+"\n");
}
builder.append("---\n");
File file = new File(filename);
try {
if (!file.exists()) {
file.createNewFile();
}
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")));
String content = builder.toString();
writer.write(content);
writer.flush();
writer.close();
in.close();
//打开文件
Runtime runtime = Runtime.getRuntime();
runtime.exec("cmd /c start "+filename);
} catch (IOException e) {
e.printStackTrace();
}
}
}
代码很简单,这里只是单纯的保存一下代码。
FileEncryptor
package main;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Entrance {
private static String ALGORITHM = "DES";
public static void main(String[] args) throws NoSuchAlgorithmException {
System.out.println("Choose the mode:");
System.out.println("1. Encrypt");
System.out.println("2. Decrypt");
Scanner in = new Scanner(System.in);
String mode = in.nextLine();
switch (mode) {
case "1":
System.out.println("Input the file path which you want to encrypt:");
String inputFile = in.nextLine();
System.out.println("Input the file path where you want to store:");
String outputFile = in.nextLine();
System.out.println("Input your key:");
String keyString = in.nextLine();
SecretKey secretKey = new SecretKeySpec(keyString.getBytes(), ALGORITHM);
encrypt(inputFile, outputFile, secretKey);
break;
case "2":
System.out.println("Input the file path which you want to decrypt:");
String inputFile2 = in.nextLine();
System.out.println("Input the file path where you want to store:");
String outputFile2 = in.nextLine();
System.out.println("Input your key:");
String keyString2 = in.nextLine();
SecretKey secretKey2 = new SecretKeySpec(keyString2.getBytes(), ALGORITHM);
decrypt(inputFile2, outputFile2, secretKey2);
break;
default:
System.out.println("Wrong input!");
break;
}
System.out.println("Press any key to exit ...");
in.nextLine();
in.close();
}
public static void encrypt(String inputFile, String outputFile, Key key) {
System.out.printf("Encrypting ");
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
InputStream in = new FileInputStream(inputFile);
OutputStream out = new FileOutputStream(outputFile);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[100*1024*1024];
int r;
while((r=in.read(buffer)) > 0){
cos.write(buffer, 0, r);
System.out.printf(".");
}
cos.close();
out.close();
in.close();
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println();
System.out.println("Done!");
}
public static void decrypt(String inputFile, String outputFile, Key key) {
System.out.printf("Decrypting ");
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
InputStream in = new FileInputStream(inputFile);
OutputStream out = new FileOutputStream(outputFile);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[100*1024*1024];
int r;
while((r=in.read(buffer)) > 0){
cos.write(buffer, 0, r);
System.out.printf(".");
}
cos.close();
out.close();
in.close();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println();
System.out.println("Done!");
}
}
就是调用Cipher类进行加解密,这里只用了DES,后面可以改进。
ToolBox
为了方便调用,这里写了一个批处理文件ToolBox.bat
@echo off
:begin
echo Welcome!
echo "1" for BlogGenerator
echo "2" for FileEncryptor
set/p step=Input your choice:
echo %step%
if "%step%"=="1" (
goto next1
) else (
if "%step%"=="2" (
goto next2
) else (
goto end
)
)
:next1
start java -jar D:/Dropbox/MySoftware/BlogGenerator.jar
goto end
:next2
start java -jar D:/Dropbox/MySoftware/FileEncryptor.jar
goto end
:end
exit