Java动态生成bat文件备份数据库

String t = Thread.currentThread().getContextClassLoader().getResource("").getPath();获取发布运行路径

//解决路径中空格的问题

t=URLDecoder.decode(t,"utf-8");


int num = t.indexOf("test");查找此项目跟目录位置

String path = t.substring(1, num).replace('/', '\\')+ "test\\static\\bat\\1.bat";写bat文件路径

String data ="\"C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\mysqldump\" -u \"root\" -p\"root\" -P\"3306\" \"test\"> \""+ mysqlPathString + "\\" + fileNameString + ".sql\"";

//bat内容

data.replace("\'", "\"");   

File txt = new File(path);

txt.createNewFile();创建文件

byte bytes[] = new byte[512]; bytes = data.getBytes();写文件

int b = data.length();

FileOutputStream fos = new FileOutputStream(txt);

fos.write(bytes, 0, b);

fos.close();

runtime.exec(path);

猜你喜欢

转载自blog.csdn.net/u010446936/article/details/53009528