Java Code Examples for com.helger.commons.io.file.SimpleFileIO#getFileAsString()

The following examples show how to use com.helger.commons.io.file.SimpleFileIO#getFileAsString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Base64Test.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeFileToFile () throws IOException
{
  final File f1 = new File ("base64.decoded");
  final File f2 = new File ("base64.encoded");
  try
  {
    assertFalse (FileHelper.existsFile (f2));
    SimpleFileIO.writeFile (f1, "Hallo Wält", StandardCharsets.UTF_8);
    Base64.encodeFileToFile (f1.getAbsolutePath (), f2.getAbsoluteFile ());
    assertTrue (FileHelper.existsFile (f2));
    final String sEncoded = SimpleFileIO.getFileAsString (f2, StandardCharsets.UTF_8);
    assertEquals ("Hallo Wält", Base64.safeDecodeAsString (sEncoded, StandardCharsets.UTF_8));
  }
  finally
  {
    FileOperations.deleteFile (f1);
    FileOperations.deleteFile (f2);
  }
}
 
Example 2
Source File: Base64Test.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeToFile () throws IOException
{
  final File f2 = new File ("base64.encoded");
  try
  {
    assertFalse (FileHelper.existsFile (f2));
    final String sDecoded = "Hallo Wält";
    Base64.encodeToFile (sDecoded.getBytes (StandardCharsets.UTF_8), f2.getAbsoluteFile ());
    assertTrue (FileHelper.existsFile (f2));
    final String sEncoded = SimpleFileIO.getFileAsString (f2, StandardCharsets.UTF_8);
    assertEquals ("Hallo Wält", Base64.safeDecodeAsString (sEncoded, StandardCharsets.UTF_8));
  }
  finally
  {
    FileOperations.deleteFile (f2);
  }
}
 
Example 3
Source File: Base64Test.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecodeFileToFile () throws IOException
{
  final File f1 = new File ("base64.encoded");
  final File f2 = new File ("base64.decoded");
  try
  {
    assertFalse (FileHelper.existsFile (f2));
    SimpleFileIO.writeFile (f1,
                            Base64.safeEncode ("Hallo Wält", StandardCharsets.UTF_8)
                                  .getBytes (StandardCharsets.ISO_8859_1));
    Base64.decodeFileToFile (f1.getAbsolutePath (), f2.getAbsoluteFile ());
    assertTrue (FileHelper.existsFile (f2));
    final String sDecoded = SimpleFileIO.getFileAsString (f2, StandardCharsets.UTF_8);
    assertEquals ("Hallo Wält", sDecoded);
  }
  finally
  {
    FileOperations.deleteFile (f1);
    FileOperations.deleteFile (f2);
  }
}
 
Example 4
Source File: Base64Test.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecodeToFile () throws IOException
{
  final File f2 = new File ("base64.decoded");
  try
  {
    assertFalse (FileHelper.existsFile (f2));
    final String sEncoded = Base64.safeEncode ("Hallo Wält", StandardCharsets.UTF_8);
    Base64.decodeToFile (sEncoded, f2.getAbsoluteFile ());
    assertTrue (FileHelper.existsFile (f2));
    final String sDecoded = SimpleFileIO.getFileAsString (f2, StandardCharsets.UTF_8);
    assertEquals ("Hallo Wält", sDecoded);
  }
  finally
  {
    FileOperations.deleteFile (f2);
  }
}
 
Example 5
Source File: FileIntIDFactory.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Override
@MustBeLocked (ELockType.WRITE)
protected final int readAndUpdateIDCounter (@Nonnegative final int nReserveCount)
{
  // Read the old content
  final String sContent = SimpleFileIO.getFileAsString (m_aFile, CHARSET_TO_USE);
  final int nRead = sContent != null ? StringParser.parseInt (sContent.trim (), 0) : 0;

  // Write the new content to the new file
  // This will fail, if the disk is full
  SimpleFileIO.writeFile (m_aNewFile, Integer.toString (nRead + nReserveCount), CHARSET_TO_USE);

  FileIOError aIOError;
  boolean bRenamedToPrev = false;
  if (m_aFile.exists ())
  {
    // Rename the existing file to the prev file
    aIOError = FileOperationManager.INSTANCE.renameFile (m_aFile, m_aPrevFile);
    bRenamedToPrev = true;
  }
  else
    aIOError = new FileIOError (EFileIOOperation.RENAME_FILE, EFileIOErrorCode.NO_ERROR);
  if (aIOError.isSuccess ())
  {
    // Rename the new file to the destination file
    aIOError = FileOperationManager.INSTANCE.renameFile (m_aNewFile, m_aFile);
    if (aIOError.isSuccess ())
    {
      // Finally delete the prev file (may not be existing for fresh
      // instances)
      aIOError = FileOperationManager.INSTANCE.deleteFileIfExisting (m_aPrevFile);
    }
    else
    {
      // 2nd rename failed
      // -> Revert original rename to stay as consistent as possible
      if (bRenamedToPrev)
        FileOperationManager.INSTANCE.renameFile (m_aPrevFile, m_aFile);
    }
  }
  if (aIOError.isFailure ())
    throw new IllegalStateException ("Error on rename(existing-old)/rename(new-existing)/delete(old): " + aIOError);

  return nRead;
}
 
Example 6
Source File: FileLongIDFactory.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Override
protected final long readAndUpdateIDCounter (@Nonnegative final int nReserveCount)
{
  final String sContent = SimpleFileIO.getFileAsString (m_aFile, CHARSET_TO_USE);
  final long nRead = sContent != null ? StringParser.parseLong (sContent.trim (), 0) : 0;

  // Write the new content to the new file
  // This will fail, if the disk is full
  SimpleFileIO.writeFile (m_aNewFile, Long.toString (nRead + nReserveCount), CHARSET_TO_USE);

  FileIOError aIOError;
  boolean bRenamedToPrev = false;
  if (m_aFile.exists ())
  {
    // Rename the existing file to the prev file
    aIOError = FileOperationManager.INSTANCE.renameFile (m_aFile, m_aPrevFile);
    bRenamedToPrev = true;
  }
  else
    aIOError = new FileIOError (EFileIOOperation.RENAME_FILE, EFileIOErrorCode.NO_ERROR);
  if (aIOError.isSuccess ())
  {
    // Rename the new file to the destination file
    aIOError = FileOperationManager.INSTANCE.renameFile (m_aNewFile, m_aFile);
    if (aIOError.isSuccess ())
    {
      // Finally delete the prev file (may not be existing for fresh
      // instances)
      aIOError = FileOperationManager.INSTANCE.deleteFileIfExisting (m_aPrevFile);
    }
    else
    {
      // 2nd rename failed
      // -> Revert original rename to stay as consistent as possible
      if (bRenamedToPrev)
        FileOperationManager.INSTANCE.renameFile (m_aPrevFile, m_aFile);
    }
  }
  if (aIOError.isFailure ())
    throw new IllegalStateException ("Error on rename(existing-old)/rename(new-existing)/delete(old): " + aIOError);

  return nRead;
}