Eaí GUJZeiros, beleza?
Então, já tive diversos problemas com o renameTo do File, porém todos foram contornados e este agora está me dando bastante dor de cabeça. Eu leio alguns arquivos XMLs e movo-os para determinadas pastas, BKP se o XML foi lido com sucesso ou ERR se caso contrário. Minha lógica para ver se o xml foi lido com sucesso é:
[code]if(f.getName().endsWith(“xml”)){
someChanged(f.getName());
boolean sucesso = false;
try {
sucesso = verificaTipoNotaFiscal(f);
} catch (ParserConfigurationException ex) {
sucesso = false;
Logger.getLogger(RetXML.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
sucesso = false;
Logger.getLogger(RetXML.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
sucesso = false;
Logger.getLogger(RetXML.class.getName()).log(Level.SEVERE, null, ex);
}
if(!sucesso){
try {
moveArquivoErro(f);
setChanged();
notifyObservers("erro");
} catch (IOException ex) {
Logger.getLogger(RetXML.class.getName()).log(Level.SEVERE, null, ex);
}
}
}[/code]
e o Método moveArquivoErro segue:
[code]private void moveArquivoErro(File file) throws IOException{
String name = file.getName();
File tmp = new File(pastaErro+name);
file = null;
boolean sucesso = file.renameTo(tmp);
}[/code]
CREIO eu, que o arquivo que está a ser movido esteja em uso… porém como saber e o que fazer para contornar?
Obrigado desde já!
Uma dica: O java não dispara exceptions por acaso.
Olá vini, obrigado por responder. Quanto as exceções, nenhuma é chamada quando eu dou o renameTo. O que eu suspeito é: quando um XML está quebrado ele continua sendo usado, por quem quer lá que seja e por isso não pode ser movido e etc.
Essa dúvida assola todos fóruns que fui e ninguém respondeu…
Tá complicado aqui.
Obrigado!
O que seria um “xml quebrado”?
E como você está tratando seu XML? Está fechando recursos no finally?
Eu Leio os XMLs no método verificaTipoNotaFiscal, segue tal método:
[code]private boolean verificaTipoNotaFiscal(File file) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = (Document) builder.parse(file);
NodeList nodeTmp = document.getElementsByTagName("nfeProc");
if(nodeTmp.getLength() == 1){
return renomeiaEMoveArquivosXML(file);
} else {
nodeTmp = document.getElementsByTagName("procCancNFe");
if(nodeTmp.getLength() == 1){
return renomeiaEMoveArquivoCancelamentoXML(file);
}
}
moveArquivoErro(file);
return true;
}[/code]
Ele cai na exception logo ali no parse, já fiz testes tirando o throw e colocando o try catch dentro desse método e anulando tais objetos, afim de saber se eram eles que estavam deixando o file em uso… tudo sem sucesso!
Isso é um xml quebrado:
<?xml version="1.0" encoding="UTF-8"?><nfeProc versao="1.10" xmlns="http://www.portalfiscal.inf.br/nfe"><NFe xmlns="http://www.portalfiscal.inf.br/nfe"><infNFe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Id="NFe23101007570682000206550010000665728800007000" versao="1.10"><ide><cUF>23</cUF><cNF>880000700</cNF><natOp>VD MERC.REGIME S.TRIBURAR</natOp><indPag>1</indPag><mod>55</mod><serie>1</serie><nNF>66572</nNF><dEmi>2010-10-14</dEmi><dSaiEnt>2010-10-14</dSaiEnt><tpNF>1</tpNF><cMunFG>2304400</cMunFG><tpImp>1</tpImp><tpEmis>1</tpEmis><cDV>0</cDV><tpAmb>1</tpAmb><finNFe>1</finNFe><procEmi>3</procEmi><verProc>1.4.2</verProc></ide><emit><CNPJ>07570682000206</CNPJ><xNome>SOLMAR DISTRIBUIDORA DE ALIMENTOS LTDA</xNome><xFant>VITAL</xFant><enderEmit><xLgr>RUA VITAL BRASIL</xLgr><nro>2740</nro><xBairro>GRANJA PORTUGAL</xBairro><cMun>2304400</cMun><xMun>FORTALEZA</xMun><UF>CE</UF><CEP>60540000</CEP><cPais>1058</cPais><fone>8531311717</fone></enderEmit><IE>060188685</IE></emit><dest><CNPJ>04968854000117</CNPJ><xNome>M & G CHURRASCARIA LTDA</xNome><enderDest><xLgr>ROD.CE 090</xLgr><nro>5188</nro><xBairro>PACHECO</xBairro><cMun>2303709</cMun><xMun>CAUCAIA</xMun><UF>CE</UF><CEP>61600000</CEP><cPais>1058</cPais><xPais>BRASIL</xPais><fone>3181825</fone></enderDest><IE>066671388</IE></dest><det nItem="1"><prod><cProd>501</cProd><cEAN/><xProd>SAL GROSSO P/CHURRASCO</xProd><CFOP>5405</CFOP><uCom>KG</uCom><qCom>30.0000</qCom><vUnCom>0.5400</vUnCom><vProd>16.20</vProd><cEANTrib/><uTrib>KG</uTrib><qTrib>30.0000</qTrib><vUnTrib>0.5400</vUnTrib></prod><imposto><ICMS><ICMS60><orig>0</orig><CST>60</CST><vBCST>0.00</vBCST><vICMSST>0.00</vICMSST></ICMS60></ICMS><PIS><PISNT><CST>07</CST></PISNT></PIS><COFINS><COFINSNT><CST>07</CST></COFINSNT></COFINS></imposto></det><det nItem="2"><prod><cProd>233</cProd><cEAN/><xProd>MAIONESE 3KG</xProd><CFOP>5405</CFOP><uCom>BD</uCom><qCom>4.0000</qCom><vUnCom>7.3000</vUnCom><vProd>29.20</vProd><cEANTrib/><uT