E文件格式: <! System=OMS Version=1.0 Code=GB2312 Data=1.0 !> <XLJH> @ 序号 名称 ... # 1 杨小猪 ... </XLJH> /** * 读文件 * eFileName : 读取文件的文件名 * node : E文件里的节点名称 */ public static List<List<String>> readEFile(String eFileName, String node){ String path = "/path..." + "/"+ eFileName; try { int startIndex = 0; int thisIndex = 0; int endIndex = 0; boolean flag = false; //文件内容的字符集 UTF8 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path),"UTF8")); String line = reader.readLine(); List<List<String>> listDatas = new ArrayList<List<String>>(); while ((line = reader.readLine()) != null && flag == false) { thisIndex++; if(line.startsWith("<"+node)){ startIndex = thisIndex; } else if(line.startsWith("</"+node)){ endIndex = thisIndex; flag = true; } else if(startIndex != 0){ String[] split = line.split("\\s+"); List<String> lineDatas = new ArrayList<String>(Arrays.asList(split)); lineDatas.remove(0);// listDatas.add(lineDatas); } } System.err.println(node+"节点标签在第"+startIndex+"-"+endIndex); reader.close(); return listDatas; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
/** * 写文件 */ public static void writeTxt(String txtInfo,String fileName) throws IOException { String filePath = "/path" + "/"+fileName; File file = new File(filePath); if (!file.exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); // write 解决中文乱码问题 // FileWriter fw = new FileWriter(file, true); // 写入文件的字符集 GBK 看需求而设定 OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(file), "GBK"); BufferedWriter bw = new BufferedWriter(fw); bw.write(txtInfo); bw.flush(); bw.close(); fw.close(); }