org.apache.commons.io.FileUtils.write()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(538)

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

FileUtils.write介绍

[英]Writes a CharSequence to a file creating the file if it does not exist using the default encoding for the VM.
[中]使用VM的默认编码将CharSequence写入创建文件的文件(如果该文件不存在)。

代码示例

代码示例来源:origin: SonarSource/sonarqube

private void setLastUsedReport(File lastReport) throws IOException {
 File f = new File(System.getProperty("java.io.tmpdir"), ".last_batch_report_dir");
 String fullPath = lastReport.getAbsolutePath();
 FileUtils.write(f, fullPath, StandardCharsets.UTF_8);
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWriteCharSequence1() throws Exception {
  final File file = new File(getTestDirectory(), "write.txt");
  FileUtils.write(file, "Hello /u1234", "UTF8");
  final byte[] text = "Hello /u1234".getBytes("UTF8");
  TestUtils.assertEqualContent(text, file);
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWriteWithEncoding_WithAppendOptionTrue_ShouldNotDeletePreviousFileLines() throws Exception {
  final File file = TestUtils.newFile(getTestDirectory(), "lines.txt");
  FileUtils.writeStringToFile(file, "This line was there before you...");
  FileUtils.write(file, "this is brand new data", (String) null, true);
  final String expected = "This line was there before you..."
      + "this is brand new data";
  final String actual = FileUtils.readFileToString(file);
  assertEquals(expected, actual);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void should_not_load_reference_hashes_when_status_same() throws Exception {
 final String source = "source";
 String key = "foo:src/Foo.java";
 FileUtils.write(ioFile, source, StandardCharsets.UTF_8);
 when(file.key()).thenReturn(key);
 when(file.status()).thenReturn(InputFile.Status.SAME);
 assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source));
 Mockito.verifyNoMoreInteractions(lastSnapshots);
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWriteCharSequence2() throws Exception {
  final File file = new File(getTestDirectory(), "write.txt");
  FileUtils.write(file, "Hello /u1234", (String) null);
  final byte[] text = "Hello /u1234".getBytes();
  TestUtils.assertEqualContent(text, file);
}

代码示例来源:origin: SonarSource/sonarqube

private File createAllLogsFiles() throws IOException {
 File dir = createLogsDir();
 FileUtils.write(new File(dir, "sonar.log"), "{app}");
 FileUtils.write(new File(dir, "ce.log"), "{ce}");
 FileUtils.write(new File(dir, "es.log"), "{es}");
 FileUtils.write(new File(dir, "web.log"), "{web}");
 return dir;
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void publishEmptySource() throws Exception {
 FileUtils.write(sourceFile, "", StandardCharsets.ISO_8859_1);
 publisher.publish(writer);
 File out = writer.getSourceFile(inputFile.scannerId());
 assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("");
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWriteWithEncoding_WithAppendOptionFalse_ShouldDeletePreviousFileLines() throws Exception {
  final File file = TestUtils.newFile(getTestDirectory(), "lines.txt");
  FileUtils.writeStringToFile(file, "This line was there before you...");
  FileUtils.write(file, "this is brand new data", (String) null, false);
  final String expected = "this is brand new data";
  final String actual = FileUtils.readFileToString(file);
  assertEquals(expected, actual);
}

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

@Test
  public void testScp() throws Exception {
    if (isRemote == false)
      return;

    SSHClient ssh = new SSHClient(this.hostname, this.port, this.username, this.password);
    File tmpFile = File.createTempFile("test_scp", "", new File("/tmp"));
    tmpFile.deleteOnExit();
    FileUtils.write(tmpFile, "test_scp", Charset.defaultCharset());
    ssh.scpFileToRemote(tmpFile.getAbsolutePath(), "/tmp");
  }
}

代码示例来源:origin: SonarSource/sonarqube

private void writeTimeToFile(String filename) {
 try {
  FileUtils.write(new File(tempDir, httpPort + "-" + filename), System.currentTimeMillis() + ",", true);
 } catch (IOException e) {
  throw new IllegalStateException(e);
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void no_reference_hashes_when_status_added() throws Exception {
 final String source = "source";
 String key = "foo:src/Foo.java";
 FileUtils.write(ioFile, source, StandardCharsets.UTF_8);
 when(file.key()).thenReturn(key);
 when(file.status()).thenReturn(InputFile.Status.ADDED);
 assertThat(sourceHashHolder.getHashedReference()).isNull();
 Mockito.verifyNoMoreInteractions(lastSnapshots);
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_WithAppendOptionTrue_ShouldNotDeletePreviousFileLines() throws Exception {
  final File file = TestUtils.newFile(getTestDirectory(), "lines.txt");
  FileUtils.writeStringToFile(file, "This line was there before you...");
  FileUtils.write(file, "this is brand new data", true);
  final String expected = "This line was there before you..."
      + "this is brand new data";
  final String actual = FileUtils.readFileToString(file);
  assertEquals(expected, actual);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void official_distribution() throws Exception {
 File rootDir = temp.newFolder();
 FileUtils.write(new File(rootDir, OfficialDistribution.BRANDING_FILE_PATH), "1.2");
 when(serverFileSystem.getHomeDir()).thenReturn(rootDir);
 assertThat(underTest.check()).isTrue();
}

代码示例来源:origin: SonarSource/sonarqube

private static File touch(File dir, String filename) throws IOException {
 File file = new File(dir, filename);
 FileUtils.write(file, RandomStringUtils.random(10));
 return file;
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void publishSourceWithLastEmptyLine() throws Exception {
 FileUtils.write(sourceFile, "1\n2\n3\n4\n", StandardCharsets.ISO_8859_1);
 publisher.publish(writer);
 File out = writer.getSourceFile(inputFile.scannerId());
 assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("1\n2\n3\n4\n");
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_WithAppendOptionFalse_ShouldDeletePreviousFileLines() throws Exception {
  final File file = TestUtils.newFile(getTestDirectory(), "lines.txt");
  FileUtils.writeStringToFile(file, "This line was there before you...");
  FileUtils.write(file, "this is brand new data", false);
  final String expected = "this is brand new data";
  final String actual = FileUtils.readFileToString(file);
  assertEquals(expected, actual);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void warnWhenSavingFolderMeasure() throws IOException {
 File xooFile = new File(srcDir, "sample.xoo");
 FileUtils.write(xooFile, "Sample xoo\n\n\ncontent", StandardCharsets.UTF_8);
 File folderMeasures = new File(srcDir, "folder.measures");
 FileUtils.write(folderMeasures, "tests:10", StandardCharsets.UTF_8);
 AnalysisResult result = tester.newAnalysis()
  .properties(ImmutableMap.<String, String>builder()
   .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
   .put("sonar.projectKey", "com.foo.project")
   .put("sonar.sources", "src")
   .build())
  .execute();
 assertThat(logTester.logs(LoggerLevel.WARN)).contains("Storing measures on folders or modules is deprecated. Provided value of metric 'tests' is ignored.");
}

代码示例来源:origin: SonarSource/sonarqube

private File prepareProject() throws IOException {
 File baseDir = temp.newFolder();
 File module1Dir = new File(baseDir, "module1");
 module1Dir.mkdir();
 File srcDir = new File(module1Dir, "src");
 srcDir.mkdir();
 File xooFile = new File(srcDir, "sample.xoo");
 FileUtils.write(xooFile, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
 return baseDir;
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void publishTestSource() throws Exception {
 FileUtils.write(sourceFile, "1\n2\n3\n4\n", StandardCharsets.ISO_8859_1);
 // sampleFile.setQualifier(Qualifiers.UNIT_TEST_FILE);
 publisher.publish(writer);
 File out = writer.getSourceFile(inputFile.scannerId());
 assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("1\n2\n3\n4\n");
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void testChangeLogForAnalysis() throws IOException, InterruptedException {
 File srcDir = new File(baseDir, "src");
 srcDir.mkdir();
 File xooFile = new File(srcDir, "sample.xoo");
 FileUtils.write(xooFile, "Sample xoo\ncontent");
 tester.newAnalysis()
  .properties(builder
   .put("sonar.sources", "src")
   .put("sonar.verbose", "true")
   .build())
  .execute();
 for (LogEvent e : logOutput) {
  savedStdOut.println("[captured]" + e.level + " " + e.msg);
 }
 // only done in DEBUG during analysis
 assertThat(logOutputStr.toString()).contains("Post-jobs : ");
}

相关文章

FileUtils类方法