简体   繁体   English

从 java 中的文本文件打印包含特定模式的行

[英]print a line that contains a specific pattern from a text-file in java

public class Auto
{
public static void main(String [] args) {

// The name of the file to open.
 System.out.print("\nPlease enter TextfileName.txt : ");
Scanner keyboard = new Scanner(System.in); 
String fileName = keyboard.next();
int counter = 0;

  //Reading filename.text from code
  System.out.println("\nReading '"+fileName+"' from Java Code.\n");

 //Date and time stamp for the program. 
  DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  Date date = new Date();

  System.out.print("Todays date: "+dateFormat.format(date)+"\n\n\n");

// This will reference one line at a time
String line = null;
FileReader fileReader = null;

//-------------------------------------------------------TAB_1----------------------------------------------//
        System.out.println("\t\t\t\t TAB_1[Date on]\n");
try {
    // FileReader reads text files in the default encoding.
    fileReader = new FileReader(fileName);

    // Always wrap FileReader in BufferedReader.
    BufferedReader bufferedReader = new BufferedReader(fileReader);


    while((line = bufferedReader.readLine()) != null) {
        counter++;
        if(counter == 1 || counter == 3 || counter == 9)
        {
           // print out the lines above that are found in the text
           System.out.println(line);
           System.out.println("----------");
        }
    }   
  }    

      catch(FileNotFoundException ex) {
            System.out.println("Unable to open file '" + fileName + "'");                
           }
               catch(IOException ex) {
           System.out.println("Error reading file '" + fileName + "'");                  
    // Or we could just do this: 
    // ex.printStackTrace();
}
finally
{
    if(fileReader != null){
       // Always close files.
      // BufferedReader.close();            
    }
}

some matcher would help, but i`m not sure how it works一些匹配器会有所帮助,但我不确定它是如何工作的

}} }}

The one i have above is working but i want to also find a specific string anywhere in the text-file and print that line我上面的那个正在工作,但我还想在文本文件中的任何地方找到一个特定的字符串并打印该行

Just use the contains method of the String class to check for occurances of a substring inside a string.只需使用 String 类的 contains 方法来检查字符串中子字符串的出现。

while((line = bufferedReader.readLine()) != null) {
        if (line.contains("some string") {
           System.out.println(line);
           System.out.println("----------");
        }
    }   

If you wish to check multiple substrings and not just one then you should create a String array of all the substrings you want to look for and loop through them.如果您希望检查多个子字符串而不仅仅是一个,那么您应该创建一个包含您要查找的所有子字符串的 String 数组并遍历它们。

First add the following line at the beggining of the class :首先在类的开头添加以下行:

public static final String[] listOfStrings = new String[] { "substring 0", "sub string 1" , "substring 2" }; 

Replace the substrings with your own values.用您自己的值替换子字符串。

Then, loop through them to find matches:然后,循环遍历它们以查找匹配项:

   while((line = bufferedReader.readLine()) != null) {
       for (String match : listOfStrings) {        
            if (line.contains(match)) {
                System.out.println(line);
                System.out.println("----------");
                break; // No need to continue after the first match
            }
        } 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM