Java简单文件处理
2016-05-26 11:39
363 查看
/** * Created by z5139 on 2016/5/17 0017. */ public class IOtxt { /** * 读取数据 */ public static String ReadData(String url) { String buffer = null; try { FileReader read = new FileReader(new File(url)); StringBuffer sb = new StringBuffer(); char ch[] = new char[1024]; int d = read.read(ch); while (d != -1) { String str = new String(ch, 0, d); sb.append(str); d = read.read(ch); } buffer = sb.toString(); read.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } /** * 写入数据 */ public static boolean WriteData(String url, int content) { BufferedWriter out = null; boolean flag; try { File file = new File(url); if (!file.exists()) { file.createNewFile(); } out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file, true))); out.write(String.valueOf(content) + " "); flag = true; return flag; } catch (Exception ex) { System.out.println(ex); flag = false; return flag; } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 创建文件 */ public static boolean createFile(String url) throws Exception { boolean flag = false; File fileName = new File(url); try { if (!fileName.exists()) { fileName.createNewFile(); flag = true; } } catch (Exception e) { flag = false; Log.e("tag", "创建失败"); } return flag; } /** * String转 int 数组 */ public static int[] StringToInt(String a) { String str[] = a.trim().split("\\s"); int recordInt[] = new int[str.length]; for (int i = 0; i < str.length; i++) { recordInt[i] = Integer.parseInt(str[i]); } return recordInt; } /** * 列出指定文件夹内路径 */ public static String[] listFile(String path) { File file = new File(path); File[] tempList = file.listFiles(); String filepath[] = new String[tempList.length]; System.out.println("该目录下对象个数:" + tempList.length); for (int i = 0; i < tempList.length; i++) { if (tempList[i].isFile()) { filepath[i] = tempList[i].getName(); } } return filepath; } /** * 列出指定文件夹内时间 */ public static String[] listTime(String path) { File file = new File(path); File[] tempList = file.listFiles(); String time[] = new String[tempList.length]; System.out.println("该目录下对象个数:" + tempList.length); for (int i = 0; i < tempList.length; i++) { if (tempList[i].isFile()) { time[i] = tempList[i].getName().substring(0, 20); } } return time; } /** * 列出指定文件夹内HR */ public static String[] listHR(String path) { File file = new File(path); File[] tempList = file.listFiles(); String HR[] = new String[tempList.length]; System.out.println("该目录下对象个数:" + tempList.length); for (int i = 0; i < tempList.length; i++) { if (tempList[i].isFile()) { HR[i] = tempList[i].getName().substring(20,22); } } return HR; } //删除空白文件 public static void deleteEmpty(String path){ File file = new File(path); if (file.length() == 0){ file.delete(); } } //创建文件夹 public static void makeRootDirectory(String filePath) { File file; try { file = < 4000 span class="hljs-keyword">new File(filePath); if (!file.exists()) { file.mkdirs(); } } catch (Exception e) { Log.i("error:", e + ""); } } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树