java 操作文件
http://www.diybl.com/ 2008-3-27 网络 点击:
[ 评论 ]
文章搜索:
【点击打包该文章】
package com.wodexiangce;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


/** *//**
* 按一定规律替换文本中的字符
* 个人文件操作记录
*
* @author <a href="mailto:lqinglong@yahoo.cn">qinglong.lu</a>
*
* 2008-3-26
*/

public class ReplayUtil ...{

public static void main(String[] args)throws IOException...{
FileReader f = null ;
BufferedReader br = null ;
FileWriter fw = null;

try ...{
f = new FileReader("d:\e.sql");
br = new BufferedReader(f);
fw = new FileWriter("d:\c.sql");
String line = null ;
String[] temp = null ;
int v = 0 ;

while((line=br.readLine())!=null)...{

if(line.trim().length()==0)...{
v= 0 ;
}

if(line.trim().startsWith("VALUES"))...{
temp = line.split(",");
int a = line.lastIndexOf(temp[2]);
String t = line.substring(0,a)+v+
line.substring(a+temp[2].length());
v++ ;
fw.write(t+" ");

} else...{
fw.write(line+" ");
}
}
fw.flush();

} catch (FileNotFoundException e) ...{
e.printStackTrace();
}

finally...{

if(br!=null)...{
br.close();
}
if(f!=null)
f.close();

if(fw!=null)...{
fw.close() ;
}
}
}

}

package com.wodexiangce;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/** *//**
* 按一定规律替换文本中的字符
* 个人文件操作记录
*
* @author <a href="mailto:lqinglong@yahoo.cn">qinglong.lu</a>
*
* 2008-3-26
*/
public class ReplayUtil ...{

public static void main(String[] args)throws IOException...{
FileReader f = null ;
BufferedReader br = null ;
FileWriter fw = null; 
try ...{
f = new FileReader("d:\e.sql");
br = new BufferedReader(f);
fw = new FileWriter("d:\c.sql");
String line = null ;
String[] temp = null ;
int v = 0 ; 
while((line=br.readLine())!=null)...{ 
if(line.trim().length()==0)...{
v= 0 ;
}

if(line.trim().startsWith("VALUES"))...{
temp = line.split(",");
int a = line.lastIndexOf(temp[2]);
String t = line.substring(0,a)+v+
line.substring(a+temp[2].length());
v++ ;
fw.write(t+" ");

} else...{
fw.write(line+" ");
}
}
fw.flush();

} catch (FileNotFoundException e) ...{
e.printStackTrace();
}
finally...{ 
if(br!=null)...{
br.close();
}
if(f!=null)
f.close();
if(fw!=null)...{
fw.close() ;
}
}
}
}
如果图片或页面不能正常显示请点击这里 站内搜索:
推荐文章 |
