我需要一个查找特定文件的方法的帮助,然后从该文件中打印出一行文本。如果在控制台中键入的字符串与该行文本中的字符串匹配,则会打印文本行。例如,如果我打电话给java Find ring report.txt address.txt Homework.java
,它应该打印如下:
report.txt: has broken up an internationa ring of DVD bootleggers that
address.txt: Kris Kringle, North Pole
address.txt: Homer Simpson, Springfield
Homework.java: String file name;
指定的单词始终是第一个命令行参数。
这是我到目前为止所做的:
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
}
}
答案 0 :(得分:1)
您正在尝试访问不在函数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
是主要方法的参数。它是一个String数组,用于存储各个命令行输入。在您的情况下,数组args[]
的内容为= {"ring", "reports.txt", "address.txt", "Homework.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);
}
}
答案 1 :(得分:0)
我添加了主要论点。我没有运行代码,所以你可以调试和测试。
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]);
}
}
}
}
答案 2 :(得分:-1)
这似乎是一种解决方案!
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");
}
}
答案 3 :(得分:-2)
好的,我想我现在就得到了你的问题。
您可以从主要方式调用您的方法:
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);
}
}
}