Adding a new line to a file every time a command is executed

Discussion in 'Plugin Development' started by tommycake50, Dec 28, 2012.

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

    tommycake50

    ok so i have this code
    dont know what all of this stuff is lol
    Code:java
    1. if(args[0].equalsIgnoreCase("put") && sender.hasPermission("log.put")){
    2. try {
    3. FileWriter fw = new FileWriter(log);
    4. String msg = ChatColor.GREEN + "[" + ChatColor.AQUA+ sender.getName() + ChatColor.GREEN + "] " + ChatColor.RESET;
    5. for(int i = 1; i < args.length; i++){
    6. msg = msg.concat(args[I] + " ");[/I]
    7. [I]}[/I]
    8. [I]fw.write(msg + "\n");[/I]
    9. [I]fw.close();[/I]
    10. [I]sender.sendMessage(msg + ChatColor.GREEN + " Has been written to the Admin Log!");[/I]
    11. [I]}catch(IOException e){[/I]
    12. [I]sender.sendMessage(ChatColor.RED + "Something went wrong while attempting to write to the log!");[/I]
    13.  
    14. [I]}[/I]
    15. [I]}[/I]

    every time i use my command /log put.
    it overwrites the existing text.
    im probably doing somehting terribly wrong that i dont know about here but could someone pls help.
     
  2. Offline

    Major_Derp

    So what is it exactly your trying to do?
    I have some code that i use to add lines to a file, it looks alot simpler than this, let me get the code for you, and i can explain it.

    This Should work, i just wrote it up, didnt test it yet, so no guarantees. If theres something wrong just tell me. So just add this code inside the command parameters thing, in place of your old file create code.
    Explanation:
    So first it will create a folder in your plugins folder with the name of your plugin. Then it will create a new file log.yml
    Everytime someone does the command it will write to the log file what you wanted, and it will send them a message.
    The try and catch just mean TRY to make the file but if it cant, then catch the reason/error and it will display it.
    Again this code SHOULD work, but if it doesnt then just tell me, and i can look at in more indepth in eclipse.



    PHP:
                        Player player = (Playersender;
                        
    File file = new File(Bukkit.getPluginManager().getPlugin("PLUGINNAMEGOESHERE").getDataFolder(),"log.yml");
                            if (!
    file.exists()) {
                                try {
                                    
    String msg ChatColor.GREEN "[" ChatColor.AQUAsender.getName() + ChatColor.GREEN "] " ChatColor.RESET;
                                    
    file.createNewFile();
                                    
    PrintWriter out = new PrintWriter(file);
                                    
    out.println("[" sender.getName() + "] ");
                                    
    out.close();
                                    
    sender.sendMessage(msg ChatColor.GREEN " Has been written to the Admin Log!");
                                } catch (
    IOException e) {
                                    
    e.printStackTrace();
                                   
    sender.sendMessage(ChatColor.RED "Something went wrong while attempting to write to the log!");
                                }
                }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  3. Offline

    tommycake50

    LOL im not an idiot but i did try this method too.
    lol no no i have the file and everything its just writing to it multiple times.
    xD
    read the OP
    its supposed to write the other arguements to the end of a file every time that command is executed
    whole class
    Show Spoiler
    Code:java
    1. package me.tommycake50.alog;
    2.  
    3. import java.io.File;
    4. import java.io.FileNotFoundException;
    5. import java.io.FileWriter;
    6. import java.io.IOException;
    7. import java.util.Scanner;
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class ALog extends JavaPlugin {
    16. private File log;
    17. public void onEnable(){
    18. listFileChecks();
    19. }
    20.  
    21. //method to see if the log file exists and if not create a new one
    22. private void listFileChecks() {
    23. if(!getDataFolder().exists()){
    24. getDataFolder().mkdir();
    25. File fl = new File(getDataFolder(), "logfile.log");
    26. if(fl.exists()){
    27. log = fl;
    28. }else{
    29. try {
    30. fl.createNewFile();
    31. log = fl;
    32. } catch (IOException e) {
    33. e.printStackTrace();
    34. }
    35. }
    36. }else{
    37. File fl = new File(getDataFolder(), "logfile.log");
    38. if(fl.exists()){
    39. log = fl;
    40. }else{
    41. try {
    42. fl.createNewFile();
    43. log = fl;
    44. } catch (IOException e) {
    45. e.printStackTrace();
    46. }
    47. }
    48. }
    49. }
    50.  
    51.  
    52. @Override
    53. public boolean onCommand(CommandSender sender, Command command,
    54. String label, String[] args) {
    55. if(label.equalsIgnoreCase("log")){
    56. if(args.length == 1){
    57. if(args[0].equalsIgnoreCase("read") && sender.hasPermission("alog.read")){
    58. try {
    59. Scanner fr = new Scanner(log);
    60. while(fr.hasNextLine()){
    61. sender.sendMessage(fr.nextLine());
    62. }
    63. fr.close();
    64. } catch (FileNotFoundException e) {
    65. e.printStackTrace();
    66. }
    67. }else if(args[0].equalsIgnoreCase("help")){
    68. sender.sendMessage(ChatColor.YELLOW + "/log" + ChatColor.BLUE + " read" + ChatColor.GREEN + " -shows you everything in the log");
    69. sender.sendMessage(ChatColor.YELLOW + "/log" + ChatColor.BLUE + " put" + ChatColor.GREEN + " -writes a line to the log");
    70. sender.sendMessage(ChatColor.YELLOW + "/log" + ChatColor.BLUE + " help" + ChatColor.GREEN + " -displays this help screen");
    71. sender.sendMessage(ChatColor.YELLOW + "/log" + ChatColor.BLUE + " clear" + ChatColor.GREEN + " -clears the log file of any logs");
    72. }else if(args[0].equalsIgnoreCase("clear") && sender.hasPermission("log.clear")){
    73. resetLog(sender);
    74. }
    75. }else if(args.length >= 2){
    76. if(args[0].equalsIgnoreCase("put") && sender.hasPermission("log.put")){
    77. try {
    78. FileWriter fw = new FileWriter(log);
    79. String msg = ChatColor.GREEN + "[" + ChatColor.AQUA+ sender.getName() + ChatColor.GREEN + "] " + ChatColor.RESET;
    80. for(int i = 1; i < args.length; i++){
    81. msg = msg.concat(args[I] + " ");[/I]
    82. [I]}[/I]
    83. [I]fw.write(msg + "\n");[/I]
    84. [I]fw.close();[/I]
    85. [I]sender.sendMessage(msg + ChatColor.GREEN + " Has been written to the Admin Log!");[/I]
    86. [I]}catch(IOException e){[/I]
    87. [I]sender.sendMessage(ChatColor.RED + "Something went wrong while attempting to write to the log!");[/I]
    88.  
    89. [I]}[/I]
    90. [I]}[/I]
    91. [I]}else if(args.length == 0){[/I]
    92. [I]sender.sendMessage(ChatColor.YELLOW + "/log" + ChatColor.BLUE + " read" + ChatColor.GREEN + " -shows you everything in the log");[/I]
    93. [I]sender.sendMessage(ChatColor.YELLOW + "/log" + ChatColor.BLUE + " put" + ChatColor.GREEN + " -writes a line to the log");[/I]
    94. [I]sender.sendMessage(ChatColor.YELLOW + "/log" + ChatColor.BLUE + " help" + ChatColor.GREEN + " -displays this help screen");[/I]
    95. [I]sender.sendMessage(ChatColor.YELLOW + "/log" + ChatColor.BLUE + " clear" + ChatColor.GREEN + " -clears the log file of any logs");[/I]
    96. [I]}[/I]
    97. [I]}[/I]
    98. [I]return false;[/I]
    99. [I]}[/I]
    100.  
    101. [I]private void resetLog(CommandSender s) {[/I]
    102. [I]log.delete();[/I]
    103. [I]try {[/I]
    104. [I]log = new File(getDataFolder(), "logfile.log");[/I]
    105. [I]log.createNewFile();[/I]
    106. [I]s.sendMessage(ChatColor.GREEN + "Log cleared!");[/I]
    107. [I]} catch (Exception e1) {[/I]
    108. [I]s.sendMessage(ChatColor.RED + "Error resetting log");[/I]
    109. [I]}[/I]
    110. [I]}[/I]
    111. [I]}[/I]
    112. [I][/I]
     
  4. Offline

    Major_Derp

    So whats the thing you want to do? You want the user to execute the command, and then it outputs something to the log? Im pretty new to making bukkit plugins, so i may not know much, but i know a little, and may be able to help.
     
  5. Offline

    tommycake50

    no my problem is with java not bukkit it overwrites the line in the file rather than writing a new line.
    i should really be using printwriter tho.
    even though i tried that it still overwrote.
    so ima try a dataoutputstream
     
  6. Offline

    theguynextdoor

    This is my preferred method of writing to a file.

    Code:
    public void logMessage(File file, String string) {
      BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
      bw.append(string);
      bw.newLine();
      bw.close();
    }
    The true part in the FileWriter is to set it to append. If you do not include that, it will overwrite what is in the file.
    The bw.append method just writes to the last line of the file, which is a new line in this case because of the next line, bw.newLine();
     
  7. Offline

    tommycake50

    thanks a lot!
     
  8. Offline

    fireblast709

    Shouldn't you use Loggers for this
     
Thread Status:
Not open for further replies.

Share This Page