查找和搜索JAVA文件的方法

查找和搜索JAVA文件的方法,java,Java,我需要一个方法的帮助,该方法可以找到一个特定的文件,然后从该文件中打印出一行文本。如果控制台中键入的字符串与该行文本中的字符串匹配,则打印该行文本。例如,如果我要调用java Find ring report.txt address.txt combiness.java它应该打印如下内容: report.txt:打破了一个DVD盗录者的国际圈子 address.txt:Kris Kringle,北极 address.txt:霍默·辛普森,斯普林菲尔德 homography.java:字符串文件名

我需要一个方法的帮助,该方法可以找到一个特定的文件,然后从该文件中打印出一行文本。如果控制台中键入的字符串与该行文本中的字符串匹配,则打印该行文本。例如,如果我要调用
java Find ring report.txt address.txt combiness.java
它应该打印如下内容:

report.txt:打破了一个DVD盗录者的国际圈子

address.txt:Kris Kringle,北极

address.txt:霍默·辛普森,斯普林菲尔德

homography.java:字符串文件名

指定的单词始终是第一个命令行参数

这就是我到目前为止所做的:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


/**
 * Code for E11.8. Searches all files specified on the command line and prints out all lines     
  containing a specified word.
 * @Michael Goedken
 */
public class Find
{
   /**
      Searches file for a word, prints out all lines containing that word.
      @param wordToFind the word to find
      @param filename the filename for the file to search
   */
   public static void findAndPrint(String wordToFind, String filename)
   {
   String input = args[0];
   for (int i = 1; i < args.length; i++)
   {
       System.out.println(" File  " + args[i]);
        File one = new File(args[i]);
        Scanner in = new Scanner(one);

        while (in.hasNext())
       {
           String line = in.nextLine();
            if (line.contains(input))
            {
               System.out.println(line);
            }
       }
   }


}

