您的位置:首页 > 编程语言 > Java开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: