首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

资料读、写

2012-10-26 
文件读、写package com.fxz.fileimport java.io.BufferedReaderimport java.io.Fileimport java.io.File

文件读、写

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 blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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);}}
?

热点排行