Example usage for java.io FileWriter FileWriter

List of usage examples for java.io FileWriter FileWriter

Introduction

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

Prototype

public FileWriter(FileDescriptor fd) 

Source Link

Document

Constructs a FileWriter given a file descriptor, using the platform's java.nio.charset.Charset#defaultCharset() default charset .

Usage

From source file:Main.java

public static void writeContentToFile(String fileName, String contents) throws IOException {
    Log.d("writeContentToFile", fileName);
    File f = new File(fileName);
    f.getParentFile().mkdirs();/*from w  w w .j a v a  2 s.c  o  m*/
    File tempFile = new File(fileName + ".tmp");
    FileWriter fw = new FileWriter(tempFile);
    BufferedWriter bw = new BufferedWriter(fw);
    int length = contents.length();
    if (length > 0) {
        bw.write(contents);
        //         int apart =  Math.min(length, 65536);
        //         int times = length / apart;
        //         for (int i = 0; i < times; i++) {
        //            bw.write(contents, i * apart, apart);
        //         }
        //         if (length % apart != 0) {
        //            bw.write(contents, times * apart, length - times * apart);
        //         }
        bw.flush();
        fw.flush();
        bw.close();
        fw.close();
        f.delete();
        tempFile.renameTo(f);
    }
}

From source file:Main.java

public static void writeToFile(Collection<?> collection, File file) throws IOException {
    if (collection != null && file != null) {
        file.getParentFile().mkdirs();/*from  w w  w. j  a  va 2 s  . c o  m*/
        File tempFile = new File(file.getAbsolutePath() + ".tmp");
        FileWriter fw = new FileWriter(tempFile);
        for (Object obj : collection) {
            fw.write(new StringBuilder(obj.toString()).append("\r\n").toString());
        }
        fw.flush();
        fw.close();
        file.delete();
        tempFile.renameTo(file);
    }
}

From source file:Main.java

static void setSetting(String name, String data) {
    try {//from   ww  w.  j  a v a 2 s .  com
        File root = new File(Environment.getExternalStorageDirectory().toString(), ".Instagram");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, name + ".txt");
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(data);
        writer.flush();
        writer.close();
    } catch (IOException e) {
    }
}

From source file:Main.java

public static void putString2XmlFile(String xml, String fileName) {
    PrintWriter out = null;/*w  w w .ja  v a 2 s. c o  m*/
    try {
        out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
        out.write(xml, 0, xml.length());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        out.flush();
        out.close();
    }
}

From source file:Main.java

/**
 * This method writes a security token file
 * /*from  ww w .jav a  2  s. c om*/
 * @param tokenFilePath
 *            - path of the security token
 * @param token
 *            - security token
 * @return true == succeed, false == failed
 */
private static boolean writeTokenFile(String tokenFilePath, String token) {
    try {
        FileWriter file = new FileWriter(tokenFilePath);
        file.write(token);
        file.close();
    } catch (IOException e) {
        return false;
    }
    return true;
}

From source file:Main.java

