JAVA对文件操作的常用方法
2015-01-30 10:41
465 查看
JAVA对文件操作的常用方法
// 文件数据的写入 public static void StringBufferWriteFile(Map<string string=""> data,String path) throws IOException { File file = new File(path); if (!file.exists()) file.createNewFile(); FileOutputStream out = new FileOutputStream(file, true); Set<string> keyStr = data.keySet(); Iterator<string> iterator = keyStr.iterator(); String writeDate = ""; while (iterator.hasNext()) { String iStr = iterator.next(); // System.out.println(iStr); String iValue = data.get(iStr); String aa = iStr + ":" + iValue; writeDate += "\n" + aa; } StringBuffer sb = new StringBuffer(); sb.append(writeDate); out.write(sb.toString().getBytes("utf-8")); out.close(); } // 文件数据的写入 覆盖原数据 public static void StringBufferWrite(String value,String path) throws IOException { BufferedWriter out = new BufferedWriter(new FileWriter(path)); out.write(value); out.flush(); out.close(); } // 文件数据的写入 public static void StringBufferWriteFileAppend(String data,String path) throws IOException { File file = new File(path); if (!file.exists()) file.createNewFile(); FileOutputStream out = new FileOutputStream(file, true); StringBuffer sb = new StringBuffer(); sb.append("\n"+data); out.write(sb.toString().getBytes("utf-8")); out.close(); } // 读取文件 public static Vector<Vector<string>> BufferedReader(String path) throws IOException { Vector<Vector<string>> date=new Vector<Vector<string>>(); File file = new File(path); if (!file.exists() || file.isDirectory()) throw new FileNotFoundException(); BufferedReader br = new BufferedReader(new FileReader(file)); String temp = null; temp = br.readLine(); while (temp != null) { if(temp!=""){ String []arr=temp.split(","); Vector<string> ss=new Vector<string>(); for(int i=0;i<arr.length;i++){ ss.add(arr[i]); } date.add(ss); } temp = br.readLine(); } return date; } // 读取文件除了输入的数据外 public static String BufferedReaderOneWithoutOne(String value,String path) throws IOException { File file = new File(path); if (!file.exists() || file.isDirectory()) throw new FileNotFoundException(); BufferedReader br = new BufferedReader(new FileReader(file)); String temp = null; StringBuffer sb = new StringBuffer(); temp = br.readLine(); while (temp != null) { String arr[] = temp.split(","); if (!arr[0].equals(value)) { sb.append(temp + "\n"); } temp = br.readLine(); } return sb.toString(); } // 读取文件 public static Vector<Vector<string>> BufferedReaderOne(String key,String path) throws IOException { Vector<Vector<string>> date=new Vector<Vector<string>>(); File file = new File(path); if (!file.exists() || file.isDirectory()) throw new FileNotFoundException(); BufferedReader br = new BufferedReader(new FileReader(file)); String temp = null; temp = br.readLine(); while (temp != null) { String valueArr[] = temp.split(","); if (valueArr[0].equals(key)) { Vector<string> row=new Vector<string>(); for(String str:valueArr){ row.add(str); } date.add(row); return date; } else { temp = br.readLine(); } } return null; } </string></string></string></string></string></string></string></string></string></string></string></string></string>
相关文章推荐
- 操作文件处理常用方法(Java实现)
- java.io.File操作文件的一些常用方法
- Java 文件操作常用方法——File
- Java常用的一些多媒体文件基本操作方法简介
- java之File对象对文件的操作常用的几个方法(推荐)
- java:File类中对文件或目录进行操作的常用方法以及遍历文件夹下面的文件
- javabean操作文件正确,但是Jsp调用javabean时文件路径出错问题解决之JavaBean访问本地文件实现路径无关实现方法
- C#中常用的经典文件操作方法
- java常用文件操作集合类
- java中,对文件进行追加内容操作的三种方法
- java 常用文件操作
- java对文件(file)操作的各种方法
- Java日期操作常用方法征集
- 从java jar文件包中读资源的常用方法
- java 追加内容到文件末尾的几种常用方法
- Java操作文件方法
- Java日期操作常用方法
- php文件操作常用函数或方法
- 几个文件操作的java方法
- 几个文件操作的java方法