Example usage for org.apache.commons.io FileUtils writeStringToFile

List of usage examples for org.apache.commons.io FileUtils writeStringToFile

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils writeStringToFile.

Prototype

public static void writeStringToFile(File file, String data) throws IOException 

Source Link

Document

Writes a String to a file creating the file if it does not exist using the default encoding for the VM.

Usage

From source file:lineage2.gameserver.network.telnet.commands.TelnetPerfomance.java

/**
 * Constructor for TelnetPerfomance./*from   ww w . ja v a 2 s. co  m*/
 */
public TelnetPerfomance() {
    _commands.add(new TelnetCommand("pool", "p") {
        @Override
        public String getUsage() {
            return "pool [dump]";
        }

        @Override
        public String handle(String[] args) {
            StringBuilder sb = new StringBuilder();
            if ((args.length == 0) || args[0].isEmpty()) {
                sb.append(ThreadPoolManager.getInstance().getStats());
            } else if (args[0].equals("dump") || args[0].equals("d")) {
                try {
                    new File("stats").mkdir();
                    FileUtils
                            .writeStringToFile(
                                    new File("stats/RunnableStats-" + new SimpleDateFormat("MMddHHmmss")
                                            .format(System.currentTimeMillis()) + ".txt"),
                                    RunnableStatsManager.getInstance().getStats().toString());
                    sb.append("Runnable stats saved.\n");
                } catch (IOException e) {
                    sb.append("Exception: " + e.getMessage() + "!\n");
                }
            } else {
                return null;
            }
            return sb.toString();
        }
    });
    _commands.add(new TelnetCommand("mem", "m") {
        @Override
        public String getUsage() {
            return "mem";
        }

        @Override
        public String handle(String[] args) {
            StringBuilder sb = new StringBuilder();
            sb.append(StatsUtils.getMemUsage());
            return sb.toString();
        }
    });
    _commands.add(new TelnetCommand("heap") {
        @Override
        public String getUsage() {
            return "heap [dump] <live>";
        }

        @Override
        public String handle(String[] args) {
            StringBuilder sb = new StringBuilder();
            if ((args.length == 0) || args[0].isEmpty()) {
                return null;
            } else if (args[0].equals("dump") || args[0].equals("d")) {
                try {
                    boolean live = (args.length == 2) && !args[1].isEmpty()
                            && (args[1].equals("live") || args[1].equals("l"));
                    new File("dumps").mkdir();
                    String filename = "dumps/HeapDump" + (live ? "Live" : "") + "-"
                            + new SimpleDateFormat("MMddHHmmss").format(System.currentTimeMillis()) + ".hprof";
                    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
                    HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server,
                            "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
                    bean.dumpHeap(filename, live);
                    sb.append("Heap dumped.\n");
                } catch (IOException e) {
                    sb.append("Exception: " + e.getMessage() + "!\n");
                }
            } else {
                return null;
            }
            return sb.toString();
        }
    });
    _commands.add(new TelnetCommand("threads", "t") {
        @Override
        public String getUsage() {
            return "threads [dump]";
        }

        @Override
        public String handle(String[] args) {
            StringBuilder sb = new StringBuilder();
            if ((args.length == 0) || args[0].isEmpty()) {
                sb.append(StatsUtils.getThreadStats());
            } else if (args[0].equals("dump") || args[0].equals("d")) {
                try {
                    new File("stats").mkdir();
                    FileUtils
                            .writeStringToFile(
                                    new File("stats/ThreadsDump-" + new SimpleDateFormat("MMddHHmmss")
                                            .format(System.currentTimeMillis()) + ".txt"),
                                    StatsUtils.getThreadStats(true, true, true).toString());
                    sb.append("Threads stats saved.\n");
                } catch (IOException e) {
                    sb.append("Exception: " + e.getMessage() + "!\n");
                }
            } else {
                return null;
            }
            return sb.toString();
        }
    });
    _commands.add(new TelnetCommand("gc") {
        @Override
        public String getUsage() {
            return "gc";
        }

        @Override
        public String handle(String[] args) {
            StringBuilder sb = new StringBuilder();
            sb.append(StatsUtils.getGCStats());
            return sb.toString();
        }
    });
    _commands.add(new TelnetCommand("net", "ns") {
        @Override
        public String getUsage() {
            return "net";
        }

        @Override
        public String handle(String[] args) {
            StringBuilder sb = new StringBuilder();
            sb.append(SelectorThread.getStats());
            return sb.toString();
        }
    });
    _commands.add(new TelnetCommand("pathfind", "pfs") {
        @Override
        public String getUsage() {
            return "pathfind";
        }

        @Override
        public String handle(String[] args) {
            StringBuilder sb = new StringBuilder();
            sb.append(PathFindBuffers.getStats());
            return sb.toString();
        }
    });
    _commands.add(new TelnetCommand("dbstats", "ds") {
        @Override
        public String getUsage() {
            return "dbstats";
        }

        @Override
        public String handle(String[] args) {
            StringBuilder sb = new StringBuilder();
            sb.append("Basic database usage\n");
            sb.append("=================================================\n");
            sb.append("Connections").append('\n');
            sb.append("     Busy: ........................ ")
                    .append(DatabaseFactory.getInstance().getBusyConnectionCount()).append('\n');
            sb.append("     Idle: ........................ ")
                    .append(DatabaseFactory.getInstance().getIdleConnectionCount()).append('\n');
            sb.append("Players").append('\n');
            sb.append("     Update: ...................... ").append(GameStats.getUpdatePlayerBase())
                    .append('\n');
            double cacheHitCount, cacheMissCount, cacheHitRatio;
            Cache cache;
            LiveCacheStatistics cacheStats;
            JdbcEntityStats entityStats;
            cache = ItemsDAO.getInstance().getCache();
            cacheStats = cache.getLiveCacheStatistics();
            entityStats = ItemsDAO.getInstance().getStats();
            cacheHitCount = cacheStats.getCacheHitCount();
            cacheMissCount = cacheStats.getCacheMissCount();
            cacheHitRatio = cacheHitCount / (cacheHitCount + cacheMissCount);
            sb.append("Items").append('\n');
            sb.append("     getLoadCount: ................ ").append(entityStats.getLoadCount()).append('\n');
            sb.append("     getInsertCount: .............. ").append(entityStats.getInsertCount()).append('\n');
            sb.append("     getUpdateCount: .............. ").append(entityStats.getUpdateCount()).append('\n');
            sb.append("     getDeleteCount: .............. ").append(entityStats.getDeleteCount()).append('\n');
            sb.append("Cache").append('\n');
            sb.append("     getPutCount: ................. ").append(cacheStats.getPutCount()).append('\n');
            sb.append("     getUpdateCount: .............. ").append(cacheStats.getUpdateCount()).append('\n');
            sb.append("     getRemovedCount: ............. ").append(cacheStats.getRemovedCount()).append('\n');
            sb.append("     getEvictedCount: ............. ").append(cacheStats.getEvictedCount()).append('\n');
            sb.append("     getExpiredCount: ............. ").append(cacheStats.getExpiredCount()).append('\n');
            sb.append("     getSize: ..................... ").append(cacheStats.getSize()).append('\n');
            sb.append("     getLocalHeapSize: ............. ").append(cacheStats.getLocalHeapSize())
                    .append('\n');
            sb.append("     getLocalDiskSize: ............... ").append(cacheStats.getLocalDiskSize())
                    .append('\n');
            sb.append("     cacheHitRatio: ............... ").append(String.format("%2.2f", cacheHitRatio))
                    .append('\n');
            sb.append("=================================================\n");
            cache = MailDAO.getInstance().getCache();
            cacheStats = cache.getLiveCacheStatistics();
            entityStats = MailDAO.getInstance().getStats();
            cacheHitCount = cacheStats.getCacheHitCount();
            cacheMissCount = cacheStats.getCacheMissCount();
            cacheHitRatio = cacheHitCount / (cacheHitCount + cacheMissCount);
            sb.append("Mail").append('\n');
            sb.append("     getLoadCount: ................ ").append(entityStats.getLoadCount()).append('\n');
            sb.append("     getInsertCount: .............. ").append(entityStats.getInsertCount()).append('\n');
            sb.append("     getUpdateCount: .............. ").append(entityStats.getUpdateCount()).append('\n');
            sb.append("     getDeleteCount: .............. ").append(entityStats.getDeleteCount()).append('\n');
            sb.append("Cache").append('\n');
            sb.append("     getPutCount: ................. ").append(cacheStats.getPutCount()).append('\n');
            sb.append("     getUpdateCount: .............. ").append(cacheStats.getUpdateCount()).append('\n');
            sb.append("     getRemovedCount: ............. ").append(cacheStats.getRemovedCount()).append('\n');
            sb.append("     getEvictedCount: ............. ").append(cacheStats.getEvictedCount()).append('\n');
            sb.append("     getExpiredCount: ............. ").append(cacheStats.getExpiredCount()).append('\n');
            sb.append("     getSize: ..................... ").append(cacheStats.getSize()).append('\n');
            sb.append("     getLocalHeapSize: ............. ").append(cacheStats.getLocalHeapSize())
                    .append('\n');
            sb.append("     getLocalDiskSize: ............... ").append(cacheStats.getLocalDiskSize())
                    .append('\n');
            sb.append("     cacheHitRatio: ............... ").append(String.format("%2.2f", cacheHitRatio))
                    .append('\n');
            sb.append("=================================================\n");
            return sb.toString();
        }
    });
    _commands.add(new TelnetCommand("aistats", "as") {
        @Override
        public String getUsage() {
            return "aistats";
        }

        @Override
        public String handle(String[] args) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < Config.AI_TASK_MANAGER_COUNT; i++) {
                sb.append("AiTaskManager #").append(i + 1).append('\n');
                sb.append("=================================================\n");
                sb.append(AiTaskManager.getInstance().getStats(i));
                sb.append("=================================================\n");
            }
            return sb.toString();
        }
    });
    _commands.add(new TelnetCommand("effectstats", "es") {
        @Override
        public String getUsage() {
            return "effectstats";
        }

        @Override
        public String handle(String[] args) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < Config.EFFECT_TASK_MANAGER_COUNT; i++) {
                sb.append("EffectTaskManager #").append(i + 1).append('\n');
                sb.append("=================================================\n");
                sb.append(EffectTaskManager.getInstance().getStats(i));
                sb.append("=================================================\n");
            }
            return sb.toString();
        }
    });
}

