[java]文件操作
来源:互联网 发布:top域名备案 编辑:程序博客网 时间:2024/04/30 11:23
/* * 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;}}
1 0
- java文件操作
- java文件操作大全
- java文件操作大全
- java文件操作大全
- java文件操作大全
- Java文件操作大全-
- java文件操作大全
- java文件操作大全
- Java文件操作详解
- Java文件操作大全
- Java文件操作大全
- java 文件操作大全
- Java文件操作详解
- java 文件操作大全
- Java文件操作详解
- java 文件操作大全
- Java 文件操作
- Java 文件操作
- 除皱针,有什么作用?——淄博友谊整形美容医院
- android-进阶(3)-自定义view(1)
- c++ 动态判断基类指针指向的子类类型(typeid)
- WPF使用ThreadPool.QueueUserWorkItem线程池防界面假死
- Spring MVC 返回文件
- [java]文件操作
- 快速排序
- java定时任务,每天定时执行任务
- Unity 曲线切线模式 的隐藏枚举 KeyFrame.tangentMode
- SAP Hybris企业培训
- 发
- 关于 tomcat 集群中 session 共享的三种方法
- 【排序算法2】冒泡排序
- 使用AIDL实现进程间的通信