Method to find string inside of the text file. Then getting the following lines up to a certain limit

前端 未结 7 558
孤街浪徒
孤街浪徒 2020-12-08 15:35

So this is what I have so far :

public String[] findStudentInfo(String studentNumber) {
                Student student = new Student();
                Scan         


        
相关标签:
7条回答
  • 2020-12-08 16:10

    You can do something like this:

    File file = new File("Student.txt");
    
    try {
        Scanner scanner = new Scanner(file);
    
        //now read the file line by line...
        int lineNum = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            lineNum++;
            if(<some condition is met for the line>) { 
                System.out.println("ho hum, i found it on line " +lineNum);
            }
        }
    } catch(FileNotFoundException e) { 
        //handle this
    }
    
    0 讨论(0)
  • 2020-12-08 16:10

    When you are reading the file, have you considered reading it line by line? This would allow you to check if your line contains the file as your are reading, and you could then perform whatever logic you needed based on that?

    Scanner scanner = new Scanner("Student.txt");
    String currentLine;
    
    while((currentLine = scanner.readLine()) != null)
    {
        if(currentLine.indexOf("Your String"))
        {
             //Perform logic
        }
    }
    

    You could use a variable to hold the line number, or you could also have a boolean indicating if you have passed the line that contains your string:

    Scanner scanner = new Scanner("Student.txt");
    String currentLine;
    int lineNumber = 0;
    Boolean passedLine = false;
    while((currentLine = scanner.readLine()) != null)
    {
        if(currentLine.indexOf("Your String"))
        {
             //Do task
             passedLine = true;
        }
        if(passedLine)
        {
           //Do other task after passing the line.
        }
        lineNumber++;
    }
    
    0 讨论(0)
  • 2020-12-08 16:10

    This will find "Mark Sagal" in Student.txt. Assuming Student.txt contains

    Student.txt

    Amir Amiri
    Mark Sagal
    Juan Delacruz
    

    Main.java

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.ArrayList;
    
    public class Main {
        public static void main(String[] args) {
            final String file = "Student.txt";
            String line = null;
            ArrayList<String> fileContents = new ArrayList<>();
    
            try {
                FileReader fReader = new FileReader(file);
                BufferedReader fileBuff = new BufferedReader(fReader);
                while ((line = fileBuff.readLine()) != null) {
                    fileContents.add(line);
                }
                fileBuff.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            System.out.println(fileContents.contains("Mark Sagal"));
        }
    }
    
    0 讨论(0)
  • 2020-12-08 16:11

    Here is a java 8 method to find a string in a text file:

    for (String toFindUrl : urlsToTest) {
            streamService(toFindUrl);
        }
    
    private void streamService(String item) {
            try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
               stream.filter(lines -> lines.contains(item))
                           .forEach(System.out::println);
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    0 讨论(0)
  • 2020-12-08 16:13

    Here is the code of TextScanner

    public class TextScanner {
    
            private static void readFile(String fileName) {
                try {
                  File file = new File("/opt/pol/data22/ds_data118/0001/0025090290/2014/12/12/0029057983.ds");
                  Scanner scanner = new Scanner(file);
                  while (scanner.hasNext()) {
                    System.out.println(scanner.next());
                  }
                  scanner.close();
                } catch (FileNotFoundException e) {
                  e.printStackTrace();
                }
              }
    
              public static void main(String[] args) {
                if (args.length != 1) {
                  System.err.println("usage: java TextScanner1"
                    + "file location");
                  System.exit(0);
                }
                readFile(args[0]);
          }
    }
    

    It will print text with delimeters

    0 讨论(0)
  • 2020-12-08 16:17

    Using the Apache Commons IO API https://commons.apache.org/proper/commons-io/ I was able to establish this using FileUtils.readFileToString(file).contains(stringToFind)

    The documentation for this function is at https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/FileUtils.html#readFileToString(java.io.File)

    0 讨论(0)
提交回复
热议问题