From source file:com.stoyanr.wordcounter.WordCounterPerfTest.java

private Path createTree(WordCounts wc) throws IOException {
    File dir = new File(DIR);
    WordCounterTest.deleteDir(dir);/*from  www. ja v  a  2 s.  co  m*/
    dir.mkdirs();
    for (int i = 0; i < numFiles; i++) {
        File dirx = new File(DIR + "/" + i);
        dirx.mkdirs();
        FileUtils.writeStringToFile(new File(DIR + "/" + i + "/" + FILE), createText(wc));
    }
    return Paths.get(dir.getPath());
}

From source file:com.thoughtworks.go.util.validators.LogDirectory.java

public Validation update(File log4jFile, Validation validation) {
    try {/*from   w w w  .  j  av  a  2s  .co  m*/
        String contents = FileUtils.readFileToString(log4jFile);

        for (LoggingFileTransformer transformer : transformers) {
            contents = transformer.transform(contents);
        }

        FileUtils.writeStringToFile(log4jFile, contents);
    } catch (Exception e) {
        return validation.addError(e);
    }
    return Validation.SUCCESS;
}

From source file:com.smash.revolance.ui.materials.CmdlineHelper.java

public void write(String in) throws IOException {
    FileUtils.writeStringToFile(this.in, in);
}

