use of java.io.FileWriter in project flink by apache.
the class TestFileUtils method createTempFileDirExtension.
public static String createTempFileDirExtension(String fileExtension, String... contents) throws IOException {
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File f = null;
do {
f = new File(tempDir, randomFileName(FILE_SUFFIX));
} while (f.exists());
f.mkdirs();
f.deleteOnExit();
for (String s : contents) {
File child = new File(f, randomFileName(fileExtension));
child.deleteOnExit();
try (BufferedWriter out = new BufferedWriter(new FileWriter(child))) {
out.write(s);
}
}
return f.toURI().toString();
}
use of java.io.FileWriter in project flink by apache.
the class WebLogDataGenerator method genDocs.
/**
* Generates the files for the documents relation. The entries apply the
* following format: <br />
* <code>URL | Content</code>
*
* @param noDocs
* Number of entries for the documents relation
* @param filterKeyWords
* A list of keywords that should be contained
* @param words
* A list of words to fill the entries
* @param path
* Output path for the documents relation
*/
private static void genDocs(int noDocs, String[] filterKeyWords, String[] words, String path) {
Random rand = new Random(Calendar.getInstance().getTimeInMillis());
try (FileWriter fw = new FileWriter(path)) {
for (int i = 0; i < noDocs; i++) {
int wordsInDoc = rand.nextInt(40) + 10;
// URL
StringBuilder doc = new StringBuilder("url_" + i + "|");
for (int j = 0; j < wordsInDoc; j++) {
if (rand.nextDouble() > 0.9) {
// Approx. every 10th word is a keyword
doc.append(filterKeyWords[rand.nextInt(filterKeyWords.length)] + " ");
} else {
// Fills up the docs file(s) with random words
doc.append(words[rand.nextInt(words.length)] + " ");
}
}
doc.append("|\n");
fw.write(doc.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
use of java.io.FileWriter in project Notes by lguipeng.
the class FileUtils method writeFile.
public boolean writeFile(String dir, String fileName, String content, boolean append) {
if (TextUtils.isEmpty(content)) {
return false;
}
FileWriter fileWriter = null;
try {
String filePath = dir + File.separator + fileName;
fileWriter = new FileWriter(filePath, append);
fileWriter.write(content + "\n");
fileWriter.close();
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
use of java.io.FileWriter in project libgdx by libgdx.
the class ProjectBuilder method build.
public boolean build() throws IOException {
settingsFile = File.createTempFile("libgdx-setup-settings", ".gradle");
buildFile = File.createTempFile("libgdx-setup-build", ".gradle");
if (!settingsFile.exists()) {
settingsFile.createNewFile();
}
if (!buildFile.exists()) {
buildFile.createNewFile();
}
settingsFile.setWritable(true);
buildFile.setWritable(true);
try {
FileWriter settingsWriter = new FileWriter(settingsFile.getAbsoluteFile());
BufferedWriter settingsBw = new BufferedWriter(settingsWriter);
String settingsContents = "include ";
for (ProjectType module : modules) {
settingsContents += "'" + module.getName() + "'";
if (modules.indexOf(module) != modules.size() - 1) {
settingsContents += ", ";
}
}
settingsBw.write(settingsContents);
settingsBw.close();
settingsWriter.close();
FileWriter buildWriter = new FileWriter(buildFile.getAbsoluteFile());
BufferedWriter buildBw = new BufferedWriter(buildWriter);
BuildScriptHelper.addBuildScript(modules, buildBw);
BuildScriptHelper.addAllProjects(buildBw);
for (ProjectType module : modules) {
BuildScriptHelper.addProject(module, dependencies, buildBw);
}
//Add task here for now
buildBw.write("\n");
buildBw.write("tasks.eclipse.doLast {\n");
buildBw.write(" delete \".project\"\n");
buildBw.write("}");
buildBw.close();
buildWriter.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
use of java.io.FileWriter in project libgdx by libgdx.
the class EffectPanel method saveEffect.
void saveEffect() {
FileDialog dialog = new FileDialog(editor, "Save Effect", FileDialog.SAVE);
if (lastDir != null)
dialog.setDirectory(lastDir);
dialog.setVisible(true);
String file = dialog.getFile();
String dir = dialog.getDirectory();
if (dir == null || file == null || file.trim().length() == 0)
return;
lastDir = dir;
int index = 0;
File effectFile = new File(dir, file);
// save each image path as relative path to effect file directory
URI effectDirUri = effectFile.getParentFile().toURI();
for (ParticleEmitter emitter : editor.effect.getEmitters()) {
emitter.setName((String) emitterTableModel.getValueAt(index++, 0));
String imagePath = emitter.getImagePath();
if ((imagePath.contains("/") || imagePath.contains("\\")) && !imagePath.contains("..")) {
// it's absolute, make it relative:
URI imageUri = new File(emitter.getImagePath()).toURI();
emitter.setImagePath(effectDirUri.relativize(imageUri).getPath());
}
}
File outputFile = new File(dir, file);
Writer fileWriter = null;
try {
fileWriter = new FileWriter(outputFile);
editor.effect.save(fileWriter);
} catch (Exception ex) {
System.out.println("Error saving effect: " + outputFile.getAbsolutePath());
ex.printStackTrace();
JOptionPane.showMessageDialog(editor, "Error saving effect.");
} finally {
StreamUtils.closeQuietly(fileWriter);
}
}
Aggregations