文件读、写
程序员文章站
2024-03-17 11:11:34
...
package com.fxz.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fxz.vo.Adress;
/**
* 读取文件
* @author Administrator
*
*/
public class FileToReader {
/**
* 读取文件,返回对象集合
* @param filePath
* @return
*/
public static List<Adress> readFileToArray(String filePath){
List<Adress> list = new ArrayList<Adress>();
File f = new File(filePath) ;
//如果文件存在
if(f.exists()){
try {
BufferedReader reader = new BufferedReader(new FileReader(f));
String txt ;
Adress adress = null ;
while((txt = reader.readLine()) != null){
if(!"".equals(txt)){
String id = txt.substring(0, 4);
String name = txt.substring(4, txt.length());
adress = new Adress(id , name);
list.add(adress);
}
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("文件不存在2");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
System.out.println("文件不存在");
}
return list ;
}
public static void main(String args[]) {
long startTime=System.currentTimeMillis();
List<Adress> list = new ArrayList<Adress>();
list = readFileToArray("c:\\1.txt");
int i = 1 ;
for (Adress adress : list) {
System.out.println(i++ + "、 " + adress.getId() + " ===== " + adress.getName());
}
long endTime=System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间: "+(endTime - startTime)+"ms");
}
}
package com.fxz.file;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 文件写入
* @author 方小洲
*/
public class FileToWrite {
/**
* 利用FileOutputStream 写入文件
* @param filePath 文件路径
* @param content 写入的内容
* @param isCover 是否覆盖
* @return
*/
public static boolean WriteFileWithFileOutputStream(String filePath , String content , boolean isCover ){
boolean isSuccess = true ;
StringBuffer str = new StringBuffer();
File f = new File(filePath) ;
long begin = System.currentTimeMillis() ;
//存在并不覆盖,读取原文件的内容
if(!isCover && f.exists()){
try {
BufferedReader reader = new BufferedReader(new FileReader(f)); //读取文件
String tmp ;
while((tmp = reader.readLine()) != null){
str.append(tmp);
}
str.append("\n");
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
str.append(content);
//写入文件
try {
FileOutputStream fos = new FileOutputStream(f);
fos.write(str.toString().getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(end - begin);
return isSuccess;
}
/**
* 利用FileWriter 写入文件
* @param filePath 文件路径
* @param content 写入的内容
* @param isCover 是否覆盖
* @return
*/
public static boolean WriteFileWithBufferedOutputStream(String filePath , String content , boolean isCover ){
boolean isSuccess = true ;
StringBuffer str = new StringBuffer();
File f = new File(filePath) ;
//存在并不覆盖,读取原文件的内容
if(!isCover && f.exists()){
try {
BufferedReader reader = new BufferedReader(new FileReader(f)); //读取文件
String tmp ;
while((tmp = reader.readLine()) != null){
str.append(tmp);
}
str.append("\n");
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
str.append(content);
//写入文件
try {
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(f));
writer.write(str.toString().getBytes());
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
return isSuccess;
}
/**
* 利用FileWriter 写入文件
* @param filePath 文件路径
* @param content 写入的内容
* @param isCover 是否覆盖
* @return
*/
public static boolean WriteFileWithFileWriter(String filePath , String content , boolean isCover ){
boolean isSuccess = true ;
StringBuffer str = new StringBuffer();
File f = new File(filePath) ;
//存在并不覆盖,读取原文件的内容
if(!isCover && f.exists()){
try {
BufferedReader reader = new BufferedReader(new FileReader(f)); //读取文件
String tmp ;
while((tmp = reader.readLine()) != null){
str.append(tmp);
}
str.append("\n");
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
str.append(content);
//写入文件
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(f));
writer.write(str.toString());
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
return isSuccess;
}
public static void main(String[] args) {
//WriteFileWithFileWriter("c:\\3.txt", "方小洲", true);
WriteFileWithBufferedOutputStream("c:\\3.txt", "ssssssssssssssfddsf方小洲", false);
}
}
上一篇: 对文件的操作,例:txt文件
下一篇: python CSV 文件的读写