/*编写一个文件内容替换程序。要求:
(1)允许用户选择目录;
(2)选择是否包含子目录;
(3)输入要修改的文件扩展名、要查找的字符串、替换后的字符串;
(4)将用户选择范围的所有文件打开并替换;
我已经把出错的地方给注释掉了,剩下的程序能正常运行!
但是还有个小问题,TextArea与System.out.printfln中的输出有点出入
请各位老大帮我看看啊~~~~~
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class FileOperate extends JFrame implements ActionListener{
private JButton jbSearch;
private JTextArea jtaFileList;
private JTextField jtfPath,jtfEndsChar,jtfSearch,jtfReplace;
public FileOperate(){
Container container=this.getContentPane();
JPanel panelNorth=new JPanel();
JPanel panelWest=new JPanel();
JPanel panelEast=new JPanel();
JPanel panelSouth=new JPanel();
jbSearch=new JButton("查找替换");
jbSearch.addActionListener(this);
jtaFileList=new JTextArea();
JLabel l1=new JLabel("搜索路径:");
jtfPath=new JTextField(20);
JLabel l2=new JLabel("扩展名:");
jtfEndsChar=new JTextField(16);
JLabel l3=new JLabel("查找字符串:");
jtfSearch=new JTextField(15);
JLabel l4=new JLabel("替换字符串:");
jtfReplace=new JTextField(15);
panelNorth.add(l1);
panelNorth.add(jtfPath);
panelNorth.add(l2);
panelNorth.add(jtfEndsChar);
panelNorth.add(jbSearch);
container.add(panelNorth,BorderLayout.NORTH);
panelWest.add(l3);
panelWest.add(jtfSearch);
container.add(panelWest,BorderLayout.WEST);
panelEast.add(l4);
panelEast.add(jtfReplace);
container.add(panelEast,BorderLayout.EAST);
container.add(new JScrollPane(jtaFileList,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS),BorderLayout.CENTER);
JLabel l5=new JLabel("请输入正确的文件目录信息!全部信息填写完毕后请点击查找替换按钮");
panelSouth.add(l5);
container.add(panelSouth,BorderLayout.SOUTH);
setSize(800,300);
setVisible(true);
}
private void replace(File file,String arg1,String arg2) throws IOException
{
char[] data=new char[(int)file.length()];
FileReader reader =new FileReader(file);
reader.read(data);
reader.close();
String tmp=new String(data);
System.out.println(tmp);
tmp.replaceAll(arg1,arg2);
file.createNewFile();
FileWriter writer=new FileWriter(file);
writer.write(tmp);
System.out.println(tmp);
writer.close();
}
void openForder(String FileName)
{
FileInputStream readFile;
FileOutputStream savaFile;
String newFileName;
String subFileName;
String s1;
String s2;
newFileName=FileName;
File file = new File(newFileName);
File[] fileList = file.listFiles();
int i;
String endsName=jtfEndsChar.getText();
StringBuffer sb=new StringBuffer();
sb.append(newFileName+"\n");
for(i=0;i<fileList.length;i++)
{
subFileName=fileList[i].getPath();
File isFord=new File(subFileName);
if(isFord.isDirectory())
{
openForder(subFileName);
}
if(subFileName.toUpperCase().endsWith(endsName.toUpperCase()))
{
sb.append(subFileName+"\n");
jtaFileList.setText(sb.toString());
System.out.println(subFileName+"\n");//for test
s1=jtfSearch.getText();
s2=jtfReplace.getText();
// replace(isFord,s1,s2); //就这里有问题
}
}
}
public static void main(String args[]) throws IOException
{
new FileOperate();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==jbSearch)
{
openForder(jtfPath.getText());
}
}
}