IO模型
阻塞IO(BIO)同步堵塞I/O模式,数据的读取写入必须堵塞在一个线程内等待其完成
一排水壶烧水,一个线程停留在一个水壶那知道烧开,才去处理下一个水壶。期间等待
非堵塞IO(NIO)同步支持堵塞与费堵塞模式,但是主要使用同步费堵塞IO
一个线程不断的轮询每个水壶的状态,看是否水壶状态发生了改变,从而进行下一步操作
异步IO(AIO)异步非堵塞I/O模型
为每个水壶上装了一个开关,烧开之后自动通知。
文件的复制导入到新的文件夹
复制文件时 有时候需要将文件复制到一个新的自定义文件夹,这时候找不到指定路径,就需要提前创建一个新的目录。
file1.mkdirs() 利用这个创建好全目录后 再创建文件。再移动大量文件时,单线程导致效率低下,采用多线程的线程池,有效提高效率,先创建线程池,然后再循环中执行线程池的单个线程方法。
/**
* 读取某个文件夹下的所有文件
*/
public static ArrayList<Map<String,Object>> readfile(String filepath) {
ArrayList<Map<String,Object>> list= new ArrayList();
File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("文件");
System.out.println("path=" + file.getPath());
System.out.println("absolutepath=" + file.getAbsolutePath());
System.out.println("name=" + file.getName());
if(file.getPath().split("-").length>=3) {
Map map=new HashMap();
map.put("phone", file.getPath().split("_")[5].substring(1,12));
map.put("path", file.getAbsolutePath());
list.add(map);
}
} else if (file.isDirectory()) {
System.out.println("文件夹");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
System.out.println("path=" + readfile.getPath());
System.out.println("absolutepath="
+ readfile.getAbsolutePath());
System.out.println("name=" + readfile.getName());
if(readfile.getPath().split("-").length>=3){
// if(readfile.getPath().indexOf("_") !=-1){
Map map=new HashMap();
// map.put("phone",readfile.getPath().split("_")[2].substring(0,readfile.getPath().split("_")[2].length()-4) );
map.put("phone",readfile.getPath().split("_")[5].substring(1,12));
map.put("path", readfile.getAbsolutePath());
list.add(map);
}
} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}
}
return list;
}
/**复制文件的方法*/
public static void copyFile(String oldPath, String filePath,String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
File file1=new File(filePath);
Boolean bbc=file1.mkdirs();// ture
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
ExecutorService executorService = null;
List<Map<String,Object>> applys = Dao.selectList(MAPPER+"sele");
if(CollectionUtils.isEmpty(applys)){
return 0;
}
executorService = Executors.newFixedThreadPool(applys.size() > 20 ? 20 : applys.size());
for(final Map<String,Object> apply : applys){
final String path = apply.get("PATH").toString();
final String idcardno = apply.get("IDCARDNO").toString();
final String name=path.split("\\\\")[4];
executorService.execute(new Runnable() {
@Override
public void run() {
ICBCListAction.copyFile(path,"D:\\电核信审录音\\录音\\"+idcardno+"\\U-电核录音\\U1-电核录音","D:\\电核信审录音\\录音\\"+idcardno+"\\U-电核录音\\U1-电核录音\\"+name);
}
});
}
//删除指定文件夹下所有文件
//param path 文件夹完整绝对路径
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
flag = true;
}
}
return flag;
}
文件导出方法(1)
public void exec() {
OutputStream outputStream = null;
try{
outputStream = getResponseStream(fileName);
workbook.write(outputStream);
outputStream.flush();
}catch (IOException e){
logger.error("", e);
throw new ActionException("文件导出失败");
} finally {
IOUtils.closeQuietly(outputStream);
}
}
public static OutputStream getResponseStream(String fileName) throws IOException {
String file = URLEncoder.encode(fileName + ".xls", "UTF-8").replaceAll("\\+", "%20");
HttpServletResponse response = SkyEye.getResponse();
response.reset();
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition",
"attachment; filename=\"" + file + "\"; filename*=utf-8''" + file);
response.setContentType("application/vnd.ms-excel");
return response.getOutputStream();
}