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;
}
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);
}
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();
}
}
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();
}
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();
}
Aggregations