当前位置: 代码迷 >> Java相关 >> [求助]一个文件内容替换程序还有一个文件操作错误
  详细解决方案

[求助]一个文件内容替换程序还有一个文件操作错误

热度:117   发布时间:2006-12-31 15:10:21.0
[求助]一个文件内容替换程序还有一个文件操作错误

/*编写一个文件内容替换程序。要求:
(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());
}
}
}

搜索更多相关的解决方案: 文件内容  import  java  awt  

----------------解决方案--------------------------------------------------------
提示你一下

你可以把文档的内容全部读进来,然后再用 String.replaceAll(String s1,String s2);
来做
----------------解决方案--------------------------------------------------------

得到了替换后的字符串后,再写回文件中去


----------------解决方案--------------------------------------------------------

好的,我再试试


----------------解决方案--------------------------------------------------------
对了,请问如果先注释掉replace(),怎样让TextArea显示的内容与System.out.printfln中的内容一致呢?
我做了好久,也不知道怎么做,请版主看看
----------------解决方案--------------------------------------------------------
从TextArea得到
----------------解决方案--------------------------------------------------------

版主我再replace()中是用的String.replaceAll(String s1,String s2);
而提示的错误为“D:\test\FileOperate.java:108: unreported exception java.io.IOException; must be caught or declared to be thrown
replace(openFile,s1,s2);
^
1 error

处理已完成。”我有些不明白,版主能不能详细地说明一下?


----------------解决方案--------------------------------------------------------

有IOException抛出,你要么把它放到try里去,要么在你的方法中声明抛出此异常


----------------解决方案--------------------------------------------------------
还是有错。。。版主可不可以在源代码上提示一下
----------------解决方案--------------------------------------------------------
如果编译出错,编译器会提示你错在哪里,这个自己去解决


如果是运行时得不到想要的结果,说明是你的程序算法有问题
----------------解决方案--------------------------------------------------------
  相关解决方案