/** * 获取File的BufferedWriter. * * @see {@link Files#newBufferedWriter} */ public static BufferedWriter asBufferedWriter(Path path) throws IOException { Validate.notNull(path, "path is null"); return Files.newBufferedWriter(path, Charsets.UTF_8); }
private void initLogWriter(Path logFilePath) { try { LOG.info("logFilePath {}", logFilePath); eventLogPath = logFilePath; eventLogWriter = Files.newBufferedWriter(eventLogPath, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND); } catch (IOException e) { LOG.error("Error setting up FileBasedEventLogger.", e); throw new RuntimeException(e); } }
/** * 简单写入String到File. */ public static void write(final CharSequence data, final File file) throws IOException { Validate.notNull(file); Validate.notNull(data); try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), Charsets.UTF_8)) { writer.append(data); } }
/** * 追加String到File. */ public static void append(final CharSequence data, final File file) throws IOException { Validate.notNull(file); Validate.notNull(data); try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), Charsets.UTF_8, StandardOpenOption.APPEND)) { writer.append(data); } }
protected PrintWriter createWriter(String outdir) throws IOException { new File(outdir).mkdirs(); String jvmArg = System.getProperty(JVM_ARG); if (jvmArg != null && !jvmArg.trim().isEmpty()) { fileName = jvmArg; } return new PrintWriter(newBufferedWriter(new File(outdir, fileName).toPath(), UTF_8)); }
protected PrintWriter createWriter(String outdir) throws IOException { new File(outdir).mkdirs(); String jvmArg = System.getProperty(JVM_ARG); if (jvmArg != null && !jvmArg.trim().isEmpty()) { fileName = jvmArg; } return new PrintWriter(newBufferedWriter(new File(outdir, fileName).toPath(), UTF_8)); }
/** * 获取File的BufferedWriter. * * @see {@link Files#newBufferedWriter} */ public static BufferedWriter asBufferedWriter(String fileName) throws IOException { Validate.notBlank(fileName, "filename is blank"); return Files.newBufferedWriter(getPath(fileName), Charsets.UTF_8); }
public void save(Path file) throws IOException { final EntityData.GlobalStore world = persisterHelper.serializeWorld(true); Path parentFile = file.toAbsolutePath().getParent(); if (!Files.isDirectory(parentFile)) { Files.createDirectories(parentFile); } try (BufferedWriter writer = Files.newBufferedWriter(file, TerasologyConstants.CHARSET)) { EntityDataJSONFormat.write(world, writer); } } }
private void disassemble1(Path file, Path output) throws IOException { ClassReader r = new ClassReader(Files.readAllBytes(file)); Path jFile = output.resolve(r.getClassName().replace('.', '/') + ".j"); createParentDirectories(jFile); try (BufferedWriter out = Files.newBufferedWriter(jFile, Charset.forName(encoding))) { PrintWriter pw = new PrintWriter(out); ClassNode node = new ClassNode(); r.accept(node, (debugInfo ? 0 : ClassReader.SKIP_DEBUG) | ClassReader.EXPAND_FRAMES | ClassReader.SKIP_FRAMES); new JasminDumper(pw).dump(node); pw.flush(); } } }
@Override public CharSource createSource(String string) throws IOException { checkNotNull(string); Path file = createFile(); try (Writer writer = java.nio.file.Files.newBufferedWriter(file, Charsets.UTF_8)) { writer.write(string); } return MoreFiles.asCharSource(file, Charsets.UTF_8); }
private Path writeTextTo(final String location) throws IOException { final Path path = Paths.get(location); Files.createDirectories(path.getParent()); try (BufferedWriter buffy = Files.newBufferedWriter(path, Charset.defaultCharset())) { buffy.write("some text"); buffy.newLine(); buffy.flush(); } return path; } }
private Path writeTextTo(final String location) throws IOException { final Path path = Paths.get(location); Files.createDirectories(path.getParent()); try (BufferedWriter buffy = Files.newBufferedWriter(path, Charset.defaultCharset())) { buffy.write("some text"); buffy.newLine(); buffy.flush(); } return path; } }
private Path writeTextTo(final String location) throws IOException { final Path path = Paths.get(location); Files.createDirectories(path.getParent()); try (BufferedWriter buffy = Files.newBufferedWriter(path, Charset.defaultCharset())) { buffy.write("some text"); buffy.newLine(); buffy.flush(); } return path; }
private Path writeTextTo(final String location) throws IOException { final Path path = Paths.get(location); Files.createDirectories(path.getParent()); try (BufferedWriter buffy = Files.newBufferedWriter(path, Charset.defaultCharset())) { buffy.write("some text"); buffy.newLine(); buffy.flush(); } return path; } }
private static void generateBucketTestData() throws Exception { double meanTest = 10; double meanControl = 10.2; Path path = FileSystems.getDefault().getPath("bucket_test_data.tsv"); try (BufferedWriter out = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) { Random rand = ThreadLocalRandom.current(); for (int i = 0; i < 1000; i++) { writeBucketTestRecord(out, "test", i, rand.nextGaussian() + meanTest); writeBucketTestRecord(out, "control", i, rand.nextGaussian() + meanControl); } } }
@Override public CharSink createSink() throws IOException { Path file = createFile(); if (initialString != null) { try (Writer writer = java.nio.file.Files.newBufferedWriter(file, Charsets.UTF_8)) { writer.write(initialString); } return MoreFiles.asCharSink(file, Charsets.UTF_8, StandardOpenOption.APPEND); } return MoreFiles.asCharSink(file, Charsets.UTF_8); }
@Test public void openFileInput_shouldReturnAFileInputStream() throws Exception { String fileContents = "blah"; File file = new File(context.getFilesDir(), "__test__"); try (Writer fileWriter = Files.newBufferedWriter(file.toPath(), UTF_8)) { fileWriter.write(fileContents); } try (FileInputStream fileInputStream = context.openFileInput("__test__")) { byte[] bytes = new byte[fileContents.length()]; fileInputStream.read(bytes); assertThat(bytes).isEqualTo(fileContents.getBytes(UTF_8)); } }
@Before public void setup() throws IOException { for (int i = 0; i < 5; i++) { try (final Writer writer = Files.newBufferedWriter(temporaryFolder.newFile("test_" + i).toPath(), StandardCharsets.UTF_8)) { writer.write((20171225 + i) + "," + i + "th test file\n"); } } for (int i = 0; i < 5; i++) { try (final Writer writer = Files.newBufferedWriter(temporaryFolder.newFile("filtered_" + i).toPath(), StandardCharsets.UTF_8)) { writer.write((20171225 + i) + "," + i + "th filtered file\n"); } } factory = new LocalFirehoseFactory(temporaryFolder.getRoot(), "test_*", null); }
@Test public void testManyComments() throws Exception { final File file = temporaryFolder.newFile("InputDetailASTManyComments.java"); try (Writer bw = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) { bw.write("class C {\n"); for (int i = 0; i <= 30000; i++) { bw.write("// " + i + "\n"); } bw.write("}\n"); } final DefaultConfiguration checkConfig = createModuleConfig(TodoCommentCheck.class); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkConfig, file.getAbsolutePath(), expected); }
@Test public void testImproperFileExtension() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(ConstantNameCheck.class); final File file = temporaryFolder.newFile("file.pdf"); try (Writer writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) { final String content = "public class Main { public static final int k = 5 + 4; }"; writer.write(content); } final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkConfig, file.getPath(), expected); }