From source file:com.sangupta.shire.site.SiteWriter.java

/**
 * Exports the given file to the site export location
 * creating all child folders that may be necessary.
 * /*from  w  w w  . j a  va  2s  . co m*/
 * @param siteFile
 */
public void export(RenderableResource resource, String pageContents) {
    // start the export process
    String path = createBasePath(resource);
    path = siteFolder.getAbsolutePath() + File.separator + path;

    File exportFile = new File(path);

    pageContents = HtmlUtils.tidyHtml(pageContents);

    try {
        FileUtils.writeStringToFile(exportFile, pageContents);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:au.org.ala.bhl.command.PaginateDocCacheCommand.java

private void paginateFile(File file, IndexerOptions options) {
    log("Paginating file: %s", file.getAbsoluteFile());
    DocumentPaginator paginator = new DocumentPaginator();

    String dirname = String.format("%s\\%s", options.getDocCachePath(),
            file.getName().substring(0, file.getName().length() - 4));

    final File documentDir = new File(dirname);

    if (!documentDir.exists()) {
        log("Creating directory: %s", documentDir.getAbsoluteFile());
        documentDir.mkdir();//from w w  w. ja v a  2s. c om

    }

    if (documentDir.exists() && documentDir.isDirectory()) {

        paginator.paginate(file.getAbsolutePath(), new PageHandler() {

            public void handlePage(String pageId, String pageText) {
                String pagePath = String.format("%s\\%s.txt", documentDir.getAbsolutePath(), pageId);
                try {
                    File f = new File(pagePath);
                    if (!f.exists()) {
                        FileUtils.writeStringToFile(f, pageText);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        });
    } else {

    }
}

From source file:com.github.neio.filesystem.paths.TestFilePath.java

@Test
public void testHashCode() throws IOException {
    FileUtils.writeStringToFile(new File(testDir, "testFile"), "Hello World");

    Assert.assertEquals(new BigInteger("a4d55a8d778e5022fab701977c5d840bbc486d0", 16).hashCode(),
            new FilePath("./testTempDir/testFile").hashCode());
}

From source file:cache.reverseproxy.CachedResponseParser.java

public static void saveFile(String fileName, String request, boolean isSoap) throws IOException {
    String fileDir = ProxyConfig.getOutputDir(isSoap);
    File file = new File(fileDir + "/" + fileName);
    if (!file.exists()) {
        FileUtils.writeStringToFile(file, request); //, Charsets.UTF_8
    }//from w w  w  . j a va  2 s  . c  o m
}