public static void xmlToFile(Document doc, File file) {
    String xmlString = xmlToString(doc);

    try {/*  w w w. ja  v  a 2s . c  om*/
        FileWriter w = new FileWriter(file);
        w.write(xmlString);
        w.flush();
        w.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:experiments.SimpleExample.java

/**
 * Starts the example.//from  w  w w .  jav a 2  s.  co  m
 * 
 * @param args
 *            if optional first argument provided, it represents the number
 *            of bits to use, but no more than 32
 * 
 * @author Neil Rotstan
 * @author Klaus Meffert
 * @throws IOException 
 * @since 2.0
 */
public static void main(String[] args) throws IOException {

    SimpleExample se = new SimpleExample();

    try {
        File[] result = { new File("ga_x.txt"), new File("ga_cos.txt"), new File("ga_ackley.txt"),
                new File("ga_quar.txt"), new File("ga_step.txt"), new File("ga_rosen.txt"),
                new File("ga_sch.txt"), new File("ga_gri.txt"), new File("ga_pen1.txt"),
                new File("ga_pen2.txt"), new File("ga_wei.txt"), new File("ga_non.txt") };
        BufferedWriter[] output = new BufferedWriter[result.length];
        for (int i = 0; i <= result.length - 1; i++) {
            if (result[i].exists()) {
                result[i].delete();
                if (result[i].createNewFile()) {
                    System.out.println("result" + i + " file create success!");
                } else {
                    System.out.println("result" + i + " file create failed!");
                }
            } else {
                if (result[i].createNewFile()) {
                    System.out.println("result" + i + " file create success!");
                } else {
                    System.out.println("result" + i + " file create failed!");
                }

            }
            output[i] = new BufferedWriter(new FileWriter(result[i]));
        }

        for (int a = 0; a <= 0; a++) {
            //            se.runga(100, 30, 40, -100,  100, new MaxFunction(), output[0]);
            //            se.runga(200, 30, 40, -100,  100, new MaxFunction(), output[0]);
            //            se.runga(120, 30, 40, -5.12,  5.12, new CosMaxFunction(), output[1]);
            //            se.runga(200, 30, 40, -5.12,  5.12, new CosMaxFunction(), output[1]);
            //            se.runga(120, 30, 40, -32,  32, new AckleyMaxFunction(), output[2]);
            se.runga(2000, 30, 40, -32, 32, new AckleyMaxFunction(), output[2]);
            //            se.runga(120, 30, 40, -100,  100, new QuardircMaxFunction(), output[3]);
            //            se.runga(200, 30, 40, -100,  100, new QuardircMaxFunction(), output[3]);
            //            se.runga(120, 30, 40, -100,  100, new StepMaxFunction(), output[4]);
            //            se.runga(200, 30, 40, -100,  100, new StepMaxFunction(), output[4]);
            //            se.runga(120, 30, 40, -30,  30, new RosenbrockMaxFunction(), output[5]);
            //            se.runga(200, 30, 40, -30,  30, new RosenbrockMaxFunction(), output[5]);
            //            se.runga(120, 30, 40, -500,  500, new SchwefelMaxFunction(), output[6]);
            //            se.runga(200, 30, 40, -500,  500, new SchwefelMaxFunction(), output[6]);
            //            se.runga(120, 30, 40, -600,  600, new GriewankMaxFunction(), output[7]);
            //            se.runga(200, 30, 40, -600,  600, new GriewankMaxFunction(), output[7]);
            //            se.runga(120, 30, 40, -50,  50, new PenalizedMaxFunction(), output[8]);
            //            se.runga(200, 30, 40, -50,  50, new PenalizedMaxFunction(), output[8]);
            //            se.runga(120, 30, 40, -50,  50, new Penalized2MaxFunction(), output[9]);
            //            se.runga(200, 30, 40, -50,  50, new Penalized2MaxFunction(), output[9]);
            //            se.runga(120, 30, 40, -5.12,  5.12, new WeiMaxFunction(), output[10]);
            //            se.runga(200, 30, 40, -5.12,  5.12, new WeiMaxFunction(), output[10]);
            //            se.runga(120, 30, 40, -0.5,  0.5, new NonMaxFunction(), output[11]);
            //            se.runga(200, 30, 40, -0.5,  0.5, new NonMaxFunction(), output[11]);
            for (BufferedWriter op : output) {
                op.write("\n");
                op.flush();
            }
        }

        for (BufferedWriter op : output) {
            op.close();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * Write a string to a file//from w  w w .j a  va 2  s.  c o  m
 *  @param content The string to write out
 *  @param outputfile The file to which the string will be written
 */
public static void writeStringToFile(String content, File outputfile) {
    if (content != null && !content.equals("") && outputfile != null) {
        try {
            PrintWriter pwriter = new PrintWriter(new FileWriter(outputfile));
            pwriter.print(content);
            pwriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * Write to file in given folder// w w w . j  a v a  2s .  c om
 * @param fcontent
 * @return
 */
public static boolean writeFile(String fcontent, String path) {

    /*
     * Write file contents to file path
     */
    try {
        File file = new File(path);
        // If file does not exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(fcontent);
        bw.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:cz.cuni.mff.peckam.ais.statistics.IonogramStatistics.java

/**
 * @param args 0 => Orbit base dir, 1 => min orbit, 2 => max orbit
 * @throws IOException On IO error./*w  w  w .ja v  a2  s .c  o  m*/
 */
public static void main(String[] args) throws IOException {
    final File orbitDir = new File(args[0]);
    final int minOrbit = Integer.parseInt(args[1]);
    final int maxOrbit = Integer.parseInt(args[2]);

    try (final BufferedWriter meanWriter = new BufferedWriter(new FileWriter("mean"));
            final BufferedWriter sdWriter = new BufferedWriter(new FileWriter("sd"));
            final BufferedWriter maxWriter = new BufferedWriter(new FileWriter("max"));
            final BufferedWriter featuresWriter = new BufferedWriter(new FileWriter("features"));
            final BufferedWriter traceWriter = new BufferedWriter(new FileWriter("trace"))) {
        final IonogramStatistics stats = new IonogramStatistics(meanWriter, sdWriter, maxWriter, featuresWriter,
                traceWriter);

        for (int i = minOrbit; i <= maxOrbit; i++) {
            final File orbitFile = new File(orbitDir,
                    (i + "").replaceAll(".$", "X") + File.separator + "FRM_AIS_RDR_" + i + ".LBL");
            if (orbitFile.exists()) {
                stats.execute(orbitFile);
            }
        }
    }
    System.out.println("Minimal value: " + min);
}