1

Hey I have a txt file I wanna read only a specific string from it. The File:

This Ticket Being Generated as You Confirm

Name    : John

Movie   : Batman

Location: MidVally

Time    : 7PM-9PM

Time    : 2

Seats   : [A-4, B-3, D-5, C-1, D-2, E-3]

UniqueID: Batman7PM-9PMMidVally

I only want to retrieve the UniqueID to save it in string to be in this case String UniqID = "Batman7PM-9PMMidVally". Any idea how to achieve it

1
  • What have you tried so far? Reading a string line by line should get you started.
    – Jelle
    Commented Dec 6, 2016 at 11:45

3 Answers 3

3
public FileReader fr = new FileReader("path/to/your/file");
public BufferedReader br = new BufferedReader(fr);
String unique;
String line;
while((line =  br.readLine()) != null){
    if (line.contains("UniqueID"))
    {
        unique = line.split(":")[1].trim();
        break;
    }
}

if the file always has the same structure.

2
  • this haven't worked. The format will always be the same
    – MrRizk
    Commented Dec 6, 2016 at 12:00
  • Forgot to put it in a loop. Edited.
    – MNEkin
    Commented Dec 6, 2016 at 12:16
1
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.stream.Stream;


public class TestClass {


    private static final String FILE_PATH = "YOUR-FILE-PATH";

    public static void main(String... a) {

        String uniqID = "";


        uniqID = getValueFromFile(FILE_PATH, "UniqueID");

        System.out.println("String UniqID = \"" + uniqID + "\"");

    }

    private static String getValueFromFile(String filePath, String id) {

        HashMap<String, String> map= readFile(filePath);
        String  value=searchMap(map,id);

        return value;


    }

    private static String searchMap(HashMap<String,String> map,String id) {


        try {

            if(map.containsKey(id))return map.get(id);

        }
        catch (Exception e){
            e.printStackTrace();

        }
        return "Not Found";

    }

    private static HashMap readFile(String filePath) {
        HashMap<String, String> map = new HashMap<>();
        try {

            try (Stream<String> lines = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
                for (String line : (Iterable<String>) lines::iterator) {
                    String[] lineValues = line.split(":");
                    if (lineValues.length == 2)
                        map.put(lineValues[0].trim(), lineValues[1].trim());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

}
3
  • Could you explain your solution? Commented Dec 6, 2016 at 11:37
  • read file method read the file line by line and split it to array
    – user2058603
    Commented Dec 6, 2016 at 11:39
  • add the key first value as id in map and second key as value in the map second method -search map search for a given key if found in map return it's value
    – user2058603
    Commented Dec 6, 2016 at 11:40
0

This is a json like structure except This Ticket Being Generated as You Confirm. Try to convert that text to json then try to retrieve the as key-value pair.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.