public static void main(String[] args) throws ParseException {
outputString1( "123", new File("C:/Users/mz/Desktop/test.doc"));
}
public static void outputString1(String str, File file) {
try {
PrintWriter out = new PrintWriter(file);
byte bytes[] = str.getBytes();
int i = 0;
while ((bytes.length - i) != 0) {
out.write(bytes[i]);
i++;
}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//中文不会乱码
public static void outputString2(String str, File file) {
try {
FileOutputStream out = new FileOutputStream(file);
out.write(str.getBytes("UTF-8") );
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
java 实现读取sql 文件批量插入
package com.kingdee.eas.custom.xzkingdeemz.utils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.kingdee.bos.BOSException;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.fm.common.FMIsqlFacadeFactory;
import com.kingdee.eas.fm.common.IFMIsqlFacade;
public class OracleConnection {
private static final String DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
private static String DATABASE_URL = "jdbc:oracle:thin:@192.168.91.135:1521:myora";;
private static String DATABASE_USER = "kingdee";
private static String DATABASE_PASSWORD = "125937";
// private static String DATABASE_URL = "jdbc:oracle:thin:@10.123.69.161:1521:KDDB1";;
// private static String DATABASE_USER = "kingdee";
// private static String DATABASE_PASSWORD = "kingdee";
public static Connection getConn() {
try {
Class.forName(DRIVER_CLASS);
Connection dbConnection = DriverManager.getConnection(
DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD);
return dbConnection;
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static void closeAll(Connection conn, PreparedStatement pstmt,
ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException {
Connection conn = null;
PreparedStatement ps = null;
conn = getConn();
// String fileName="C:/Users/mz/Desktop/环保集团数据迁移/新盛-环保资料/供应商基本资料分类明细.txt" ;
String fileName="C:/Users/mz/Desktop/test.txt" ;
//读取文件
BufferedReader br = null;
StringBuffer sb = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8")); //这里可以控制编码
sb = new StringBuffer();
String line = null;
while((line = br.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
String data = new String(sb);
String[] split = data.split(";");
try {
for (int i = 0; i < split.length; i++) {
conn.setAutoCommit(false);
String sql= split[i].toString() ;
if(sql.contains("insert")){
System.out.println(i+sql);
ps = conn.prepareStatement( sql);
boolean execute = ps.execute() ;
ps.close();
}
}
conn.commit();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("导入结束。。。");
}
}