Example usage for java.io FileWriter write

List of usage examples for java.io FileWriter write

Introduction

In this page you can find the example usage for java.io FileWriter write.

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:Main.java

public static void createTestDataFile(String filename, String[] lines) throws IOException {
    FileWriter writer = null;
    try {/* www .  ja  v  a  2s. com*/
        File file = new File(filename);
        file.deleteOnExit();
        writer = new FileWriter(file);
        for (String line : lines) {
            writer.write(line + "\n");
        }
    } finally {
        if (writer != null) {
            writer.close();
        }
    }

}

From source file:de.tor.tribes.util.AttackToTextWriter.java

private static boolean writeBlocksToFiles(List<String> pBlocks, Tribe pTribe, File pPath) {
    int fileNo = 1;
    String baseFilename = pTribe.getName().replaceAll("\\W+", "");
    for (String block : pBlocks) {
        String fileName = FilenameUtils.concat(pPath.getPath(), baseFilename + fileNo + ".txt");
        FileWriter w = null;
        try {//  w  w  w .j  av  a 2  s  . c o m
            w = new FileWriter(fileName);
            w.write(block);
            w.flush();
            w.close();
        } catch (IOException ioe) {
            logger.error("Failed to write attack to textfile", ioe);
            return false;
        } finally {
            if (w != null) {
                try {
                    w.close();
                } catch (IOException ignored) {
                }
            }
        }
        fileNo++;
    }
    return true;
}

From source file:tools.EpsServletUtilities.java

