Read only a portion of a. txt file in Java

Asked

Viewed 59 times

-1

Hi, I’m developing a Discord bot and wanted it to get the Guild ID and Chat I made it write automatically when someone typed -setarbot Guild: 430119147146444807 Channel: 437245715576520716 in a file called Guilds.yml and I wanted to do something to read this file just grab the ID of the Guild and the Channel

and then return true with a Boolean.

  • 2

    The site will not provide a complete solution for you, try to put some code and exemplify the part where you are in trouble. The site people will try to help you.

1 answer

0

Depends on the file pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-yaml</artifactId>
    <version>2.3.0</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.2.3</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

Don’t forget to pass the file path correctly

  ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    try {
        Map<String, String> map = mapper.readValue(new File("guilds.yaml"), Map.class);
        System.out.println(ReflectionToStringBuilder.toString(mapToStringStyle.MULTI_LINE_STYLE));
        System.out.println(map.get("id"))
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
  • here for me did not run mapToStringStyle...

  • I use it this way: Objectmapper mapper = new Objectmapper();Map<String, String> map = mapper.readValue(new File("Guilds.json"), Map.class); with my file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.