-4

I want to get a specific String from a Text file.

E.g i only want to have the indicated part (3rd Line) to be saved in new File

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

My code:

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);
} 

I have everything saved in a ArrayList but how can i now

  1. Save only one element of this ArrayList
  2. Save only part of a line (the bold one) in a variable or a new file.

EDIT I solved everything Thank you very much for your help espacially @dustytrash

xAllj
  • 1
  • 5
  • How do you determine which line you want to 'save'? – dustytrash Sep 18 '18 at 17:14
  • i'm edited it now lets say the keyword is "key" and it is in the middle of a line (but the line itself can change everytime (it's a log file) So the programm save only a part of a line wich contains the keyword "key" – xAllj Sep 18 '18 at 17:20

1 Answers1

-1

You check each line to see if it contains the value you need. When you find the value you can stop scanning (Assuming you're only looking for 1 line). Then you can parse the line if needed.

The following produced 'key://xbmcbin/ is mapped to: C:\Program Files (x86)\Kodi' as per your example:

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);
} 
dustytrash
  • 1,568
  • 1
  • 10
  • 17
  • You are a god dustytrash thank you very much but what if i only want to save till: "key://xbmcbin/ is mapped to: C:\Program Files (x86)\" I dont want to save the line till the very end only till the last backslash before Kodi? – xAllj Sep 18 '18 at 17:35
  • @xAllj Just use the split function again, taking the left side. I'll edit my answer – dustytrash Sep 18 '18 at 17:41
  • i can replace the end Edit Forget about my question – xAllj Sep 18 '18 at 17:42
  • Thanks a lot can you maybe explain how exactly this line works? result = lineMustContain + nextLine.split(lineMustContain)[1].split(trimFromLine)[0]; cause it's very nice that the program is working but i dont really understand the whole line You saved 2 Strings wich contains keywords But after this i dont understand this line If you would be so kind to explain it to me. it would be very nice – xAllj Sep 18 '18 at 17:48
  • @xAllj No problem, I've updated my answer to make things a little more clear. But to answer you, the split method was taking a string, turning it into an array and taking the first (0) or second (1) part of the array. – dustytrash Sep 18 '18 at 18:05