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

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

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

FileUtils.writeStringToFile介绍

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

代码示例

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

public static File writeStringToTempFileInFolder(String directoryName, String fileName, String contents)
    throws Exception {
  File folderToCreate =  tempFiles.createUniqueFolder(directoryName);
  File fileToCreate = new File(folderToCreate, fileName);
  folderToCreate.mkdirs();
  FileUtils.writeStringToFile(fileToCreate, contents, UTF_8);
  fileToCreate.deleteOnExit();
  folderToCreate.deleteOnExit();
  return fileToCreate;
}

代码示例来源:origin: jenkinsci/jenkins

@Initializer(after=JOB_LOADED)
  public static void init(Jenkins h) throws IOException {
    File userContentDir = new File(h.getRootDir(), "userContent");
    if(!userContentDir.exists()) {
      userContentDir.mkdirs();
      FileUtils.writeStringToFile(new File(userContentDir,"readme.txt"), Messages.Hudson_USER_CONTENT_README() + "\n");
    }
  }
}

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

private File createScript(String name, String content) throws IOException {
  File shellScript = new File(subFolder, name);
  FileUtils.writeStringToFile(shellScript, content, UTF_8);
  return shellScript;
}

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

public SetupOfAgentPluginsFile withBundledPlugin(String pluginFileName, String pluginFileContent) throws IOException {
  FileUtils.writeStringToFile(new File(bundledPluginsDir, pluginFileName), pluginFileContent, UTF_8);
  return this;
}

代码示例来源:origin: joelittlejohn/jsonschema2pojo

public void close() throws IOException {
    super.close();
    
    final String javaSource = new String(javaSourceStream.toByteArray(), encoding);
    final String scalaSource = Converter.instance210().convert(javaSource, new ConversionSettings(false));
    
    FileUtils.writeStringToFile(new File(scalaFileName), scalaSource, encoding);
  }
};

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

private void createFilesForTestCopyDirectory(final File grandParentDir, final File parentDir, final File childDir) throws Exception {
  final File childDir2 = new File(parentDir, "child2");
  final File grandChildDir = new File(childDir, "grandChild");
  final File grandChild2Dir = new File(childDir2, "grandChild2");
  final File file1 = new File(grandParentDir, "file1.txt");
  final File file2 = new File(parentDir, "file2.txt");
  final File file3 = new File(childDir, "file3.txt");
  final File file4 = new File(childDir2, "file4.txt");
  final File file5 = new File(grandChildDir, "file5.txt");
  final File file6 = new File(grandChild2Dir, "file6.txt");
  FileUtils.deleteDirectory(grandParentDir);
  grandChildDir.mkdirs();
  grandChild2Dir.mkdirs();
  FileUtils.writeStringToFile(file1, "File 1 in grandparent", "UTF8");
  FileUtils.writeStringToFile(file2, "File 2 in parent", "UTF8");
  FileUtils.writeStringToFile(file3, "File 3 in child", "UTF8");
  FileUtils.writeStringToFile(file4, "File 4 in child2", "UTF8");
  FileUtils.writeStringToFile(file5, "File 5 in grandChild", "UTF8");
  FileUtils.writeStringToFile(file6, "File 6 in grandChild2", "UTF8");
}

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

private File createZip(String subDirectoryName) throws IOException {
  File first = new File(artifactDest, "first");
  FileUtils.writeStringToFile(first, "First File", UTF_8);
  File second = new File(artifactDest, subDirectoryName + "/second");
  FileUtils.writeStringToFile(second, "Second File", UTF_8);
  new ZipUtil().zip(artifactDest, zip, 0);
  return zip;
}

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

private void enableAuthentication() throws IOException {
  File confFile = new File(repo.projectRepositoryRoot(), "conf/svnserve.conf");
  String passwd = "[general]\n"
      + "anon-access = none\n"
      + "auth-access = read\n"
      + "auth-access = write\n"
      + "password-db = passwd\n";
  FileUtils.writeStringToFile(confFile, passwd, UTF_8);
}

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

public File writeFileWithContent(String relativePath, String content) throws Exception
{
  File dest = new File(directory,relativePath);
  FileUtil.createParentFolderIfNotExist(dest);
  FileUtils.writeStringToFile(dest, content, UTF_8);
  return dest;
}

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

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

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

@Test
public void testChecksumCRC32() throws Exception {
  // create a test file
  final String text = "Imagination is more important than knowledge - Einstein";
  final File file = new File(getTestDirectory(), "checksum-test.txt");
  FileUtils.writeStringToFile(file, text, "US-ASCII");
  // compute the expected checksum
  final Checksum expectedChecksum = new CRC32();
  expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length());
  final long expectedValue = expectedChecksum.getValue();
  // compute the checksum of the file
  final long resultValue = FileUtils.checksumCRC32(file);
  assertEquals(expectedValue, resultValue);
}

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

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

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

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

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

