Cannot create a .txt file

Discussion in 'Plugin Development' started by kSafin, Jan 22, 2012.

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

    kSafin

    I'm trying to make a log.txt file in my plugins data folder and I can't.

    This method checks if the file exists, and if not, creates it:
    Code:
    public void checkLog() {
            if (log.exists() == false) {
                try {
                logger.info(log.getAbsolutePath());
                log.createNewFile();
                } catch (Exception e) {
                    logger.info("[DynamicEconomy] Error creating log file.");
                    e.printStackTrace();
                }
            } else {
                logger.info("[DynamicEconomy] Log file detected.");
            }
        }
    In this code, log is
    Code:
    File log = new File(this.getDataFolder(),"log.txt");
    I know the directory is correct because before the createNewFile call, I used a logger to output the file path to console and the file path was
    "/home/29kirills9086/service6303/plugins/DynamicEconomy/log.txt"
    Which is correct.

    I'm getting an IOException as such:
    Code:
    2012-01-22 13:07:20 [SEVERE] java.io.IOException: No such file or directory
    2012-01-22 13:07:20 [SEVERE]    at java.io.UnixFileSystem.createFileExclusively(Native Method)
    2012-01-22 13:07:20 [SEVERE]    at java.io.File.createNewFile(File.java:947)
    2012-01-22 13:07:20 [SEVERE]    at me.ksafin.DynamicEconomy.Utility.checkLog(Utility.java:32)
    2012-01-22 13:07:20 [SEVERE]    at me.ksafin.DynamicEconomy.Utility.<init>(Utility.java:19)
    2012-01-22 13:07:20 [SEVERE]    at me.ksafin.DynamicEconomy.DynamicEconomy.onEnable(DynamicEconomy.java:120)
    2012-01-22 13:07:20 [SEVERE]    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:231)
    2012-01-22 13:07:20 [SEVERE]    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:1059)
    2012-01-22 13:07:20 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:253)
    2012-01-22 13:07:20 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:191)
    2012-01-22 13:07:20 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:174)
    2012-01-22 13:07:20 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:444)
    2012-01-22 13:07:20 [SEVERE]    at org.bukkit.Bukkit.reload(Bukkit.java:188)
    2012-01-22 13:07:20 [SEVERE]    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:22)
    2012-01-22 13:07:20 [SEVERE]    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
    2012-01-22 13:07:20 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:386)
    2012-01-22 13:07:20 [SEVERE]    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:777)
    2012-01-22 13:07:20 [SEVERE]    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:737)
    2012-01-22 13:07:20 [SEVERE]    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:725)
    2012-01-22 13:07:20 [SEVERE]    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
    2012-01-22 13:07:20 [SEVERE]    at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    2012-01-22 13:07:20 [SEVERE]    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:100)
    2012-01-22 13:07:20 [SEVERE]    at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    2012-01-22 13:07:20 [SEVERE]    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:536)
    2012-01-22 13:07:20 [SEVERE]    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:434)
    2012-01-22 13:07:20 [SEVERE]    at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    
     
  2. Offline

    Sir Savary

    Code:
    public void checkLog(File Log) {
            if (!Log.exists()) {
                try {
                logger.info(Log.getAbsolutePath());
                Log.createNewFile();
                } catch (Exception e) {
                    logger.info("[DynamicEconomy] Error creating log file.");
                    e.printStackTrace();
                }
            } else {
                logger.info("[DynamicEconomy] Log file detected.");
            }
        }

    That should do it, make sure to call checkLog like this: checkLog(log)
     
  3. Offline

    Technius

  4. Offline

    kSafin

    Why is this different than my code, besides passing the file?
    In my code the file is a variable of the class instead, should that make a difference?

    doesn't work. File not found exception because the file never existed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  5. Offline

    Technius

    Sorry, forgot the catch part.

    Code:java
    1.  
    2. public void checkLog()
    3. {
    4. if(!log.exists())
    5. {
    6. try
    7. {
    8. fos.flush();
    9. fos.close();
    10. }
    11. catch(IOException ioe)
    12. {
    13. System.out.println("ERROR!");
    14. }
    15. }
    16. }
    17.  


    And why would you get a FNFE if you're trying to create a file that doesn't exist? Did you even compile it? Or was the catch part incomplete?
     
  6. Offline

    kSafin

    I was never able to create the file at all, so I can't make a FOS of it.
    My problem is creating the file which doesn't seem to work.
     
  7. Offline

    Technius

    A FOS Creates the file. Maybe you're not even using the code I provided. So once again,

    Code:java
    1.  
    2. public boolean checkConfig()
    3. {
    4. File file = new File(a.getDataFolder().getPath());//a is the main class
    5. if(!file.exists())file.mkdir();
    6. File config = new File(file.getPath() + File.separator + "config.txt");
    7. try
    8. {
    9. if(!config.exists())
    10. {
    11. fos.flush();
    12. fos.close();
    13. }
    14. return true;
    15. }
    16. catch(IOException ioe)
    17. {
    18. return false;
    19. }
    20. }
    21.  


    Don't try anything else. Obviously, File.createNewFile() doesn't seem to work.

    USE THE FILE OUTPUT STREAM. Thanks.
     
  8. Offline

    kSafin

    No need to be so mean about it :p

    I don't know why but the code you initially provided didn't work, even when I added the missing catch.

    This code, however, did work, and wonderfully.

    Thank you
     
Thread Status:
Not open for further replies.

Share This Page