Writing to text file using PrintWriter new line

Discussion in 'Spigot Plugin Development' started by minecraftguy719, Jun 22, 2018.

  1. Hello,

    I am trying to log my staff chat using a text file, however, I have the line printing but it prints each line on top of each other.

    Any help would be appreciated,

    Thanks

    Code:

    Code (Text):

    package tk.auroraskyblock.staff.chat;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.io.UnsupportedEncodingException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import net.md_5.bungee.api.ChatColor;
    public class onChatEvent implements Listener {
       
        @EventHandler
          public void onChat(AsyncPlayerChatEvent event) throws UnsupportedEncodingException, FileNotFoundException
          {
            Player p = event.getPlayer();
            boolean b = Command.toggled.contains(p);
            String name = p.getDisplayName().toString();
            if (b == true)
            {
              String msg = event.getMessage();
              event.setCancelled(true);
              Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&4&l&oStaff Chat: &7" + name + " &7» &r" + msg), "aurora.staff.chat");
                  PrintWriter writer = new PrintWriter("plugins/AuroraStaff/stafflog.txt", "UTF-8");
                 
                  DateFormat dateFormat = new SimpleDateFormat("dd/mm/yy HH:mm:ss");
                  Calendar cal = Calendar.getInstance();
                  cal.add(Calendar.HOUR, -1);
                  cal.add(Calendar.MINUTE, -2);
                  writer.write(dateFormat.format(cal.getTime()) + " Staff Chat: " + p.getDisplayName() + " " + msg + System.getProperty("line.separator"));
                  writer.close();
                 
            } else {
                DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.HOUR, -1);
                cal.add(Calendar.MINUTE, -2);
                String msg = event.getMessage();
                  event.setCancelled(true);
                  Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&7" + dateFormat.format(cal.getTime()) + " " + name + " &7» &r" + msg), "aurora.staff.chat");
            }
          }
    }
     
     
  2. Hello, thank you for your reply I have tried using \n\r but with no success. It removes the top line.
     
  3. Firestar311

    Supporter

    If you use a BufferedWriter, it has a BufferedWriter#newLine() method.
    For my file logging stuff, that is what I use
     
  4. Hello, I have tried your suggestion but to no success.

    Thanks.
    Code:
    Code (Text):

    package tk.auroraskyblock.staff.chat;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import net.md_5.bungee.api.ChatColor;
    public class onChatEvent implements Listener {
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) throws UnsupportedEncodingException, FileNotFoundException {
            Player p = event.getPlayer();
            boolean b = Command.toggled.contains(p);
            String name = p.getDisplayName().toString();
            if (b == true) {
                DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.HOUR, -1);
                cal.add(Calendar.MINUTE, -2);
                String msg = event.getMessage();
                event.setCancelled(true);
                String finalmsg = ChatColor.translateAlternateColorCodes('&', "&7" + dateFormat.format(cal.getTime()) + " &4&l&oStaff Chat: &7" + name + " &7» &r" + msg);
                Bukkit.broadcast(
                        finalmsg,
                        "aurora.staff.chat");
               
                BufferedWriter bw = null;
                FileWriter fw = null;
               
                try {
                    fw = new FileWriter("plugins/AuroraStaff/stafflog.txt");
                    bw = new BufferedWriter(fw);
                    bw.newLine();
                    bw.write(finalmsg);
                    bw.close();
                    fw.close();
                   
                } catch(IOException e) {
                    e.printStackTrace();
                }
               
               
               
            } else {
                DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.HOUR, -1);
                cal.add(Calendar.MINUTE, -2);
                String msg = event.getMessage();
                event.setCancelled(true);
                Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&',
                        "&7" + dateFormat.format(cal.getTime()) + " " + name + " &7» &r" + msg), "aurora.player");
            }
        }
    }
     
     
  5. Don't roleplay, just use:
    Code (Java):
            Logger      logger = Logger.getLogger("minecfatguy719");
            FileHandler fileHandler;

            try {
                fileHandler = new FileHandler(this.getDataFolder() + File.separator + "fatboilog.log");
                logger.addHandler(fileHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
         
            logger.log(Level.INFO, "33,000 emails Hillary deleted");
    You can also do fileHandler.setFormatter(), to set the format of the log.
     
  6. Firestar311

    Supporter

    Use the new line method after you write
     
  7. Hello, thank you for your suggestion however that did not work.

    Thanks.
     
  8. What editor are you using to view the files?
    The BufferedWriter#newLine() method normally uses the system property "line.separator" as the line separator (so "\r\n" on Windows, "\n" on other systems), so your original code should work with an editor like Notepad++