@Test
public void shouldUploadArtifactChecksumForADirectory() throws IOException {
  String data = "Some text whose checksum can be asserted";
  String secondData = "some more";
  FileUtils.writeStringToFile(tempFile, data, UTF_8);
  File anotherFile = new File(artifactFolder, "bond/james_bond/another_file");
  FileUtils.writeStringToFile(anotherFile, secondData, UTF_8);
  when(httpService.upload(eq("http://baseurl/artifacts/dest?attempt=1&buildId=build42"), eq(FileUtils.sizeOfDirectory(artifactFolder)), any(File.class), eq(expectedProperties(data, secondData)))).thenReturn(HttpServletResponse.SC_OK);
  artifactsRepository.upload(console, artifactFolder, "dest", "build42");
}

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

public void addUser(String username, String password) throws Exception {
  enableAuthentication();
  File passwdFile = new File(repo.projectRepositoryRoot(), "conf/passwd");
  String passwd = String.join(FileUtil.lineSeparator(), Files.readAllLines(passwdFile.toPath()));
  if (!(passwd.contains("\n[users]\n"))) {
    passwd = passwd + "\n[users]\n";
  }
  passwd = passwd + String.format("\n%s = %s\n", username, password);
  FileUtils.writeStringToFile(passwdFile, passwd, UTF_8);
}

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

@Test
public void shouldUploadArtifactChecksumForADirectory() throws IOException {
  String data = "Some text whose checksum can be asserted";
  String secondData = "some more";
  FileUtils.writeStringToFile(tempFile, data, UTF_8);
  File anotherFile = new File(artifactFolder, "bond/james_bond/another_file");
  FileUtils.writeStringToFile(anotherFile, secondData, UTF_8);
  when(httpService.upload(any(String.class), eq(FileUtils.sizeOfDirectory(artifactFolder)), any(File.class), eq(expectedProperties(data, secondData)))).thenReturn(HttpServletResponse.SC_OK);
  goArtifactsManipulatorStub.publish(goPublisher, "dest", artifactFolder, jobIdentifier);
}

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

@Test
public void testChecksum() throws Exception {
  // create a test file
  final String text = "Imagination is more important than knowledge - Einstein";
  final File file = new File(getTestDirectory(), "checksum-test.txt");
  FileUtils.writeStringToFile(file, text, "US-ASCII");
  // compute the expected checksum
  final Checksum expectedChecksum = new CRC32();
  expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length());
  final long expectedValue = expectedChecksum.getValue();
  // compute the checksum of the file
  final Checksum testChecksum = new CRC32();
  final Checksum resultChecksum = FileUtils.checksum(file, testChecksum);
  final long resultValue = resultChecksum.getValue();
  assertSame(testChecksum, resultChecksum);
  assertEquals(expectedValue, resultValue);
}

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

@Test(expected = FileAccessRightsCheckException.class)
public void shouldFailGracefullyWhenBundledFileCannotBeRead() throws Exception {
  SystemEnvironment systemEnvironmentFail = mock(SystemEnvironment.class);
  when(systemEnvironmentFail.get(PLUGIN_GO_PROVIDED_PATH)).thenReturn("");
  when(systemEnvironmentFail.get(PLUGIN_EXTERNAL_PROVIDED_PATH)).thenReturn(externalPluginsDir.getAbsolutePath());
  when(systemEnvironmentFail.get(ALL_PLUGINS_ZIP_PATH)).thenReturn("");
  FileUtils.writeStringToFile(new File(externalPluginsDir, "external-task-1.jar"), "External1", UTF_8);
  PluginsZip pluginsZipFail = new PluginsZip(systemEnvironmentFail, pluginManager);
  pluginsZipFail.create();
}

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

@Test(expected = FileAccessRightsCheckException.class)
public void shouldFailGracefullyWhenExternalFileCannotBeRead() throws Exception {
  File bundledPluginsDir = temporaryFolder.newFolder("plugins-bundled-ext");
  SystemEnvironment systemEnvironmentFail = mock(SystemEnvironment.class);
  when(systemEnvironmentFail.get(PLUGIN_GO_PROVIDED_PATH)).thenReturn(bundledPluginsDir.getAbsolutePath());
  when(systemEnvironmentFail.get(PLUGIN_EXTERNAL_PROVIDED_PATH)).thenReturn("");
  when(systemEnvironmentFail.get(ALL_PLUGINS_ZIP_PATH)).thenReturn("");
  FileUtils.writeStringToFile(new File(bundledPluginsDir, "bundled-task-1.jar"), "Bundled1", UTF_8);
  PluginsZip pluginsZipFail = new PluginsZip(systemEnvironmentFail, pluginManager);
  pluginsZipFail.create();
}

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

@Test
public void shouldReplaceJettyXmlIfItDoesNotContainCorrespondingJettyVersionNumber() throws IOException {
  File jettyXml = temporaryFolder.newFile("jetty.xml");
  when(systemEnvironment.getJettyConfigFile()).thenReturn(jettyXml);
  String originalContent = "jetty-v6.2.3\nsome other local changes";
  FileUtils.writeStringToFile(jettyXml, originalContent, UTF_8);
  jetty9Server.replaceJettyXmlIfItBelongsToADifferentVersion(systemEnvironment.getJettyConfigFile());
  assertThat(FileUtils.readFileToString(systemEnvironment.getJettyConfigFile(), UTF_8), is(FileUtils.readFileToString(new File(getClass().getResource("config/jetty.xml").getPath()), UTF_8)));
}

相关文章

FileUtils类方法