   /**
      First argument of the main method should be the word to be searched
      For other arguments of the main method, store the file names to be examined
   */
public static void main(String[] args)
{
   // call findAndPrint for each text file



   }
 }
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
/**
*代码为E11.8。搜索命令行上指定的所有文件并打印出所有行
包含特定单词的。
*@Michael Goedken
*/
公共类查找
{
/**
在文件中搜索一个单词,打印出包含该单词的所有行。
@param word查找要查找的单词
@param filename要搜索的文件的文件名
*/
公共静态void findAndPrint(字符串wordToFind,字符串文件名)
{
字符串输入=args[0];
对于(int i=1;i
好的,我想我现在明白你的问题了

您可以从主菜单调用您的方法:

public static void main(String[] args) {
//may need to throw file not found exception here
        findAndPrint();
    }
然后需要从方法中删除参数:

public static void findAndPrint() throws FileNotFoundException {
        Scanner in = new Scanner(new FileReader("C:\\yourfilepath\\actualfile.txt"));
        //you can get user input etc here if necessary
        String input = "pass";
        while (in.hasNext()) {
            String line = in.nextLine();
            if (line.contains(input)) {
                System.out.println(line);
            }
        }
    }

我添加了主要论点。我还没有运行代码,所以这要由您来调试和测试

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


/**
 * Code for E11.8. Searches all files specified on the command line and prints out all lines containing a specified word.
 * @Michael Goedken
 */
public class Find {
  /** Searches file for a word, prints out all lines containing that word.
  @param wordToFind the word to find
  @param filename the filename for the file to search
  */
  public static void findAndPrint(String wordToFind, String filename) {
   System.out.println(" File  " + filename);
   File one = new File(filename);
   Scanner in;
   try {
       in = new Scanner(one);
       while (in.hasNext()) {
           String line = in.nextLine();
           if (line.contains(wordToFind)) {
               System.out.println(line);
           }
       }
   } catch (FileNotFoundException e) {
        e.printStackTrace();
   }
}




public static void main(String[] args) {
    int length = args.length;
    if (length > 0 ) {
        String wordToFind = args[0];
        // now gather file names, bypassing first string
        for (int ii = 1; ii < length; ii++) {
            findAndPrint(wordToFind, args[ii]);         
        }
    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
/**
*代码为E11.8。搜索命令行上指定的所有文件,并打印出包含指定单词的所有行。
*@Michael Goedken
*/
公共类查找{
/**在文件中搜索一个单词,打印出包含该单词的所有行。
@param word查找要查找的单词
@param filename要搜索的文件的文件名
*/
公共静态void findAndPrint(字符串wordToFind,字符串文件名){
System.out.println(“文件”+文件名);
文件一=新文件(文件名);
扫描仪输入;
试一试{
in=新扫描仪(一台);
while(在.hasNext()中){
String line=in.nextLine();
if(line.contains(wordToFind)){
系统输出打印项次(行);
}
}
}catch(filenotfounde异常){
e、 printStackTrace();
}
}
公共静态void main(字符串[]args){
int length=args.length;
如果(长度>0){
字符串wordToFind=args[0];
//现在收集文件名,绕过第一个字符串
对于(int ii=1;ii<长度;ii++){
find和print(wordToFind,args[ii]);
}
}
}

}

您正在尝试访问数组
args[]
,该数组不在函数
findAndPrint()
的范围内。您需要将
args[0]
作为参数传递给main方法中的函数调用语句:

public static void main(String[] args){
    findAndPrint(args[0], args[1]); //for report.txt
    findAndPrint(args[0], args[2]); //for address.txt
}
args
是main方法的参数。它是一个字符串数组,用于存储单个命令行输入。在您的例子中,数组
args[]
的内容是=
{“ring”、“reports.txt”、“address.txt”、“homotation.java”}

您现在可以通过以下方式修改
findAndPrint()
函数:

static void findAndPrint(String wordToFind, String filename){
    Scanner fscan = new Scanner(new File(filename));
    String str = "";
    while((str = fscan.nextLine()) != null){
        if(str.contains(wordToFind))
            System.out.println(str);
    }
}

这似乎是一个解决方案

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;


/**
 * Code for E11.8. Searches all files specified on the command line and prints     out all lines     
   containing a specified word.
 * @Michael Goedken
 */
public class Find
{
   /**
   Searches file for a word, prints out all lines containing that word.
  @param wordToFind the word to find
  @param filename the filename for the file to search
   */
   public static void findAndPrint(String wordToFind, String filename) throws FileNotFoundException
{
   Scanner in = new Scanner(new FileReader(filename));

   String input = wordToFind;
   while (in.hasNext()) 
   {
      String line = in.nextLine();
      if (line.contains(input)) 
      {
           System.out.println(line);
      }
   }
 }

/**
  First argument of the main method should be the word to be searched
  For other arguments of the main method, store the file names to be examined
*/
   public static void main(String[] args) throws FileNotFoundException
{
  // call findAndPrint for each text file

   findAndPrint("tt", "/Users/MichaelGoedken/Desktop/mary.txt");

   findAndPrint("0", "/Users/MichaelGoedken/Desktop/transactions.txt");

  }
}

运行代码时会发生什么情况?@pruntlar syntax error您尝试在
findAndPrint
方法中读取
args[0]
时没有变量;它可以在
main
中工作,因为它有一个
String[]args
参数,但是,在您的
findAndPrint
方法的上下文中,它没有意义。@Aaron如何更改我的方法,以便我可以在我的
main
中调用
findAndPrint
,而不将代码移动到
main
您的搜索词是
ring
。假设一个文件有这样一行:“一枚钻石戒指值一千美元。”输出结果会是什么?我决不建议以你解决问题的方式编写另一段代码,而不是具体解释和尝试更正当前代码。它不完全是另一段代码。它的操作代码改变了;“只有一个小小的改变,”普格亚默,这解决了你的问题吗?