import java.io.*;

創(chuàng)新互聯(lián)建站2013年至今,先為遂昌等服務(wù)建站,遂昌等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為遂昌企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
public class MyCopy {
public static void main(String args[]){
try {
MyCopy j = new MyCopy(); j.CopyFile(new File(args[0]),new File(args[1]));
}
catch (Exception e) {
e.printStackTrace();
}
}
public void CopyFile(File in, File out) throws Exception {
FileInputStream fis = new FileInputStream(in);
FileOutputStream fos = new FileOutputStream(out);
byte[] buf = new byte[1024];
int i = 0;
while((i=fis.read(buf))!=-1) {
fos.write(buf, 0, i);
}
fis.close();
fos.close();
}
}
// 程序運(yùn)行時(shí)的命令語(yǔ)法為:
// javac MyCopy.java (sourcefile) (destfile)
// java MyCopy.java c:\1.txt d:\1.txt
// 當(dāng)然前提c盤(pán)1.txt 已存在。
直接用SQL語(yǔ)句(完全備份):
backup
database
數(shù)據(jù)庫(kù)
to
disk='c:\你的備份文件名'
將MySql中的數(shù)據(jù)庫(kù)導(dǎo)出到文件中 備份
import java.io.*;
import java.lang.*;
public class BeiFen {
public static void main(String[] args) {
// 數(shù)據(jù)庫(kù)導(dǎo)出
String user = "root"; // 數(shù)據(jù)庫(kù)帳號(hào)
String password = "root"; // 登陸密碼
String database = "test"; // 需要備份的數(shù)據(jù)庫(kù)名
String filepath = "e:\\test.sql"; // 備份的路徑地址
String stmt1 = "mysqldump " + database + " -u " + user + " -p"
+ password + " --result-file=" + filepath;
/*
* String mysql="mysqldump test -u root -proot
* --result-file=d:\\test.sql";
*/
try {
Runtime.getRuntime().exec(stmt1);
System.out.println("數(shù)據(jù)已導(dǎo)出到文件" + filepath + "中");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
將數(shù)據(jù)從磁盤(pán)上的文本文件還原到MySql中的數(shù)據(jù)庫(kù)
import java.io.*;
import java.lang.*;
/*
* 還原MySql數(shù)據(jù)庫(kù)
* */
public class Recover {
public static void main(String[] args) {
String filepath = "d:\\test.sql"; // 備份的路徑地址
//新建數(shù)據(jù)庫(kù)test
String stmt1 = "mysqladmin -u root -proot create test";
String stmt2 = "mysql -u root -proot test " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };
try {
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(cmd);
System.out.println("數(shù)據(jù)已從 " + filepath + " 導(dǎo)入到數(shù)據(jù)庫(kù)中");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
public class mycopy {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: Java mycopy〈sourcefile〉〈destfile〉");
System.exit(0);
}
File sourcefile = new File(args[0]);
if (!sourcefile.exists()) {
System.out.println("sourcefile: " + args[0] + " not exists");
System.exit(0);
}
FileChannel srcChannel = null;
FileChannel dstChannel = null;
try {
srcChannel = new FileInputStream(args[0]).getChannel();
dstChannel = new FileOutputStream(args[1]).getChannel();
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
srcChannel.close();
dstChannel.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
需要jdk1.4的支持
新聞名稱(chēng):java實(shí)現(xiàn)文件備份代碼 java實(shí)現(xiàn)數(shù)據(jù)庫(kù)備份
當(dāng)前地址:http://chinadenli.net/article18/hipsdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、服務(wù)器托管、網(wǎng)站策劃、品牌網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)