如果想手工的将android中xml的的字符串全部提取出来是一件很头疼的事情,不尽耗时耗力还不能保证是否齐全。今天给各位带来自己原创的提取字符串的方法,希望能对各位有所帮助。
复制代码- public static void getFileInfo(File file) {
- BufferedReader br = null;
- String strXmlLine = "";
- System.out.println("<!--"+file.getName()+"-->");
- try {
- br = new BufferedReader(new FileReader(file));
- int lineNumber=0;// 第几行
- int postion=0;//按顺序出现的位置(用于命名)
- while ((strXmlLine = br.readLine()) != null) {
- lineNumber++;
- if(strXmlLine.contains("text=")&&!strXmlLine.contains("X")&&!strXmlLine.contains("\"\"")&&!strXmlLine.contains("@string"))
- {
- // System.out.println("第"+lineNumber+"行:"+strXmlLine);
- postion++;
- System.out.println(" <string name=\""+file.getName().replace(".xml","")+"_"+postion+"\">"+strXmlLine.replace("android:text=","").replace("/>","").replace(">","").replace("\"","").trim()+"</string>");
- }
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- try {
- br.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
|