Java,从文件中获取特定的字符串 [重复]

Posted

技术标签:

【中文标题】Java,从文件中获取特定的字符串 [重复]【英文标题】:Java , get specific String from File [duplicate] 【发布时间】:2019-02-22 18:51:31 【问题描述】:

我想从文本文件中获取特定的字符串。

例如,我只想将指示的部分(第 3 行)保存在新文件中

19:22:08.999 T:5804  NOTICE: Aero is enabled
19:22:08.999 T:5804  NOTICE: special://xbmc/ is mapped to: C:\Program Files (x86)\Kodi
19:22:08.999 T:5804  NOTICE: key://xbmcbin/ is mapped to: C:\Program Files (x86)\Kodi
     I want this part -----> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19:22:08.999 T:5804  NOTICE: special://xbmcbinaddons/ is mapped to: C:\Program Files (x86)\Kodi/addons

我的代码:

public static void main(String[] args) throws IOException 
    ArrayList<String> result = new ArrayList<>();
    Scanner s = null; 
    try  

        s = new Scanner(new BufferedReader(new FileReader("C:/test.txt"))); 

        while (s.hasNextLine()) 
            result.add(s.nextLine());
        
     finally 
        if (s != null)
            s.close();
        
     System.out.println(result);
 

我将所有内容都保存在 ArrayList 中,但现在我该怎么做

    仅保存此 ArrayList 的一个元素 仅将行的一部分(粗体)保存在变量或新文件中。

编辑 我解决了一切非常感谢您的帮助,尤其是@dustytrash

【问题讨论】:

如何确定要“保存”哪一行? 我正在编辑它现在可以说关键字是“key”并且它位于一行的中间(但该行本身每次都可以更改(它是一个日志文件)所以程序只保存包含关键字“key”的行的一部分 【参考方案1】:

您检查每一行以查看它是否包含您需要的值。当您找到该值时,您可以停止扫描(假设您只在寻找 1 行)。然后您可以根据需要解析该行。

根据您的示例,以下生成的 'key://xbmcbin/ 映射到:C:\Program Files (x86)\Kodi':

public static void main(String[] args) throws IOException 

    String result = "";
    Scanner s = null; 
    try 
    
        final String searchKey = "key:";
        final String trimFromLine = "Kodi";
        s = new Scanner(new BufferedReader(new FileReader("test.txt"))); 

        while (s.hasNextLine() && result.isEmpty()) 
        
            String nextLine = s.nextLine();
            if (nextLine.contains(searchKey))
            
                // Trim everything from the end, to the beginning character of our searchKey (in this case 'key:')
                result = nextLine.substring(nextLine.indexOf(searchKey));

                // Take everything from the beginning to the end of the beginning character in our trimFromLine (in the case start index of 'Kodi')
                result = result.substring(0, result.indexOf(trimFromLine));
            
        
     
    finally 
    
        if (s != null)
        
            s.close();
        
     System.out.println(result);
 

【讨论】:

你是个上帝,非常感谢你,但如果我只想保存到:“key://xbmcbin/ 映射到:C:\Program Files (x86)\”我不想要将行保存到最后只到 Kodi 之前的最后一个反斜杠? @xAllj 再次使用拆分功能,取左侧。我会编辑我的答案 我可以替换结束编辑忘记我的问题 非常感谢您能解释一下这条线的工作原理吗?结果 = lineMustContain + nextLine.split(lineMustContain)[1].split(trimFromLine)[0];因为该程序正在运行非常好,但我真的不明白整行你保存了 2 个包含关键字的字符串但是在此之后我不明白这一行如果你愿意向我解释一下。会很好 @xAllj 没问题,我已经更新了我的答案,让事情变得更加清晰。但要回答您,split 方法是获取一个字符串,将其转换为一个数组并获取数组的第一 (0) 或第二 (1) 部分。

以上是关于Java,从文件中获取特定的字符串 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

Java:从特定字符后开始的字符串中获取子字符串

如何在jQuery中的两个字符串之间获取url的特定部分[重复]

java 从字符串的特定位置获取特定字符

使用 PHP 从网页中提取特定数据 [重复]

如何从CoffeeScript中的JSON中获取数据? [重复]

Bash脚本从.txt文件中删除特定行[重复]