public static String saveChartAsEPS(JFreeChart chart, int width, int height, HttpSession session)
        throws IOException {

    if (chart == null) {
        throw new IllegalArgumentException("Null 'chart' argument.");
    }/*  ww  w. jav  a2s  .  c o  m*/
    EpsServletUtilities.createTempDir();
    String prefix = EpsServletUtilities.tempFilePrefix;
    if (session == null) {
        prefix = EpsServletUtilities.tempOneTimeFilePrefix;
    }
    File tempFile = File.createTempFile(prefix, ".eps", new File(System.getProperty("java.io.tmpdir")));

    EpsGraphics2D graphic2d = new EpsGraphics2D();
    chart.draw(graphic2d, new Rectangle2D.Double(0, 0, width, height));

    try {
        FileWriter fos = new FileWriter(tempFile);
        fos.write(graphic2d.toString());
        fos.close();

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();
    }

    if (session != null) {
        EpsServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getPath();

}

From source file:com.espe.distribuidas.pmaldito.servidorbdd.operaciones.Archivo.java

/**
 * Permite insertar registros en una tabla
 *
 * @param string//  w  w  w.jav  a 2  s .  co m
 * @param archivo
 */
@SuppressWarnings("ConvertToTryWithResources")
public static void insertar(String string, File archivo) {

    try {
        FileWriter fw = new FileWriter(archivo, true);
        fw.write(string + "\n");
        fw.close();
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:io.github.casnix.spawnerdropper.SpawnerStack.java

public static boolean PutSpawnerIntoService(String spawnerType) {
    // Read ./SpawnerDropper.SpawnerStack.json into a string

    // get the value of JSON->{spawnerType} and add 1 to it.

    // Write file back to disk
    try {//from  w w  w  . j  a v a 2  s  .  co  m
        // Read entire ./SpawnerDropper.SpawnerStack.json into a string
        String spawnerStack = new String(
                Files.readAllBytes(Paths.get("./plugins/SpawnerDropper/SpawnerStack.json")));

        // get the value of JSON->{spawnerType}
        JSONParser parser = new JSONParser();

        Object obj = parser.parse(spawnerStack);

        JSONObject jsonObj = (JSONObject) obj;

        long numberInService = (Long) jsonObj.get(spawnerType);

        if (numberInService < 0) {
            numberInService = 0;
        }

        //         System.out.println("[SD DBG MSG] PSIS numberInServer("+numberInService+")");
        numberInService += 1;
        jsonObj.put(spawnerType, new Long(numberInService));

        FileWriter file = new FileWriter("./plugins/SpawnerDropper/SpawnerStack.json");
        file.write(jsonObj.toJSONString());
        file.flush();
        file.close();

        return true;
    } catch (ParseException e) {
        Bukkit.getLogger().warning("[SpawnerDropper] Caught ParseException in PutSpawnerIntoService(String)");
        e.printStackTrace();

        return false;
    } catch (FileNotFoundException e) {
        Bukkit.getLogger()
                .warning("[SpawnerDropper] Could not find ./plugins/SpawnerDropper/SpawnerStack.json");
        e.printStackTrace();

        return false;
    } catch (IOException e) {
        Bukkit.getLogger().warning("[SpawnerDropper] Caught IOException in PutSpawnerIntoService(String)");
        e.printStackTrace();

        return false;
    }
}

From source file:jlite.cli.JobSubmit.java

private static void run(String jdlFile, CommandLine line) throws Exception {
    if (line.hasOption("debug")) {
        Logger logger = Logger.getLogger(GridSessionImpl.class);
        logger.setLevel(Level.DEBUG);
    }/*from  w  w w  . j ava 2  s . co  m*/

    GridSessionConfig conf = new GridSessionConfig();
    GridSession grid;

    if (line.hasOption("proxypath")) {
        conf.setProxyPath(line.getOptionValue("proxypath"));
    }

    String vo = Util.readVOFromVOMSProxy(conf.getProxyPath());

    if (line.hasOption("xml")) {
        System.out.println("<vo>" + vo + "</vo>");
    } else {
        System.out.println("Working VO: " + vo);
    }
    String wmProxyURL = conf.getWMProxies().get(vo);
    if (line.hasOption("e")) {
        wmProxyURL = line.getOptionValue("e");
    }
    if (wmProxyURL == null) {
        throw new GridAPIException("Could not find WMProxy server for VO: " + vo);
    }
    if (line.hasOption("xml")) {
        System.out.println("<wmProxy>" + wmProxyURL + "</wmProxy>");
    } else {
        System.out.println("Connecting to WMProxy service: " + wmProxyURL + "\n");
    }

    String delegationId;
    if (line.hasOption("a")) {
        grid = GridSessionFactory.create(conf);
        delegationId = line.getOptionValue("d", System.getProperty("user.name"));
        grid.delegateProxy(wmProxyURL, delegationId);
        if (line.hasOption("xml")) {
            System.out.println("<delegationId>" + delegationId + "</delegationId>");
        } else {
            System.out.println("Your proxy has been successfully delegated to WMProxy\n"
                    + "Delegation identifier: " + delegationId + "\n");
        }
    } else {
        delegationId = line.getOptionValue("d", System.getProperty("user.name"));
        conf.setDelegationId(delegationId);
        grid = GridSessionFactory.create(conf);
    }

    JobAd jad = new JobAd();
    jad.fromFile(jdlFile);
    if (line.hasOption("r")) {
        Op expr = new Op(Expr.EQUAL, Constant.getInstance("other.GlueCEUniqueID"),
                Constant.getInstance(line.getOptionValue("r")));
        jad.delAttribute("Requirements");
        jad.setAttribute("Requirements", expr);
    }

    String inputDir = line.getOptionValue("in", System.getProperty("user.dir"));

    String jobId = grid.submitJob(wmProxyURL, jad.toString(), inputDir);

    if (line.hasOption("xml")) {
        System.out.println("<jobId>" + jobId + "</jobId>");
    } else {
        System.out.println("The job has been successfully submitted to the WMProxy");
        System.out.println("Your job identifier is: \n\n\t" + jobId);
    }

    if (line.hasOption("o")) {
        File outFile = new File(line.getOptionValue("o"));
        FileWriter out = new FileWriter(outFile, true);
        out.write(jobId + "\n");
        out.close();
        if (line.hasOption("xml")) {
            System.out.println("<jodIdFile>" + outFile.getAbsolutePath() + "</jobIdFile>");
        } else {
            System.out.println(
                    "\nThe job identifier has been saved in the following file:\n" + outFile.getAbsolutePath());
        }
    }
}

From source file:com.boyuanitsm.pay.alipay.util.AlipayCore.java

/** 
 * ???//from ww  w  .  j a va 2s.c o  m
 * @param sWord ?
 */
public static void logResult(String sWord) {
    FileWriter writer = null;
    try {
        writer = new FileWriter(AlipayConfig.log_path + "alipay_log_" + System.currentTimeMillis() + ".txt");
        writer.write(sWord);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.ccoe.build.core.utils.FileUtils.java

public static void writeToFile(File targetFile, String body) {

    FileWriter fileWriter = null;
    try {/*from  www .j  ava  2  s .com*/
        fileWriter = new FileWriter(targetFile);
        fileWriter.write(body);
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            fileWriter.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:io.github.casnix.spawnerdropper.SpawnerStack.java

public static boolean TakeSpawnerOutOfService(String spawnerType) {
    // Read ./SpawnerDropper.SpawnerStack.json into a string

    // get the value of JSON->{spawnerType} and subtract 1 to it unless it is 0.
    // If it's zero, return false

    // Write file back to disk
    try {//ww w  .  ja  v a 2s .co m
        // Read entire ./SpawnerDropper.SpawnerStack.json into a string
        String spawnerStack = new String(
                Files.readAllBytes(Paths.get("./plugins/SpawnerDropper/SpawnerStack.json")));

        // get the value of JSON->{spawnerType}
        JSONParser parser = new JSONParser();

        Object obj = parser.parse(spawnerStack);

        JSONObject jsonObj = (JSONObject) obj;

        long numberInService = (Long) jsonObj.get(spawnerType);

        if (numberInService <= 0) {
            return false;
        } else {
            //            System.out.println("[SD DBG MSG] TSOOS numberInServer("+numberInService+")");

            numberInService -= 1;
            jsonObj.put(spawnerType, new Long(numberInService));

            FileWriter file = new FileWriter("./plugins/SpawnerDropper/SpawnerStack.json");
            file.write(jsonObj.toJSONString());
            file.flush();
            file.close();

            return true;
        }
    } catch (ParseException e) {
        Bukkit.getLogger().warning("[SpawnerDropper] Caught ParseException in TakeSpawnerOutOfService(String)");
        e.printStackTrace();

        return false;
    } catch (FileNotFoundException e) {
        Bukkit.getLogger()
                .warning("[SpawnerDropper] Could not find ./plugins/SpawnerDropper/SpawnerStack.json");
        e.printStackTrace();

        return false;
    } catch (IOException e) {
        Bukkit.getLogger().warning("[SpawnerDropper] Caught IOException in TakeSpawnerOutOfService(String)");
        e.printStackTrace();

        return false;
    }
}

From source file:com.serotonin.m2m2.web.dwr.ModulesDwr.java

@DwrPermission(admin = true)
synchronized public static void scheduleRestart() {
    if (RESTART_TASK == null) {
        SystemEventType.raiseEvent(new SystemEventType(SystemEventType.TYPE_SYSTEM_SHUTDOWN),
                System.currentTimeMillis(), false,
                new TranslatableMessage("modules.restartScheduledBy", Common.getUser().getUsername()));

        long timeout = Common.getMillis(Common.TimePeriods.SECONDS, 10);
        RESTART_TASK = new TimeoutTask(timeout, new TimeoutClient() {
            @Override/*from w  w  w. ja v  a2s. c  o m*/
            public void scheduleTimeout(long fireTime) {
                File restartFlag = new File(Common.MA_HOME, "RESTART");
                if (!restartFlag.exists()) {
                    try {
                        FileWriter fw = new FileWriter(restartFlag);
                        fw.write("restart");
                        fw.close();
                    } catch (IOException e) {
                        LOG.error("Unabled to create restart flag file", e);
                    }
                }
                Providers.get(ILifecycle.class).terminate();
            }
        });
    }
}