您好,欢迎光临本网站![请登录][注册会员]  
文件名称: Java文件处理工具类--FileUtil
  所属分类: Java
  开发工具:
  文件大小: 2kb
  下载次数: 0
  上传时间: 2010-01-14
  提 供 者: hexia*****
 详细说明: package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is b ad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入流读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len = in.read(buf)) >= 0) out.write(buf, 0, len); in.close(); return out.toByteArray(); } /** * Write string content to local file. * * @param fileName - * local file name will write to * @param content * String text * @return true if success * @throws IOException */ public static boolean writeFileString(String fileName, String content) throws IOException { FileWriter fout = new FileWriter(fileName); fout.write(content); fout.close(); return true; } /** * Write string content to local file using given character encoding. * * @param fileName - * local file name will write to * @param content * String text * @param encoding * the encoding * @return true if success * @throws IOException */ public static boolean writeFileString(String fileName, String content, String encoding) throws IOException { OutputStreamWriter fout = new OutputStreamWriter(new FileOutputStream( fileName), encoding); fout.write(content); fout.close(); return true; } /** * Write binary byte array to local file. * * @param fileName - * local file name will write to * @param content * binary byte array * @return true if success * @throws IOException */ public static boolean writeFileBinary(String fileName, byte[] content) throws IOException { FileOutputStream fout = new FileOutputStream(fileName); fout.write(content); fout.close(); return true; } /** * 检查文件名是否合法.文件名字不能包含字符\/:*?"<>| * * @param fileName文件名,不包含路径 * @return boolean is valid file name */ public static boolean isValidFileName(String fileName) { boolean isValid = true; String errChar = "\\/:*?\"<>|"; // if (fileName == null || fileName.length() == 0) { isValid = false; } else { for (int i = 0; i < errChar.length(); i++) { if (fileName.indexOf(errChar.charAt(i)) != -1) { isValid = false; break; } } } return isValid; } /** * 把非法文件名转换为合法文件名. * * @param fileName * @return */ public static String replaceInvalidFileChars(String fileName) { StringBuffer out = new StringBuffer(); for (int i = 0; i < fileName.length(); i++) { char ch = fileName.charAt(i); // Replace invlid chars: \\/:*?\"<>| switch (ch) { case '\\': case '/': case ':': case '*': case '?': case '\"': case '<': case '>': case '|': out.append('_'); break; default: out.append(ch); } } return out.toString(); } /** * Convert a given file name to a URL(URI) string. * * @param fileName - * the file to parse * @return - URL string */ public static String filePathToURL(String fileName) { String fileUrl = new File(fileName).toURI().toString(); return fileUrl; } /** * Write string content to local file. * * @param fileName - * local file name will write to * @param content * String text * @return true if success * @throws IOException */ public static boolean appendFileString(String fileName, String content) throws IOException { OutputStreamWriter fout = new OutputStreamWriter(new FileOutputStream( fileName, true), "GBK"); fout.write(content); fout.close(); return true; } public static void main(String[] args) { System.out.println(replaceInvalidFileChars("http://www.abc.com/")); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 输入关键字,在本站1000多万海量源码库中尽情搜索: