六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 38|回复: 0

读文件、修改文件

[复制链接]

升级  93%

145

主题

145

主题

145

主题

举人

Rank: 3Rank: 3

积分
479
 楼主| 发表于 2013-1-26 15:54:36 | 显示全部楼层 |阅读模式
/**
  * 读取txt文件
  */
public void readerTXT(){
  List<String> emailList=new ArrayList<String>();
  List<String> emailLocationList=new ArrayList<String>();
  File file=new File("C:\\Documents and Settings\\Administrator\\桌面\\email2.txt");   
  try
  {
  BufferedReader input=new BufferedReader(new FileReader(file));
  String text;
  while((text=input.readLine())!=null)
   //System.out.println(text);
  emailList.add(text);
  } catch(Exception ex) {}   
  if(emailList!=null){
   for(int i=0;i<emailList.size();i++){
    String emailStr=emailList.get(i);
    if(emailStr!=null&&!"".equals(emailStr)){
     String [] emailArray=emailStr.split(";");
     for(int j=0;j<emailArray.length;j++){
      if(emailArray[j]!=null&&!"".endsWith(emailArray[j])){
       emailLocationList.add(emailArray[j]);
      }
     }
    }
   }
  }
  
  
  for(int i=0;i<emailLocationList.size();i++){
   System.out.println(emailLocationList.get(i));
   
  }
  
}

/**
  * 写入文件
  */
public void writerFile(){
  File file=new File("C:\\Documents and Settings\\Administrator\\桌面\\发送成功.txt");
  try {
   FileWriter fw = new FileWriter(file);
   fw.write("wangwei <wangwei.0822>\r\n");
   fw.write("wangwei <wangwei.0822>\r\n");
   fw.flush();
   fw.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
}

/**
  * 文件copy
  */
public void copyFile(){
  File filei=new File("D:\\aaa.txt");
  File fileo=new File("D:\\bbb.txt");
  try {
   FileInputStream in=new FileInputStream(filei);
   FileOutputStream out=new FileOutputStream(fileo);
   byte [] b=new byte[1024];   
   while((in.read(b)>-1)){
    out.write(b);   
   }
   in.close();
   out.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表