Reading a text document

Discussion in 'Plugin Development' started by Vinceguy1, Apr 23, 2012.

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

    Vinceguy1

    Code (open)

    Code:
    package me.Vinceguy1.ChangeGameMode;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    public class Config extends Main {
        public static boolean Command()
        {
            /** ChangeGameMode Folder **/
            try{
              File ChangeGameMode = new File("plugins\\ChangeGameMode");
              if(!ChangeGameMode.exists()){
              ChangeGameMode.mkdir();
              System.out.println("New directory \"ChangeGameMode\" has been created.");
              }
            }
            catch(Exception ex)
            {
                System.out.println("Could not create \"ChangeGameMode\" directory.");
            }
              /** Config File **/
            try{
              File Config = new File("plugins\\ChangeGameMode\\Config.txt");
              if(!Config.exists()){
              Config.createNewFile();
              BufferedWriter ConfigWrite = new BufferedWriter(new FileWriter("plugins\\ChangeGameMode\\Config.txt"));
              ConfigWrite.write("ConsoleLogging=on");
              ConfigWrite.write("\r\n");
              ConfigWrite.write("AlreadyInMessages=on");
              ConfigWrite.write("\r\n");
              ConfigWrite.write("NoPermissionMessages=on");
              ConfigWrite.write("\r\n");
              ConfigWrite.write("CannotFindUserMessages=on");
              ConfigWrite.close();
              System.out.println("New file \"Config.txt\" has been created in the \"ChangeGameMode\" directory.");
              }
              else
              {
                  BufferedReader ConfigRead = new BufferedReader(new FileReader("plugins\\ChangeGameMode\\Config.txt"));
                  String ConsoleLogging = ConfigRead.readLine();
                  System.out.println(ConsoleLogging);
                  String AlreadyIn = ConfigRead.readLine();
                  String NoPermission = ConfigRead.readLine();
                  String CannotFindUser = ConfigRead.readLine();
                  ConfigRead.close();
                  /** Read Line 1 **/
                  if(ConsoleLogging == "ConsoleLogging=on"){
                      ConfigVariables.ConsoleLogging = true;
                  }
                  else if(ConsoleLogging == "ConsoleLogging=off")
                  {
                      ConfigVariables.ConsoleLogging = false;
                  }
                  if(ConsoleLogging != "ConsoleLogging=on" & ConsoleLogging != "ConsoleLogging=off")
                  {
                      System.out.println("Could not read ConsoleLogging line, defaulting to on.");
                  }
                  /** Read Line 2 **/
                  if(AlreadyIn == "AlreadyInMessages=on"){
                      ConfigVariables.AlreadyInMessages = true;
                  }
                  else if(AlreadyIn == "AlreadyInMessages=off")
                  {
                      ConfigVariables.AlreadyInMessages = false;
                  }
                  if (AlreadyIn != "AlreadyInMessages=off" & AlreadyIn != "AlreadyInMessages=on")
                  {
                      System.out.println("Could not read AlreadyInMessages line, defaulting to on.");
                  }
                  /** Read Line 3 **/
                  if(NoPermission == "NoPermissionMessages=on"){
                      ConfigVariables.NoPermissionMessages = true;
                  }
                  else if(NoPermission == "NoPermissionMessages=off")
                  {
                      ConfigVariables.NoPermissionMessages = false;
                  }
                  if(NoPermission != "NoPermissionMessages=off" & NoPermission != "NoPermissionMessages=on")
                  {
                      System.out.println("Could not read NoPermissionMessages line, defaulting to on.");
                  }
                  /** Read Line 4 **/
                  if(CannotFindUser == "CannotFindUserMessages=on"){
                      ConfigVariables.CannotFindUserMessages = true;
                  }
                  else if(CannotFindUser == "CannotFindUserMessages=off")
                  {
                      ConfigVariables.CannotFindUserMessages = false;
                  }
                  if(CannotFindUser != "CannotFindUserMessages=off" & CannotFindUser != "CannotFindUserMessages=on")
                  {
                      System.out.println("Could not read CannotFindUserMessages line, defaulting to on.");
                  }
              }
              }
                catch(Exception ex)
                {
                    System.out.println("Could not create \"Config.txt\" file.");
                }
           
         
         
            return true;
        }
    }
    

    For some reason every line says it can not read it! even though when i print it out to the console it looks perfectly fine.
     
  2. Offline

    Njol

    Your code isn't working because you have to use 'equals' to compare strings, not ==, e.g. string.equals("something").
    But I think what you want to make is a properties file. Java has a built-in class called Properties which is able to load & save such 'key=value' text files. Alternatively you could also simply use Bukkit's config system which would change the layout to "key: value", but make it easier for you, as you can get a key with e.g. getConfig().getString("console logging").
    Also you should not use '\' to separate directories. Only Windows uses this, but on Unix (Linux, Mac, etc.) this will likely not work. You should either use / or File.separator, or construct your files with new File(plugin.getDataFolder(), "filename.ext").
     
  3. Offline

    Vinceguy1

    Njol
    I was thinking thats what i had to do (the .equals) and as for the '\' i will definately change it to '/' but with bukkit's files and the properties, idk how to do that so i'll stick with this, but can mac and linux read .txt or can they only read .rtf>?
     
  4. Offline

    Njol

    You can read every file on every OS, but you might need specific software to display and/or edit the contents easily.
     
  5. Offline

    messageofdeath

    after every ConfigWrite.write("something"); put this
    ConfigWrite.newLine();
    ConfigWrite.write("something else");

    and the outcome is
    something
    something else
     
  6. Offline

    Vinceguy1

    messageofdeath
    No the problem was with reading, and its fixed now
     
Thread Status:
Not open for further replies.

Share This Page