一个JAVA小问题,急求高人解决!!!
1)编写一个类FileSearch,为该类建一个静态变量count,用来对搜索到的文件进行记数,再写一个方法publicvoidsearch(Filepath,Strin...
1) 编写一个类FileSearch,为该类建一个静态变量count,用来对搜索到的文件进行记数,再写一个方法public void search(File path,String name),该方法传入两个参数,分别表示要搜索的路径和要搜索的文件名,在该方法中通过递归调用本方法来实现各级目录的搜索。
2) 在FileSearch类的main方法中创建该类的一个实例,通过File的相关方法获得操作系统所在的目录,加上”setup”作为参数传递给search方法,实现对文件的搜索。
3) 修改上述程序,实现对txt文件的搜索。
步骤:
按照内容1)编写程序FileSearch.java,然后编译成FileSearch.class文件,运行程序。。
按照内容2)编写程序FileSearch2.java,然后编译成FileSearch2.class文件,运行程序。 展开
2) 在FileSearch类的main方法中创建该类的一个实例,通过File的相关方法获得操作系统所在的目录,加上”setup”作为参数传递给search方法,实现对文件的搜索。
3) 修改上述程序,实现对txt文件的搜索。
步骤:
按照内容1)编写程序FileSearch.java,然后编译成FileSearch.class文件,运行程序。。
按照内容2)编写程序FileSearch2.java,然后编译成FileSearch2.class文件,运行程序。 展开
4个回答
展开全部
FileSearch实现了操作系统文件夹中对文件名中含有指定字符串的查找,会在控制台输出所有符合条件的文件路径并且最终输出总数。
FileSearch2实现了操作系统文件夹中所有txt文件的查找,原理相同。FileOperation 接口是辅助接口,用于通用化文件迭代
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
public class FileSearch {
private long count = 0;
public long getCount() {
return count;
}
public void search(File path, final String name) {
doWithFiles(path, new FileOperation() {
public void operate(File f) {
if (f.getName().indexOf(name) >= 0) {
count++;
System.out.println(f.getAbsolutePath());
}
}
});
}
public static void main(String[] args) throws Exception {
String winpath = getWinPath();
System.out.println(winpath);
FileSearch fileSearch = new FileSearch();
fileSearch.search(new File(winpath), "setup");
System.out.println(fileSearch.getCount());
}
private static String getWinPath() throws IOException,
InterruptedException, UnsupportedEncodingException {
Process p = Runtime.getRuntime().exec("cmd /c echo %windir%");
p.waitFor();
StringBuffer sb = new StringBuffer();
byte[] buffer = new byte[256];
int cnt = 0;
InputStream is = p.getInputStream();
while((cnt = is.read(buffer))>0) {
sb.append(new String(buffer,0,cnt,"ASCII"));
}
String winpath = sb.toString().trim();
return winpath;
}
public void doWithFiles(File f, FileOperation fo) {
if (f.isFile()) {
fo.operate(f);
} else if (f.isDirectory()) {
File[] files = f.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File ff = files[i];
doWithFiles(ff, fo);
}
}
}
}
}
class FileSearch2 {
private long count = 0;
public long getCount() {
return count;
}
public void search(File path, final String name) {
doWithFiles(path, new FileOperation() {
public void operate(File f) {
if (f.getName().endsWith(name)) {
count++;
System.out.println(f.getAbsolutePath());
}
}
});
}
public static void main(String[] args) throws Exception {
String winpath = getWinPath();
System.out.println(winpath);
FileSearch fileSearch = new FileSearch();
fileSearch.search(new File(winpath), ".txt");
System.out.println(fileSearch.getCount());
}
private static String getWinPath() throws IOException,
InterruptedException, UnsupportedEncodingException {
Process p = Runtime.getRuntime().exec("cmd /c echo %windir%");
p.waitFor();
StringBuffer sb = new StringBuffer();
byte[] buffer = new byte[256];
int cnt = 0;
InputStream is = p.getInputStream();
while((cnt = is.read(buffer))>0) {
sb.append(new String(buffer,0,cnt,"ASCII"));
}
String winpath = sb.toString().trim();
return winpath;
}
public void doWithFiles(File f, FileOperation fo) {
if (f.isFile()) {
fo.operate(f);
} else if (f.isDirectory()) {
File[] files = f.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File ff = files[i];
doWithFiles(ff, fo);
}
}
}
}
}
interface FileOperation {
public void operate(File f);
}
FileSearch2实现了操作系统文件夹中所有txt文件的查找,原理相同。FileOperation 接口是辅助接口,用于通用化文件迭代
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
public class FileSearch {
private long count = 0;
public long getCount() {
return count;
}
public void search(File path, final String name) {
doWithFiles(path, new FileOperation() {
public void operate(File f) {
if (f.getName().indexOf(name) >= 0) {
count++;
System.out.println(f.getAbsolutePath());
}
}
});
}
public static void main(String[] args) throws Exception {
String winpath = getWinPath();
System.out.println(winpath);
FileSearch fileSearch = new FileSearch();
fileSearch.search(new File(winpath), "setup");
System.out.println(fileSearch.getCount());
}
private static String getWinPath() throws IOException,
InterruptedException, UnsupportedEncodingException {
Process p = Runtime.getRuntime().exec("cmd /c echo %windir%");
p.waitFor();
StringBuffer sb = new StringBuffer();
byte[] buffer = new byte[256];
int cnt = 0;
InputStream is = p.getInputStream();
while((cnt = is.read(buffer))>0) {
sb.append(new String(buffer,0,cnt,"ASCII"));
}
String winpath = sb.toString().trim();
return winpath;
}
public void doWithFiles(File f, FileOperation fo) {
if (f.isFile()) {
fo.operate(f);
} else if (f.isDirectory()) {
File[] files = f.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File ff = files[i];
doWithFiles(ff, fo);
}
}
}
}
}
class FileSearch2 {
private long count = 0;
public long getCount() {
return count;
}
public void search(File path, final String name) {
doWithFiles(path, new FileOperation() {
public void operate(File f) {
if (f.getName().endsWith(name)) {
count++;
System.out.println(f.getAbsolutePath());
}
}
});
}
public static void main(String[] args) throws Exception {
String winpath = getWinPath();
System.out.println(winpath);
FileSearch fileSearch = new FileSearch();
fileSearch.search(new File(winpath), ".txt");
System.out.println(fileSearch.getCount());
}
private static String getWinPath() throws IOException,
InterruptedException, UnsupportedEncodingException {
Process p = Runtime.getRuntime().exec("cmd /c echo %windir%");
p.waitFor();
StringBuffer sb = new StringBuffer();
byte[] buffer = new byte[256];
int cnt = 0;
InputStream is = p.getInputStream();
while((cnt = is.read(buffer))>0) {
sb.append(new String(buffer,0,cnt,"ASCII"));
}
String winpath = sb.toString().trim();
return winpath;
}
public void doWithFiles(File f, FileOperation fo) {
if (f.isFile()) {
fo.operate(f);
} else if (f.isDirectory()) {
File[] files = f.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File ff = files[i];
doWithFiles(ff, fo);
}
}
}
}
}
interface FileOperation {
public void operate(File f);
}
展开全部
你看看这个我想应该可以写出来了,
void getfile(File fi)
{
String[] root=fi.list();
File[] roott=fi.listFiles();
for(int i=0;i<root.length;i++)
{
if(!roott[i].isDirectory()&&(roott[i].Filename==filename||roott[i].Filename.equals(filename)))
{
System.out.println(
roott[i].getPath());
return;
}
else
{
getfile(roott[i],filename);
}
}
}
}
void getfile(File fi)
{
String[] root=fi.list();
File[] roott=fi.listFiles();
for(int i=0;i<root.length;i++)
{
if(!roott[i].isDirectory()&&(roott[i].Filename==filename||roott[i].Filename.equals(filename)))
{
System.out.println(
roott[i].getPath());
return;
}
else
{
getfile(roott[i],filename);
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author ww
*
*/
public class FileSearch {
private static int count = 0;
//用来存放搜索出的文件
private static List<File> fileList = new ArrayList<File>();
/**
* 查找文件目录(path)下的文件名(不包括后缀名)为name的个数
*
* @param path
* @param name
*/
public void search(File path,String name){
if(!path.isDirectory()){ //如果搜索的不是文件夹,直接返回
return;
}
File[] files = path.listFiles();
for (File file : files) {
if(file.isFile()){
String fileName = file.getName(); //文件名,包含后缀名
//System.out.println(fileName);
if(fileName.lastIndexOf(".") != -1){
if(name.equalsIgnoreCase(fileName.substring(0,fileName.lastIndexOf(".")))){
this.setCount(this.getCount() + 1);
this.getFileList().add(file);
}
}else{
if(name.equalsIgnoreCase(fileName)){
this.setCount(this.getCount() + 1);
this.getFileList().add(file);
}
}
}else if (file.isDirectory()) {//如果为文件夹,递归本方法继续搜索
search(file,name);
}
}
}
/**
* 查找文件目录(path)下的后缀名为name的个数
*
* @param path
* @param name
*/
public void searchForSuffix(File path,String name){
if(!path.isDirectory()){ //如果搜索的不是文件夹,直接返回
return;
}
File[] files = path.listFiles();
for (File file : files) {
if(file.isFile()){
String fileName = file.getName(); //文件名,包含后缀名
if(fileName.lastIndexOf(".") != -1){
//System.out.println(fileName.substring(fileName.lastIndexOf(".")+1));
if(name.equalsIgnoreCase(fileName.substring(fileName.lastIndexOf(".")+1))){
this.setCount(this.getCount() + 1);
this.getFileList().add(file);
}
}
}else if (file.isDirectory()) {//如果为文件夹,递归本方法继续搜索
searchForSuffix(file,name);
}
}
}
public static void main(String[] args){
FileSearch fileSearch = new FileSearch();
String dir = "c:";
String name = "setup";
String suffix = "txt";
File path = new File(dir);
//实现对setup文件的搜索
fileSearch.search(path,name);
System.out.println(fileSearch.getCount());
for(File file:fileSearch.getFileList()){
System.out.println(file.getAbsolutePath());
}
fileSearch.setCount(0);
fileSearch.setFileList(new ArrayList<File>());
//实现对txt文件的搜索
fileSearch.searchForSuffix(path, suffix);
System.out.println(fileSearch.getCount());
for(File file:fileSearch.getFileList()){
System.out.println(file.getAbsolutePath());
}
}
/**
* @param count the count to set
*/
public void setCount(int count) {
FileSearch.count = count;
}
/**
* @return the count
*/
public int getCount() {
return count;
}
/**
* @param fileList the fileList to set
*/
public void setFileList(List<File> fileList) {
FileSearch.fileList = fileList;
}
/**
* @return the fileList
*/
public List<File> getFileList() {
return fileList;
}
}
import java.util.ArrayList;
import java.util.List;
/**
*
* @author ww
*
*/
public class FileSearch {
private static int count = 0;
//用来存放搜索出的文件
private static List<File> fileList = new ArrayList<File>();
/**
* 查找文件目录(path)下的文件名(不包括后缀名)为name的个数
*
* @param path
* @param name
*/
public void search(File path,String name){
if(!path.isDirectory()){ //如果搜索的不是文件夹,直接返回
return;
}
File[] files = path.listFiles();
for (File file : files) {
if(file.isFile()){
String fileName = file.getName(); //文件名,包含后缀名
//System.out.println(fileName);
if(fileName.lastIndexOf(".") != -1){
if(name.equalsIgnoreCase(fileName.substring(0,fileName.lastIndexOf(".")))){
this.setCount(this.getCount() + 1);
this.getFileList().add(file);
}
}else{
if(name.equalsIgnoreCase(fileName)){
this.setCount(this.getCount() + 1);
this.getFileList().add(file);
}
}
}else if (file.isDirectory()) {//如果为文件夹,递归本方法继续搜索
search(file,name);
}
}
}
/**
* 查找文件目录(path)下的后缀名为name的个数
*
* @param path
* @param name
*/
public void searchForSuffix(File path,String name){
if(!path.isDirectory()){ //如果搜索的不是文件夹,直接返回
return;
}
File[] files = path.listFiles();
for (File file : files) {
if(file.isFile()){
String fileName = file.getName(); //文件名,包含后缀名
if(fileName.lastIndexOf(".") != -1){
//System.out.println(fileName.substring(fileName.lastIndexOf(".")+1));
if(name.equalsIgnoreCase(fileName.substring(fileName.lastIndexOf(".")+1))){
this.setCount(this.getCount() + 1);
this.getFileList().add(file);
}
}
}else if (file.isDirectory()) {//如果为文件夹,递归本方法继续搜索
searchForSuffix(file,name);
}
}
}
public static void main(String[] args){
FileSearch fileSearch = new FileSearch();
String dir = "c:";
String name = "setup";
String suffix = "txt";
File path = new File(dir);
//实现对setup文件的搜索
fileSearch.search(path,name);
System.out.println(fileSearch.getCount());
for(File file:fileSearch.getFileList()){
System.out.println(file.getAbsolutePath());
}
fileSearch.setCount(0);
fileSearch.setFileList(new ArrayList<File>());
//实现对txt文件的搜索
fileSearch.searchForSuffix(path, suffix);
System.out.println(fileSearch.getCount());
for(File file:fileSearch.getFileList()){
System.out.println(file.getAbsolutePath());
}
}
/**
* @param count the count to set
*/
public void setCount(int count) {
FileSearch.count = count;
}
/**
* @return the count
*/
public int getCount() {
return count;
}
/**
* @param fileList the fileList to set
*/
public void setFileList(List<File> fileList) {
FileSearch.fileList = fileList;
}
/**
* @return the fileList
*/
public List<File> getFileList() {
return fileList;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
帮你顶一个。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询