① java璁捐¢樺寘鍚闆嗗悎錛宨terator for錛宖oreach錛屾枃浠惰誨啓榪欏嚑涓鐭ヨ瘑鐐
importjava.util.*;
importjava.io.*;
/**
*@author:kaifang
**/
publicclassListDemo{
publicstaticvoidmain(String[]args)
{
List<String>list=newArrayList<String>();
list.add("AAA");
list.add("BBB");
list.add("CCC");
list.add("DDD");
//forearch寰鐜
System.out.println("forearch閬嶅巻錛");
for(Strings:list){
System.out.println(s);
}
//榪浠e櫒寰鐜
System.out.println("榪浠e櫒閬嶅巻錛");
Iterator<String>it=list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//璇誨啓鏂囦歡
try{
Filefile=newFile("aa.txt");
FileWriterfw=newFileWriter(file);
fw.write("鍛靛懙鍝掞紝AAA~~");
fw.close();
FileReaderfr=newFileReader(file);
char[]cs=newchar[60];
fr.read(cs);
System.out.println("鍐呭規潵鑷鏂囦歡錛歕n"+newString(cs));
}catch(Exceptione){
e.printStackTrace();
}
}
}
<?php
$file_dir="1.txt";
$fp=fopen($file_dir,"r");
$content=fread($fp,filesize($file_dir));//讀文件
fclose($fp);
function replaces($str)//為各欄位添加''
{
foreach($str as $k=>$v)
{
$str[$k]="'".trim($v)."'";
}
return $str;
}
function Get_item($tmp1,$tmp2,$cont,$sq)//取得sql語句;tmp1為記錄分割點,tmp2為欄位分割點
{
$tmp_rows=explode($tmp1,$cont);
foreach($tmp_rows as $key=>$value)
{
$tmp_rows2[$key]=replaces(explode($tmp2,trim($value)));
$sql[$key]=$sq.implode(",",$tmp_rows2[$key]).")\"";
}
return $sql;
}
$sq="\"insert into table1 (a1,a2,a3) values (";//sql的前半部分,要完善!
$sql=array();
$sql=Get_item("-"," ",$content,$sq);
//$db=mysql_connect("localhost","root","abc");//聯接mysql,自己加
//mysql_select_db(....
foreach($sql as $v)
{
mysql_query($v);//執行sql;
}
//mysql_close($db);
?>
或者生成一個1.sql文件,再把它導入mysql;