zoukankan      html  css  js  c++  java
  • java中IO写文件工具类

    以下是一些依据经常使用java类进行组装的对文件进行操作的类,平时,我更喜欢使用Jodd.io中提供的一些对文件的操作类,里面的方法写的简单易懂。
    当中jodd中提供的JavaUtil类中提供的方法足够我们使用,里面的方法写的很简练,比如append,read等方法,封装更好,更符合面向对象,
    这里面我写的一些方法可多都是模仿jodd,从里面进行抽取出来的。
    	/**
    	 * 获取路径目录下的全部文件
    	 * @param path
    	 * @return
    	 */
    	public static File[] getKeywordFiles(String path) {
    		File dir = new File(path);
    		if (!dir.exists())
    			return null;
    		File[] fs = dir.listFiles();
    		return fs;
    	}
    		/**
    	 * 删除目录 param folderPath 目录完整绝对路径
    	 */
    	private static void delFolder(String folderPath) {
    		try {
    			delAllFile(folderPath); // 删除完里面全部内容
    			String filePath = folderPath;
    			filePath = filePath.toString();
    			java.io.File myFilePath = new java.io.File(filePath);
    			myFilePath.delete(); // 删除空目录
    		} catch (Exception e) {
    			log.error(e);
    		}
    	}
    
    	/**
    	 * 读取一个文件
    	 * @param filePathAndName
    	 * @return
    	 * @throws IOException
    	 */
    	public static List<String> readFile(String filePathAndName)
    			throws IOException {
    		FileInputStream fis = new FileInputStream(filePathAndName);
    		InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
    		BufferedReader br = new BufferedReader(isr);
    		LineNumberReader lnr = new LineNumberReader(br);
    
    		List<String> returnValue = new ArrayList<String>();
    		int cnt = 0;
    		while (true) {
    			cnt++;
    			String tempStr = lnr.readLine();
    			if (tempStr == null)
    				break;
    			if (tempStr.length() < 2)
    				continue;
    			returnValue.add(tempStr);
    		}
    		lnr.close();
    		br.close();
    		isr.close();
    		fis.close();
    		return returnValue;
    	}
    	
    	/**
    	 * 读取一个文件,并排重后返回
    	  
    	 */
    	public static List<String> readFileNoDup(String filePathAndName)
    			throws IOException {
    		FileInputStream fis = new FileInputStream(filePathAndName);
    		InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
    		BufferedReader br = new BufferedReader(isr);
    		LineNumberReader lnr = new LineNumberReader(br);
    
    		Set<String> set = new HashSet<String>();
    		while (true) {
    			String tempStr = lnr.readLine();
    			if (tempStr == null)
    				break;
    			if (tempStr.length() < 2)
    				continue;
    			set.add(tempStr.trim());
    		}
    		lnr.close();
    		br.close();
    		isr.close();
    		fis.close();
    		List<String> returnValue = new ArrayList<String>(set.size());
    		returnValue.addAll(set);
    		return returnValue;
    	}
    	
    	/**
    	 * 加入内容到指定文件 假设该文件不存在,则创建并加入内容 假设该文件已存在,则加入内容到已有内容最后
    	 * flag为true,则向现有文件里加入内容,否则覆盖原有内容
    	 
    	 */
    	public static void writeFile(String filePathAndName, String fileContent,
    			boolean flag) throws IOException {
    		if (null == fileContent || fileContent.length() < 1)
    			return;
    		File file = new File(filePathAndName);
    
    		if (!file.exists()) {
    			file.createNewFile();
    		}
    		FileOutputStream fos = new FileOutputStream(filePathAndName, flag);
    		OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
    		osw.write(fileContent + "
    ");
    		osw.flush();
    		osw.close();
    	}
    
    	/**
    	 * 加入内容到指定文件 假设该文件不存在,则创建并加入内容 假设该文件已存在,则加入内容到已有内容最后
    	 * flag为true,则向现有文件里加入内容,否则覆盖原有内容
    	 
    	 */
    	public static void writeFile(String filePathAndName,
    			List<String> fileContent, boolean flag) throws IOException {
    		File file = new File(filePathAndName);
    
    		if (!file.exists()) {
    			file.createNewFile();
    		}
    		FileOutputStream fos = new FileOutputStream(filePathAndName, flag);
    		OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
    		for (String temp : fileContent)
    			osw.write(temp + "
    ");
    		osw.flush();
    		osw.close();
    	}
    	
    	/**
    	 * 加入内容到指定文件 假设该文件不存在,则创建并加入内容 假设该文件已存在,则加入内容到已有内容最后
    	 * flag为true,则向现有文件里加入内容,否则覆盖原有内容
    	 
    	 */
    	public static void writeFile(String filePath,String filename,
    			List<String> fileContent, boolean flag) throws IOException {
    		File file = new File(filePath);
    		
    		if(!file.exists()){
    			boolean tempFlag = file.mkdirs();
    			if(!tempFlag){
    				log.error("目录"+filePath+"创建失败");
    				return;
    			}
    		}
    		
    		file = new File(filePath,filename);
    
    		if (!file.exists()) {
    			file.createNewFile();
    		}
    		FileOutputStream fos = new FileOutputStream(filePath+filename, flag);
    		OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
    		for (String temp : fileContent)
    			osw.write(temp + "
    ");
    		osw.flush();
    		osw.close();
    	}
    

  • 相关阅读:
    sql STUFF用法
    关于原型链
    原生js事件绑定
    http常见7种请求
    关于linux的一些常用的指令
    flex布局详解
    html5 新增元素以及css3新特性
    css浮动以及清除
    css 浮动
    计算机网络
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4469921.html
Copyright © 2011-2022 走看看