我正在编写一些脚本来雾化我几乎每天都需要做的手动功能。
我的第一个问题是从Path和zip文件名构建一个String。 第二个问题是循环使用zip文件名(路径+ zip文件名)。
这是包含多个zip文件的目录路径:/Users/John.Smith/Desktop/Test_script/
以下是众多zip文件之一的名称:CRM_CI_20161016_000001_50661561.zip
最后,我需要遍历每个zip文件名称50661561
的目录和子字符串,以便操作它。
有人可以给我一个建议吗?
这是我下面的代码,只能操作一个zip文件:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class UnzipUtilityTest {
public static void main(String[] args) {
// unzip file
String zipFilePath = "/Users/John.Smith/Desktop/Test_script/CRM_CI_20161016_000001_50661561.zip";
String destDirectory = "/Users/John.Smith/Desktop/Test_script/test";
UnzipUtility unzipper = new UnzipUtility();
try {
unzipper.unzip(zipFilePath, destDirectory);
} catch (Exception ex) {
System.out.println("ERROR:Unzip did not work");
}
// read provider id
String old_prov_id = zipFilePath.substring(66, 74);
System.out.println("Old provider ID :"+old_prov_id );
// add +1 to provider ID
int new_provider_ID = Integer.parseInt(old_prov_id);
new_provider_ID++;
System.out.println("New provider ID :"+new_provider_ID );
// convert provider-id INT into String
String str_provider_id = Integer.toString(new_provider_ID);
System.out.println("New String provider ID :"+str_provider_id );
// concatenate two String into one
StringBuilder bufferPDF = new StringBuilder()
.append(new_provider_ID).append(".pdf");
System.out.println(bufferPDF.toString());
StringBuilder bufferXML = new StringBuilder()
.append(new_provider_ID).append(".xml");
System.out.println(bufferXML.toString());
// convert names of XML and PDF
Path sourcePDF = Paths.get("/Users/John.Smith/Desktop/Test_script/test/50661561.pdf");
try {
Files.move(sourcePDF, sourcePDF.resolveSibling(bufferPDF.toString()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Path sourceXML = Paths.get("/Users/John.Smith/Desktop/Test_script/test/50661561.xml");
try {
Files.move(sourceXML, sourceXML.resolveSibling(bufferXML.toString()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// change provider-id and filename in xml file
try {
String filepath = "/Users/John.Smith/Desktop/Test_script/test/50661562.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
// Get the root element provider-id
Node provider = doc.getElementsByTagName("provider-id").item(0);
provider.setTextContent(str_provider_id);
// Get the root element filename
Node filename = doc.getElementsByTagName("filename").item(0);
filename.setTextContent(str_provider_id);
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
System.out.println("Done");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
}
答案 0 :(得分:1)
如果你弄错了,你的主要问题是你有一个像
这样的字符串CRM_CI_20161016_000001_50661561.zip
你想要获取50661561
如果是这样,您只需使用String.lastIndexOf()方法即可。 只需做一个
String input = "CRM....
int indexOfLastUnderScore = input.lastIndexOf('_');
int indexOfZipExtension = input.lastIndexOf('.');
String substringWithNumer = input.substring(indexOfLastUnderScore+1, indexOfZipExtension);
当然,您也可以在这里转向正则表达式,但我认为从长远来看,将上述代码推送到小型辅助方法中更容易维护。
(提示:我没有通过编译器运行我的代码;所以要注意拼写错误或微妙的"关闭一个"错误;但它应该足以让你前进)
答案 1 :(得分:1)
我会稍微改变一下:
示例: