Search in sources :

Example 56 with FileWriter

use of java.io.FileWriter in project robolectric by robolectric.

the class TemporaryAsset method createFile.

public File createFile(AndroidManifest manifest, String fileName, String contents) throws Exception {
    File assetBase = ((FileFsFile) manifest.getAssetsDirectory()).getFile();
    File file = new File(assetBase, fileName);
    file.getParentFile().mkdirs();
    FileWriter fileWriter = new FileWriter(file);
    try {
        fileWriter.write(contents);
    } finally {
        fileWriter.close();
    }
    assetsToDelete.add(file);
    return file;
}
Also used : FileFsFile(org.robolectric.res.FileFsFile) FileWriter(java.io.FileWriter) FileFsFile(org.robolectric.res.FileFsFile) File(java.io.File)

Example 57 with FileWriter

use of java.io.FileWriter in project flink by apache.

the class LinearRegressionDataGenerator method main.

/**
	 * Main method to generate data for the {@link org.apache.flink.examples.java.ml.LinearRegression} example program.
	 * <p>
	 * The generator creates to files:
	 * <ul>
	 * <li><code>{tmp.dir}/data</code> for the data points
	 * </ul> 
	 * 
	 * @param args 
	 * <ol>
	 * <li>Int: Number of data points
	 * <li><b>Optional</b> Long: Random seed
	 * </ol>
	 */
public static void main(String[] args) throws IOException {
    // check parameter count
    if (args.length < 1) {
        System.out.println("LinearRegressionDataGenerator <numberOfDataPoints> [<seed>]");
        System.exit(1);
    }
    // parse parameters
    final int numDataPoints = Integer.parseInt(args[0]);
    final long firstSeed = args.length > 1 ? Long.parseLong(args[4]) : DEFAULT_SEED;
    final Random random = new Random(firstSeed);
    final String tmpDir = System.getProperty("java.io.tmpdir");
    // write the points out
    BufferedWriter pointsOut = null;
    try {
        pointsOut = new BufferedWriter(new FileWriter(new File(tmpDir + "/" + POINTS_FILE)));
        StringBuilder buffer = new StringBuilder();
        // DIMENSIONALITY + 1 means that the number of x(dimensionality) and target y
        double[] point = new double[DIMENSIONALITY + 1];
        for (int i = 1; i <= numDataPoints; i++) {
            point[0] = random.nextGaussian();
            point[1] = 2 * point[0] + 0.01 * random.nextGaussian();
            writePoint(point, buffer, pointsOut);
        }
    } finally {
        if (pointsOut != null) {
            pointsOut.close();
        }
    }
    System.out.println("Wrote " + numDataPoints + " data points to " + tmpDir + "/" + POINTS_FILE);
}
Also used : Random(java.util.Random) FileWriter(java.io.FileWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 58 with FileWriter

use of java.io.FileWriter in project flink by apache.

the class TestConfigUtils method loadGlobalConf.

public static Configuration loadGlobalConf(String contents) throws IOException {
    final File tempDir = new File(System.getProperty("java.io.tmpdir"));
    File confDir;
    do {
        confDir = new File(tempDir, TestFileUtils.randomFileName());
    } while (confDir.exists());
    try {
        confDir.mkdirs();
        final File confFile = new File(confDir, GlobalConfiguration.FLINK_CONF_FILENAME);
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(confFile));
            try {
                writer.write(contents);
            } finally {
                writer.close();
            }
            return GlobalConfiguration.loadConfiguration(confDir.getAbsolutePath());
        } finally {
            confFile.delete();
        }
    } finally {
        confDir.delete();
    }
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 59 with FileWriter

use of java.io.FileWriter in project flink by apache.

the class TestFileUtils method createTempFileInDirectory.

public static String createTempFileInDirectory(String dir, String contents) throws IOException {
    File f;
    do {
        f = new File(dir + "/" + randomFileName());
    } while (f.exists());
    f.getParentFile().mkdirs();
    f.createNewFile();
    f.deleteOnExit();
    try (BufferedWriter out = new BufferedWriter(new FileWriter(f))) {
        out.write(contents);
    }
    return f.toURI().toString();
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 60 with FileWriter

use of java.io.FileWriter in project flink by apache.

the class TestFileUtils method createTempFile.

public static String createTempFile(String contents) throws IOException {
    File f = File.createTempFile(FILE_PREFIX, FILE_SUFFIX);
    f.deleteOnExit();
    try (BufferedWriter out = new BufferedWriter(new FileWriter(f))) {
        out.write(contents);
    }
    return f.toURI().toString();
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Aggregations

FileWriter (java.io.FileWriter)1994 File (java.io.File)1195 IOException (java.io.IOException)866 BufferedWriter (java.io.BufferedWriter)798 PrintWriter (java.io.PrintWriter)329 Test (org.junit.Test)243 Writer (java.io.Writer)181 FileReader (java.io.FileReader)148 BufferedReader (java.io.BufferedReader)128 ArrayList (java.util.ArrayList)121 FileNotFoundException (java.io.FileNotFoundException)78 Date (java.util.Date)68 FileOutputStream (java.io.FileOutputStream)65 Properties (java.util.Properties)65 HashMap (java.util.HashMap)61 FileInputStream (java.io.FileInputStream)54 StringWriter (java.io.StringWriter)51 Path (org.apache.hadoop.fs.Path)50 Map (java.util.Map)42 InputStreamReader (java.io.InputStreamReader)34