How to extract starting of a String in Java

后端 未结 1 636
南方客
南方客 2021-01-26 11:16

I have a text file with more than 20,000 lines and i need to extract specific line from it. The output of this program is completely blank file.

There are 20,000 lines

相关标签:
1条回答
  • 2021-01-26 12:02

    We will assume that you use Java 7, since this is 2014.

    Here is a method which will return a List<String> where each element is an ISDN:

    private static final Pattern ISDN = Pattern.compile("ISDN=(.*)");
    
    // ...
    
    public List<String> getISDNsFromFile(final String fileName)
        throws IOException
    {
        final Path path = Paths.get(fileName);
        final List<String> ret = new ArrayList<>();
    
        Matcher m;
        String line;
    
        try (
            final BufferedReader reader
                = Files.newBufferedReader(path, StandardCharsets.UTF_8);
        ) {
            while ((line = reader.readLine()) != null) {
                m = ISDN.matcher(line);
                if (m.matches())
                    ret.add(m.group(1));
            }
        }
    
        return ret;
    }
    
    0 讨论(0)
提交回复
热议问题