java.nio.file.Files.newBufferedWriter()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(257)

本文整理了Java中java.nio.file.Files.newBufferedWriter()方法的一些代码示例,展示了Files.newBufferedWriter()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.newBufferedWriter()方法的具体详情如下:
包路径:java.nio.file.Files
类名称:Files
方法名:newBufferedWriter

Files.newBufferedWriter介绍

暂无

代码示例

代码示例来源:origin: springside/springside4

/**
 * 获取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);
}

代码示例来源:origin: apache/storm

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

代码示例来源:origin: springside/springside4

/**
 * 简单写入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);
  }
}

代码示例来源:origin: springside/springside4

/**
 * 追加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);
  }
}

代码示例来源:origin: org.testng/testng

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

代码示例来源:origin: org.testng/testng

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

代码示例来源:origin: springside/springside4

/**
 * 获取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);
}

代码示例来源:origin: MovingBlocks/Terasology

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

代码示例来源:origin: pxb1988/dex2jar

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

代码示例来源:origin: google/guava

@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);
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

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;
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

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;
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

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;
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

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;
  }
}

代码示例来源:origin: apache/incubator-druid

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

代码示例来源:origin: google/guava

@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);
}

代码示例来源:origin: robolectric/robolectric

@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));
 }
}

代码示例来源:origin: apache/incubator-druid

@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);
}

代码示例来源:origin: checkstyle/checkstyle

@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);
}

代码示例来源:origin: checkstyle/checkstyle

@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);
}

相关文章

微信公众号

最新文章

更多