对文件的操作!!

pea 2004-07-29 08:03:04
想用JAVA实现一个功能!!
1)在本地磁盘 例如:E:\test 文件夹test中有好多文件夹,每个文件夹里又有好多不同类型的文件,如:*.bat *.java 等
2)现在想做的是:想写个程序,查找文件夹test里含有某个关键字如:value的文件(此处注意的是,此关键字不能是在注释行里,也不能是某个词的一部分,如:valuebbs)
查到之后,删除此文件,并把此文件的路径包括文件名,写入excel里。
请高手们指教!!!
不胜感激!!!
...全文
130 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pea 2004-08-08
  • 打赏
  • 举报
回复
少候结分!!
多谢支持!!
pea 2004-08-08
  • 打赏
  • 举报
回复
感谢高手门的指教
我是刚入门,这是面试的一道题,
楼上大哥看来是高手还望多多指教,
看来还得倍加努力学习呀
snap2008cn 2004-07-30
  • 打赏
  • 举报
回复
这样的问题也来问!

干脆找个抢手帮做得了!
lbl20020123 2004-07-30
  • 打赏
  • 举报
回复
import java.io.*;

public class MB2
{
public static void main(String[] args)
{
if (args.length < 1)
{
System.out.println("over. You must input one parameter at least.");
System.exit(0);
}

try
{
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));

BufferedWriter out = new BufferedWriter (new OutputStreamWriter(new FileOutputStream(args[0] + ".bak")));

int count = 0;
String sin, sout, sout2, num;

sin = in.readLine();

int first, last, off;

while (sin != null)
{
sout = sin.trim();

first = 0;
last = 0;
off = 0;
count = 0;

first = sout.indexOf(' ', 0);
if (first == -1)
{
sin = in.readLine();
continue;
}
while (first != -1)
{
last = first;
first = sout.indexOf(' ',first + 1);
}

num = sout.substring(last + 1, sout.length());


first = sout.indexOf(' ', 0);
last = sout.indexOf(' ',first + 1);

sout2 = sout.substring(first + 1, last) + "[" + num + "]," + sout.substring(0, first) +
"v(" + num + ")," ;

first = last;
last = sout.indexOf(' ',first + 1);

sout2 += sout.substring(first + 1, last);

out.write(sout2, 0, sout2.length());

out.newLine();

sin = in.readLine();
}

out.close();
in.close();

File file1 = new File(args[0]);
if (file1.exists())
{
file1.delete();
}

File file2 = new File(args[0] + ".bak");
if (file2.exists())
{
file2.renameTo(file1);
}
}
catch (Exception e)
{
System.err.println(e);
}
}
}
lbl20020123 2004-07-30
  • 打赏
  • 举报
回复
import java.io.*;

public class JavaIO
{
public static void main(String[] args)
{
String a = new String(), b = "hello";
System.out.println(a);
System.out.println(b);

/* try
{
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("c:\\aaa.txt")));
out.write("Hello,everyone bitch!");
out.close();
}
catch(Exception e)
{
System.err.println(e);
}
finally
{
System.out.println("done.");
}

*/ try
{
RandomAccessFile raf = new RandomAccessFile("c:\\aaa.txt", "rw");
System.out.println(raf.length());
String s = raf.readLine();
// s.seek(1);
// System.out.println(s);
// System.out.println(raf.getFD());
raf.seek(raf.length());
raf.writeBytes("\n" + s);
raf.close();
}
catch(Exception e)
{
System.err.println(e);
}
}
}
lbl20020123 2004-07-30
  • 打赏
  • 举报
回复
import java.io.*;
import java.util.*;

class TestDirectory
{
public static void main(String[] args)
{
if (args.length < 1)
{
System.out.println("over. You must input one parameter at least.");
System.exit(0);
}

WorkClass r1 = new WorkClass(args[0]);

Thread t1 = new Thread(r1);

t1.start();

}
}

class WorkClass implements Runnable
{
String pathName;
File myFile;
Vector v1;
public WorkClass(String path)
{
pathName = new String(path);
v1 = new Vector();
v1.add(pathName);
myFile = new File((String)v1.remove(0));
}

public void run()
{
while(myFile.isDirectory() == true)
{
System.out.println("[" + myFile.getName() + "]");
String filenameList[] = myFile.list();

for(int i = 0; i < filenameList.length; i++)
{
File file = new File(pathName + "\\" + filenameList[i]);

if (file.isDirectory())
{
v1.add(new String(pathName + "\\" + filenameList[i]));
}
else if (filenameList[i].endsWith(".sql") == true)
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pathName + "\\" + filenameList[i])));
BufferedWriter out = new BufferedWriter (new OutputStreamWriter(new FileOutputStream(pathName + "\\" + filenameList[i] + ".bak")));

String sin, sout;
sin = in.readLine();
while (sin != null)
{
if (sin.startsWith("REM") == true)
{
sout = "--" + sin;
}
else
{
sout = sin;
}

out.write(sout, 0, sout.length());
out.newLine();
sin = in.readLine();
}

out.close();
in.close();

File fileBak = new File(pathName + "\\" + filenameList[i] + ".bak");
if (file.exists())
{
file.delete();
}

if (fileBak.exists())
{
fileBak.renameTo(file);
}

System.out.println("\t" + filenameList[i] + "\t\t\t\t successful !");
}
catch(Exception e)
{
System.err.println(e);
}
}
}

if (v1.isEmpty() == true)
{
System.out.println("program end.");
break;
}
else
{
pathName = (String)v1.remove(0);
myFile = new File(pathName);
}
}
}
}
  • 打赏
  • 举报
回复
看看有关帮助吧,不难的
weimenren 2004-07-29
  • 打赏
  • 举报
回复
这东西你应该自己写,写完后,你最少对java.io包有足够的了解。 你可以看看 thinking in java 的 相关章节.



pea 2004-07-29
  • 打赏
  • 举报
回复
怎么没有人顶呢!!
难道是没有高手吗?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