java问题(高手)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java问题(高手)相关的知识,希望对你有一定的参考价值。
如何将.tex里的数据写到mysql
挺复杂的啊,如果把.txt里的内容先放在xml里。然后在入库,会不会容易点。我的数据记录有3000条,而有的还乱码。
我这有代码 是我们做的工程的
给你也不一定能明白什么意思 我们用的自己的框架
告诉你思路
首先你这个.txt的文件中的内容格式必须固定
不允许错乱 发段代码 希望你能看懂思路。
public class ReadFileUtil
public final static Log log = LogFactory.getLog(ReadFileUtil.class.getName());
/**
* constructor
*
* @param pathFile
* @return
* @throws Exception
*/
public ReadFileUtil()
// read file to list memory
public List readFromFile(File pathFile)throws Exception
ArrayList m_list = new ArrayList();
ArrayList number_list = new ArrayList();
BufferedReader reader = new BufferedReader(new FileReader(pathFile));
List<Long[][]> id_count_list = new ArrayList();
Long instrumentId=0L;
Long[][] idAndUsecount=new Long[1][2];
idAndUsecount[0][0]=0L;
idAndUsecount[0][1]=0L;
String temp = "";
int loop = 0;
int index=1;
/**
* isErrorData=0 表示操作成功,无错误数据且无异常
* isErrorData=1 表示操作成功,有错误数据
* isErrorData=2 表示操作不成功,异常回滚
*/
int isErrorData=0;
/*
* while((temp=reader.readLine())!=null) loop++;
*
* System.out.println(new String(temp.getBytes("euc-kr"),"gb2312"));
*/
Session session = DatabaseFactory.getCheckDataIF().getCheckDataService().getCheckDataManager().getSession();
ItemIF itemIF =DatabaseFactory.getItemIF();
SampleInformationIF sampleInformationIF =DatabaseFactory.getSampleInfomationIF();
UsersIF usersIF = DatabaseFactory.getUsersIF();
CheckedCompanyIF checkedCompanyIF = DatabaseFactory.getCheckedCompanyIF();
InstrumentIF instrumentIF = DatabaseFactory.getInstrumentIF();
InstrumentMappingIF instrumentMappingIF = DatabaseFactory.getInstrumentMappingIF();
DictionaryIF dictionaryIF = DatabaseFactory.getDictionaryIF();
CheckDataIF checkDataIF = DatabaseFactory.getCheckDataIF();
Transaction tx = null;
CheckData checkData ;
try
while((temp=reader.readLine())!=null)
CheckData checkDatas = new CheckData();
// temp--2008-03-06 11:27:43
// /elearn/action=searchList&knSubjectTbKnTypeTNo=1,2008-03-06
// 11:27:43,127.0.0.1,1,2,1,test1,
String [] lines = temp.split("#");
Users userName = new Users();
userName.setName(lines[9]);
List userNameList = usersIF.searchUsers(userName);
if(userNameList!=null && userNameList.size()>0)
userName = (Users)userNameList.get(0);
checkDatas.setUserId(userName.getId());
else
isErrorData=1;
number_list.add(index);
index++;
continue;
checkDatas.setCheckPlace(lines[1]);
checkDatas.setCheckTime(DateFormat.parser(lines[0]));
List searchList = checkDataIF.searchCheckData(checkDatas);
if(searchList.size()!=0)
isErrorData=1;
number_list.add(index);
index++;
continue;
else
checkData = new CheckData();
if(!temp.contains("该txt文件已被处理完并插入到数据库中"))
if (lines.length == 16 )
if(lines[0]!=null && lines[0].length()> 0)
checkData.setCheckTime(DateFormat.parser(lines[0]));
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[1]!=null && lines[1].length()> 0)
checkData.setCheckPlace(lines[1]);
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[2]!=null && lines[2].length()> 0)
checkData.setCheckMethod(lines[2]);
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[3]!=null && lines[3].length()> 0)
checkData.setStandardNumber(lines[3]);
else
isErrorData=1;
number_list.add(index);
index++;
continue;
// 检索项目,如果存在获得id,否则对整个文件不做处理,
if(lines[4]!=null && lines[4].length()> 0)
Item item = new Item();
item.setName(lines[4]);
List itemList = itemIF.searchItem(item);
if(itemList!=null && itemList.size() >0)
item = (Item)itemList.get(0);
checkData.setItemId(item.getId());
else
isErrorData=1;
number_list.add(index);
index++;
continue;
else
isErrorData=1;
number_list.add(index);
index++;
continue;
// 检索样本大类,如果存在获得id,否则对整个文件不做处理,
if(lines[5]!=null && lines[5].length()> 0)
SampleInformation sampleInformation = new SampleInformation();
sampleInformation.setName(lines[5]);
List sampleInformationList = sampleInformationIF.searchSampleInformation
(sampleInformation);
sampleInformation = (SampleInformation)sampleInformationList.get(0);
Item item = new Item();
item.setName(lines[4]);
List itemList = itemIF.searchItem(item);
if(itemList!=null && itemList.size() >0)
item = (Item)itemList.get(0);
float value1=Float.parseFloat(item.getStandardValue1());
float value2=Float.parseFloat(item.getStandardValue2());
checkData.setSampleTypeId(sampleInformation.getId());
if(lines[7]!=null && lines[7].length()>0)
if(Float.parseFloat(lines[7])>value1 && Float.parseFloat(lines[7])
<value2)
checkData.setQualified(1L);
else
checkData.setQualified(2L);
else
isErrorData=1;
number_list.add(index);
index++;
continue;
else
isErrorData=1;
number_list.add(index);
index++;
continue;
// 检索样本,如果存在获得id,否则对整个文件不做处理,
if(lines[6]!=null && lines[6].length()> 0)
SampleInformation sampleInformation = new SampleInformation();
sampleInformation.setName(lines[6]);
List sampleInformationList = sampleInformationIF.searchSampleInformation
(sampleInformation);
if(sampleInformationList!=null && sampleInformationList.size() >0)
sampleInformation = (SampleInformation)sampleInformationList.get(0);
checkData.setSampleId(sampleInformation.getId());
else
isErrorData=1;
number_list.add(index);
index++;
continue;
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[7]!=null && lines[7].length()> 0)
checkData.setCheckResult(Float.parseFloat(lines[7]));
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[8]!=null && lines[8].length()> 0)
if(Enums.getInstance().getDescByName("CheckData", "CHECK_DATA_RESULT_UNIT_1").equals
("mg/L"))
checkData.setResultUnit(Enums.getInstance().getLongValueByName("CheckData",
"CHECK_DATA_RESULT_UNIT_1"));
else
if(Enums.getInstance().getDescByName("CheckData", "CHECK_DATA_RESULT_UNIT_2").equals("%"))
checkData.setResultUnit(Enums.getInstance().getLongValueByName("CheckData",
"CHECK_DATA_RESULT_UNIT_2"));
else
if(Enums.getInstance().getDescByName("CheckData", "CHECK_DATA_RESULT_UNIT_3").equals
("mg/kg"))
checkData.setResultUnit(Enums.getInstance().getLongValueByName("CheckData",
"CHECK_DATA_RESULT_UNIT_3"));
else
if(Enums.getInstance().getDescByName("CheckData", "CHECK_DATA_RESULT_UNIT_4").equals
("g/100g"))
checkData.setResultUnit(Enums.getInstance().getLongValueByName("CheckData",
"CHECK_DATA_RESULT_UNIT_4"));
else
isErrorData=1;
number_list.add(index);
index++;
continue;
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[9]!=null && lines[9].length()> 0)
Users user = new Users();
user.setName(lines[9]);
List userList = usersIF.searchUsers(user);
if(userList!=null && userList.size()>0)
user = (Users)userList.get(0);
checkData.setUserId(user.getId());
else
isErrorData=1;
number_list.add(index);
index++;
continue;
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[10]!=null && lines[10].length()> 0)
CheckedCompany checkedCompany = new CheckedCompany();
checkedCompany.setName(lines[10]);
List checkedCompanyList = checkedCompanyIF.searchCheckedCompany(checkedCompany);
if(checkedCompanyList!=null && checkedCompanyList.size()>0)
checkedCompany = (CheckedCompany)checkedCompanyList.get(0);
checkData.setCheckedCompanyId(checkedCompany.getId());
checkData.setAreaId(checkedCompany.getAreaId());
else
isErrorData=1;
number_list.add(index);
index++;
continue;
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[11]!=null && lines[11].length()> 0)
checkData.setSampleSource(lines[11]);
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[12]!=null && lines[12].length()> 0)
checkData.setSampleCount(Long.parseLong(lines[12]));
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[13]!=null && lines[13].length()> 0)
Instrument instrument = new Instrument();
InstrumentMapping instrumentMapping = new InstrumentMapping();
instrument.setSnumber(lines[13]);
List instrumentList = instrumentIF.searchInstrument(instrument);
if(instrumentList!=null && instrumentList.size()>0)
instrument = (Instrument)instrumentList.get(0);
checkData.setInstrumentId(instrument.getId());
instrumentMapping.setInstrumentId(instrument.getId());
List instrumentMappingList = instrumentMappingIF.searchInstrumentMapping(instrumentMapping);
if(instrumentMappingList!=null && instrumentMappingList.size()>0)
instrumentMapping = (InstrumentMapping)instrumentMappingList.get(0);
instrumentId=instrumentMapping.getInstrumentId();
if(id_count_list.size()<=0)
idAndUsecount[0][0]=instrumentId;
idAndUsecount[0][1]=1L;
id_count_list.add(idAndUsecount);
else
for(int i=0;i<id_count_list.size();i++)
if(id_count_list.get(i)[0][0].longValue()==instrumentId)
if(id_count_list.get(i)[0][1]==null)
id_count_list.get(i)[0][1]=1L;
else
id_count_list.get(i)[0][1]=id_count_list.get
(i)[0][1]+1L;
else
idAndUsecount[0][0]=instrumentId;
idAndUsecount[0][1]=1L;
id_count_list.add(idAndUsecount);
else
isErrorData=1;
number_list.add(index);
index++;
continue;
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[14]!=null && lines[14].length()> 0)
Dictionary dictionary = new Dictionary();
dictionary.setDetail(lines[14]);
List dictionaryList = dictionaryIF.searchDictionary(dictionary);
if(dictionaryList!=null &&dictionaryList.size() >0)
dictionary=(Dictionary)dictionaryList.get(0);
checkData.setTaskLevelId(dictionary.getId());
else
isErrorData=1;
number_list.add(index);
index++;
continue;
else
isErrorData=1;
number_list.add(index);
index++;
continue;
if(lines[15]!=null && lines[15].length()> 0)
checkData.setTreatmentResult(lines[15]);
else
isErrorData=1;
number_list.add(index);
index++;
continue;
checkData.setIsAudit(Enums.getInstance().getLongValueByName("CheckData",
"CHECK_DATA_AUDIT_NO"));
m_list.add(checkData);
index++;
loop++;
else
return null;
if (m_list.size()>0)
tx = session.beginTransaction();
for(int j=0;j<m_list.size();j++)
checkData = (CheckData)m_list.get(j);
session.save(checkData);
session.flush();
session.clear();
for(int k=0;k<id_count_list.size();k++)
// InstrumentMapping instrumentMapping = new InstrumentMapping();
// instrumentMapping = instrumentMappingIF.searchInstrumentMappingByID(id_count_list.get
//
// (k)[0][0]);
// Long useCount=instrumentMapping.getUseCount();
// if(useCount-id_count_list.get(k)[0][1]>0)
// instrumentMapping.setUseCount(useCount+id_count_list.get(k)[0][1]);
// else
// instrumentMapping.setUseCount(0L);
//
// instrumentMappingIF.updateInstrumentMapping(instrumentMapping);
Instrument instrument = new Instrument();
instrument.setSnumber(lines[13]);
List instrumentList = instrumentIF.searchInstrument(instrument);
if(instrumentList.size()>0 && instrumentList!=null)
for(int x=0;x<instrumentList.size();x++)
Instrument instruments = new Instrument();
instruments = (Instrument)instrumentList.get(x);
InstrumentMapping instrumentMapping = new InstrumentMapping();
instrumentMapping.setInstrumentId(instruments.getId());
List instrumentMappingList = instrumentMappingIF.searchInstrumentMapping(instrumentMapping);
for(int y=0;y<instrumentMappingList.size();y++)
InstrumentMapping instrumentMappings = new InstrumentMapping();
instrumentMappings = (InstrumentMapping)instrumentMappingList.get(y);
instrumentMappings.setUseCount(instrumentMappings.getUseCount()+1);
instrumentMappingIF.updateInstrumentMapping(instrumentMappings);
tx.commit();
loop=0;
id_count_list.clear();
m_list.clear();
// session.close();
// reader.close();
catch(Exception ex)
log.error("ReadFileUtil ,readFromFile,"+ex.getMessage());
if (tx != null)
tx.rollback();
if (session != null)
session.close();
if (reader != null)
reader.close();
isErrorData=2;
number_list.add(isErrorData);
return number_list;
public boolean writeToFile(File pathFile,List list) throws Exception
BufferedReader reader = new BufferedReader(new FileReader(pathFile));
String disectory=ConfigureFile.getMessage("filepath");
File tempFile = new File(disectory+"temp.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
int index=1;
boolean firstFlag=true;
String temp="";
List tempList = new ArrayList();
if(!tempFile.exists())
tempFile.mkdirs();
if(list!=null&&list.size()>0)
tempList=list.subList(0,list.size()-1);
try
while((temp=reader.readLine())!=null)
writer.append(temp);
writer.newLine();
writer.flush();
reader.close();
writer.close();
reader= new BufferedReader(new FileReader(tempFile));
writer=new BufferedWriter(new FileWriter(pathFile));
while((temp=reader.readLine())!=null)
if(list!=null && list.size() >0&&firstFlag)
int isError = Integer.parseInt(list.get(list.size()-1).toString());
if(isError==0)
writer.append("该txt文件已被处理完并插入到数据库中且数据格式正确!");
writer.newLine();
else if(isError==1)
writer.append("该txt文件已被处理完并插入到数据库中但个别数据格式不正确未作处理!\n");
writer.newLine();
else if(isError==2)
return true;
firstFlag=false;
if(tempList!=null && tempList.size()>0)
if(Integer.parseInt(tempList.get(0).toString())==index)
writer.append("**************************************************************************数据格式错误****************************************************************************\n");
writer.newLine();
writer.append(temp);
writer.newLine();
writer.append("**************************************************************************数据格式错误****************************************************************************\n");
writer.newLine();
index++;
tempList.remove(0);
else
writer.append(temp);
writer.newLine();
index++;
else
writer.append(temp);
writer.newLine();
writer.flush();
catch(Exception exc)
exc.printStackTrace();
writer.close();
return false;
reader.close();
writer.close();
return true;
// trunc str
/*
* para : 2008-03-06 11:19:44 /elearn/action=ucc return: /elearn/action=ucc
*/
private String trunc(String _str)
String retVal = "";
if (_str==null || "".equals(_str))
return retVal;
try
int start = _str.indexOf("/");
if (start>0)
retVal = _str.substring(start);
catch(Exception ex)
retVal = "";
return retVal;
以上是关于java问题(高手)的主要内容,如果未能解决你的问题,请参考以下文章