项目完成 开始导数据 数据操作完成入数据库 同时每条数据会形成一个XML文件
用fileoutputStream 写xml文件 一条写一个 这样反复的打开一个fileoutputstream 一开始前面操作都比较快 但是到了700多条的时候 速度就非常慢 每次写完 流都有正确的关闭 为什么速度会突然慢很多呢 public String createSaveFile(String formName, String sn, String fileStr)
throws IOException {
// 设置配置文件路径
PropertiesUtil
.setResourceBundleBaseName("com.sunnysoft.schss.common.properties.config");
// 根据系统设置文件保存的根路径
String rootPath = null;
String osName = System.getProperty("os.name");
if (osName != null && osName.length() >= 7
&& "windows".equalsIgnoreCase(osName.substring(0, 7))) {
rootPath = PropertiesUtil.getMessage("fileSaver.windows");
} else {
rootPath = PropertiesUtil.getMessage("fileSaver.unix");
}
int maxFileNum = Integer.parseInt(PropertiesUtil
.getMessage("maxFileNum"));
// 通用路径分隔符
String separator = File.separator;
// 保存文件目录
String saveDirPath = null;
// 保存文件路径
String filePath = null;
// 获取第一级目录
File firstDir = new File(rootPath + formName);
if (!firstDir.exists()) {
firstDir.mkdirs();
}
// 获取第二级目录
File[] secondDirs = firstDir.listFiles();
if (secondDirs != null && secondDirs.length != 0) {
for (File secondDir : secondDirs) {
File[] files = secondDir.listFiles();
if (files == null || files.length < maxFileNum) {
saveDirPath = secondDir.getPath();
break;
}
}
if (saveDirPath == null) {
File subDir = new File(firstDir.getPath() + separator
+ formName + "sub" + (secondDirs.length + 1));
subDir.mkdirs();
saveDirPath = subDir.getPath();
}
} else {
File subDir = new File(firstDir.getPath() + separator + formName
+ "sub" + 1);
subDir.mkdirs();
saveDirPath = subDir.getPath();
}
filePath = saveDirPath + separator + formName + sn + ".xml";
File xmlFile = new File(filePath);
// xmlFile.setWritable(false);
// xmlFile.setReadable(false);
// xmlFile.setExecutable(false);
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
xmlFile), "GBK");
out.write(fileStr);
out.close();
String retFilePath = filePath.replace('\\', '/').substring(
rootPath.length());
return retFilePath;
}
请问下是为什么
用fileoutputStream 写xml文件 一条写一个 这样反复的打开一个fileoutputstream 一开始前面操作都比较快 但是到了700多条的时候 速度就非常慢 每次写完 流都有正确的关闭 为什么速度会突然慢很多呢 public String createSaveFile(String formName, String sn, String fileStr)
throws IOException {
// 设置配置文件路径
PropertiesUtil
.setResourceBundleBaseName("com.sunnysoft.schss.common.properties.config");
// 根据系统设置文件保存的根路径
String rootPath = null;
String osName = System.getProperty("os.name");
if (osName != null && osName.length() >= 7
&& "windows".equalsIgnoreCase(osName.substring(0, 7))) {
rootPath = PropertiesUtil.getMessage("fileSaver.windows");
} else {
rootPath = PropertiesUtil.getMessage("fileSaver.unix");
}
int maxFileNum = Integer.parseInt(PropertiesUtil
.getMessage("maxFileNum"));
// 通用路径分隔符
String separator = File.separator;
// 保存文件目录
String saveDirPath = null;
// 保存文件路径
String filePath = null;
// 获取第一级目录
File firstDir = new File(rootPath + formName);
if (!firstDir.exists()) {
firstDir.mkdirs();
}
// 获取第二级目录
File[] secondDirs = firstDir.listFiles();
if (secondDirs != null && secondDirs.length != 0) {
for (File secondDir : secondDirs) {
File[] files = secondDir.listFiles();
if (files == null || files.length < maxFileNum) {
saveDirPath = secondDir.getPath();
break;
}
}
if (saveDirPath == null) {
File subDir = new File(firstDir.getPath() + separator
+ formName + "sub" + (secondDirs.length + 1));
subDir.mkdirs();
saveDirPath = subDir.getPath();
}
} else {
File subDir = new File(firstDir.getPath() + separator + formName
+ "sub" + 1);
subDir.mkdirs();
saveDirPath = subDir.getPath();
}
filePath = saveDirPath + separator + formName + sn + ".xml";
File xmlFile = new File(filePath);
// xmlFile.setWritable(false);
// xmlFile.setReadable(false);
// xmlFile.setExecutable(false);
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
xmlFile), "GBK");
out.write(fileStr);
out.close();
String retFilePath = filePath.replace('\\', '/').substring(
rootPath.length());
return retFilePath;
}
请问下是为什么
解决方案 »
- 高手指Initialization of bean failed; nested exception is java.lang.OutOfMemoryError
- 求救!诡异的update installer 6.0.2.7
- ssh架构里的拦截器,一般什么情况下用呢?
- 为什么这条语句总是返回false呢???
- tomcat除了问题。http://localhost:8080也访问不了不知如何解决。请指教
- 求救Gompertz曲线算法。1000分答谢。
- 请问:java.sql.RetsultSet中的.last()的操作机制是什么?此操作效率很低。
- 一个初学者的巨大烦恼-tomcat配置问题!
- 对权限做到细粒度控制的开源项目Robusta AC!
- java编译时报错:找不到符号;
- jasperreport生成报表问题
- Struts2中接收页面参数为空的问题
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFile), "GBK"));
试试,因为没有buffering的话,流内容会被直接写到文件,影响效率
xmlFile), 也需要关闭吧。
找到问题了
File[] files = secondDir.listFiles();
问题在这里当文件夹里文件写多了的时候 每次都要循环整个文件夹 在去弄数组 再达到一定数量以后 效率自然就低了下来。