Tutorial Read/Write to a file without SnakeYAML

Discussion in 'Resources' started by Skionz, Nov 25, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Skionz

    Hello, this is going to be a tutorial on how to read and write to files without using SnakeYAML. For those of you who don't know, SnakeYAML is the library Bukkit uses for its YAML files. I will be using the PHP code block just because it doesn't remove my tabs when I edit the thread.

    Reading from a file:
    Show Spoiler

    This will be a quick tutorial no how to read from a file, line by line.
    First off we will have to create our BufferedReader as so
    PHP:
    BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
    Now that we have our reader we are going to actually start iterating through each line of the file
    PHP:
    String line;
    while((
    line reader.nextLine()) != null) {
        
    //assuming the class extends JavaPlugin we print the line to the console
        
    this.getLogger().info(line);
    }
    The while loop assigns "reader.nextLine()" to the variable "line." Then we make sure that reader.nextLine() isn't null because if it is that means we are finished reading our file.

    This is basically it except we have to close our stream like this.
    PHP:
    reader.close();
    And here is our final code...
    PHP:
    BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
    String line;
    while((
    line reader.nextLine()) != null) {
        
    this.getLogger().info(line);
    }
    reader.close();

    Writing too a file:
    Show Spoiler

    In my opinion, writing to a file is more simple and here is how you would do so.
    PHP:
    BufferedWriter writer = new BufferedWriter(new FileWriter("file.txt"true));
    writer.append("My line...");
    writer.newLine();
    writer.close();
    Basically what this does is create a new BufferedWriter and FileWriter. The second parameter in FileWriter is a boolean which makes it so the file isn't reset every time we write to it. the BufferedWriter#append() method writes to the file and the BufferedWriter#newLine() method creates a blank line. That is pretty much it, all we have to do now is close the stream with BufferedWriter#close().

    You can also clear the file by creating a PrintWriter and closing the stream right away.
    PHP:
    PrintWriter writer = new PrintWriter("stuff.txt");
    writer.close();

    This can be used for easy to configure MOTDs, messages, or other things.
    NOTE: I didn't write any of this in an IDE so don't be afraid to point out syntax errors and feel free to correct any other mistakes.
     
    leon3001, teej107 and xTrollxDudex like this.
  2. Offline

    xTrollxDudex

    Skionz
    You forgot the String line; in the final code of the first spoiler

    Edit: The applicability to Bukkit is questionable, your post might be locked or removed by a moderator.
     
  3. Offline

    Skionz

    xTrollxDudex Got it, thanks for pointing that out!
    EDIT: Yea it might. I just remember seeing Essentials motd.txt and thought it was a more unique way to configure it then just a list in a config.
     
  4. Offline

    ChipDev

    Nice tutorial, lucky you all your tutorials are great..
    *Php colors hurt my eyes ;(
     
    Skionz likes this.
  5. Offline

    rbrick

    Nicely explained :)
     
    Skionz likes this.
Thread Status:
Not open for further replies.

Share This Page