List<String> fileContent=new ArrayList<String>();
public String sAllWork(String psFileName)
{
List<String> vls_fileCOntent=loadfile(readPath+"\\"+psFileName);
this.fileContent=ls_addCollate(vls_fileCOntent);
//sWriteFile(psFileName);
try {
writeFileContext(this.fileContent, writePath+"\\"+psFileName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.fileContent=null;
return null;
}
public List<String> loadfile(String filename)
{
File file = new File(filename);
StringBuilder localStrBulider = new StringBuilder();
List<String> fileheap=new ArrayList<String>();
if(file.isFile() && file.exists()) {
try {
FileInputStream pfile = new FileInputStream(filename);//首先,获取文件。
InputStreamReader isr = new InputStreamReader(pfile,"Unicode");//将读取文件的格式转换为文件的格式,
int temp;
int lasttemp=0;
//https://blog.csdn.net/Gadxiong/article/details/80098161
String rs="";
while ((temp=isr.read())!=-1){//循环遍历
//System.out.print((char)temp);//输出文件里的内容
rs+=(char)temp;
if (lasttemp==13&&temp==10)
{
fileheap.add(rs);
rs="";
}
lasttemp=temp;
}
isr.close();//关闭isr
pfile.close();//关闭file
}catch (Exception e) {
// TODO Auto-generated catch block
}
}else {
}
return fileheap;
}
public List<String> ls_addCollate(List<String> pls_sql)
{
List<String> rls=new ArrayList<String>();
int vn_len=pls_sql.size();
boolean vb_started=false;
boolean vb_onlyCopy=false;
for (int wn=0;wn<vn_len;wn++)
{
String ws_row=pls_sql.get(wn);
try {
ws_row=trimnull(ws_row);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (vb_started==false)
if (ws_row.startsWith("CREATE TABLE"))
vb_started=true;
if (vb_started)
{
if (ws_row.startsWith("GO"))
vb_onlyCopy=true;
if (vb_onlyCopy==false)
{
if (ws_row.contains("varchar]"))
{
ws_row=sReplace(ws_row);
}
}
}
rls.add(ws_row);
}
return rls;
}
private String sReplace(String psRow)
{
String rsRow=psRow;
int vn_loc=rsRow.indexOf(")");
if (vn_loc==-1)
return rsRow;
rsRow=rsRow.replace(")", ") collate Chinese_PRC_CS_AS ");
return rsRow;
}
public static void writeFileContext(List<String> strings, String path) throws Exception
{//https://blog.csdn.net/yanhhuan/article/details/109074873
if(null==strings||strings.size()==0||path==null||"".equals(path)){
return;
}
File file = new File(path);
//如果没有文件就创建
if (!file.isFile()) {
file.createNewFile();
}
BufferedWriter writer = new BufferedWriter(new FileWriter(path));
for (String l : strings) {
writer.write(l + "\r\n");
}
writer.close();
}
public String sWriteFile(String psFile)
{
//https://blog.csdn.net/jake_Aaron/article/details/120163767
Path ps=Paths.get(writePath+"\\"+psFile);
//Stream<String> lines = Files.lines(ps, Charset.forName("UTF-8"));
//lines.forEach(System.out::println);
try {
Files.write(Paths.get(writePath+"\\"+psFile), this.fileContent, StandardCharsets.UTF_8);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private String sWriteFile_utffail(String psFile)
{
try {
Path output = Paths.get(writePath+"\\"+psFile);
Files.write(output, this.fileContent);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 去除字符串中的null域
* @param string
* @return
* @throws UnsupportedEncodingException
*/
public static String trimnull(String string) throws UnsupportedEncodingException
{//https://blog.csdn.net/vamlichunyan/article/details/49765365
ArrayList<Byte> list = new ArrayList<Byte>();
byte[] bytes = string.getBytes("UTF-8");
for(int i=0;bytes!=null&&i<bytes.length;i++){
if(0!=bytes[i]){
list.add(bytes[i]);
}
}
byte[] newbytes = new byte[list.size()];
for(int i = 0 ; i<list.size();i++){
newbytes[i]=(Byte) list.get(i);
}
String str = new String(newbytes,"UTF-8");
return str;
}
Java对SQL改装
最新推荐文章于 2024-10-16 18:20:22 发布
文章介绍了Java方法loadfile用于加载并处理文件内容,ls_addCollate函数对SQL语句进行整理,writeFileContext用于写入文件,展示了如何使用BufferedWriter和Files.write进行文件操作。
摘要由CSDN通过智能技术生成