标签:java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; /** * * @author 海兰 */ public class FileUtil { public static String[] getUtfLines(String path) throws IOException { byte[] filebyte=readFile(path); String filestr=new String(filebyte,"UTF-8"); String[] rev=filestr.split("\n"); return rev; } public static String[] getLines(String path) throws IOException { byte[] filebyte=readFile(path); String filestr=new String(filebyte,"GBK"); String[] rev=filestr.split("\n"); return rev; } /** * 读文件 * * @param path * 文件名 * @return 文件内容 * @throws IOException */ public static byte[] readFile(String path) throws IOException { return readFile(path, 0); } /** * 读文件 * * @param path * 文件名 * @param offset * 偏移位置 * @return 从偏移位置开始读取的文件内容 * @throws IOException */ public static byte[] readFile(String path, long offset) throws IOException { return readFile(path, offset, -1); } /** * 读文件 * * @param path * 文件名 * @param offset * 偏移位置 * @param size * 读取大小 * @return 从偏移位置开始读取size大小的文件内容 * @throws IOException */ public static byte[] readFile(String path, long offset, int size) throws IOException { InputStream is = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); is = new FileInputStream(path); is.skip(offset); byte[] buff = new byte[4096]; int bufLength = -1; while ((bufLength = is.read(buff)) >= 0) { if (size > 0 && bufLength > size - baos.size()) { baos.write(buff, 0, size - baos.size()); break; } else { baos.write(buff, 0, bufLength); } } return baos.toByteArray(); } finally { if (is != null) { is.close(); } } } /** * 写文件 * * @param path * 文件名 * @param content * 文件内容 * @return 服务端保存文件的实际绝对路径 * @throws IOException */ public static String writeFile(String path, String content) throws IOException { byte[] matter=content.getBytes("GBK"); return writeFile(path, matter, false); } /** * 写文件 * * @param path * 文件名 * @param content * 文件内容 * @param append * 追加方式 * @return 服务端保存文件的实际绝对路径 * @throws IOException */ public static String writeFile(String path, String content,boolean append)throws IOException { byte[] matter=content.getBytes("GBK"); return writeFile(path, matter, append); } public static String writeToFile(String savePath, String content) { File f = new File(savePath); if (f.exists()) f.delete(); try { PrintStream ps = new PrintStream(savePath); String tmp[] = content.split("\n"); for (int i = 0; i < tmp.length; i++) ps.println(tmp[i]); } catch (Exception e) { e.printStackTrace(); } return ""; } /** * 写文件 * * @param path * 文件名 * @param content * 文件内容 * @return 服务端保存文件的实际绝对路径 * @throws IOException */ public static String writeFile(String path, byte[] content) throws IOException { return writeFile(path, content, false); } /** * 写文件 * * @param path * 文件名 * @param content * 文件内容 * @param append * 追加方式 * @return 服务端保存文件的实际绝对路径 * @throws IOException */ public static String writeFile(String path, byte[] content, boolean append) throws IOException { if (path == null || path.length() == 0) { path = File.createTempFile("writeServerFile", ".file") .getAbsolutePath(); } else { path = new File(path).getAbsolutePath(); } OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(path, append)); os.write(content); os.flush(); } finally { if (os != null) { os.close(); } } return path; } /** * 获得该路径下的所有文件名的列表 * @param Url * @return */ public static String[] getFileUrl(String Url) { File path = new File(Url); String[] list; list=path.list(); return list; } }
标签:java
原文地址:http://blog.csdn.net/hadoop_/article/details/46561159