71 lines
1.7 KiB
C++
71 lines
1.7 KiB
C++
#ifndef B2D3FCFE_BBC5_4EFD_9AA7_7EEB5C58E2AE
|
|
#define B2D3FCFE_BBC5_4EFD_9AA7_7EEB5C58E2AE
|
|
#include <Poco/File.h>
|
|
#include <Poco/FileStream.h>
|
|
#include <Poco/StreamCopier.h>
|
|
#include <string>
|
|
#include "Logger.h"
|
|
namespace Util
|
|
{
|
|
class FileUtil
|
|
{
|
|
private:
|
|
public:
|
|
static std::string readFile(const std::string& fileFullName)
|
|
{
|
|
std::string content;
|
|
Poco::File file(fileFullName);
|
|
if (file.exists())
|
|
{
|
|
try
|
|
{
|
|
Poco::FileInputStream fis(fileFullName);
|
|
Poco::StreamCopier::copyToString(fis, content);
|
|
fis.close();
|
|
}
|
|
catch (Poco::Exception& e)
|
|
{
|
|
logger().error("readFile read error: %s", std::string(e.what()));
|
|
}
|
|
}
|
|
return content;
|
|
}
|
|
|
|
static bool writeFile(const std::string& fileFullName, const std::string& content)
|
|
{
|
|
try
|
|
{
|
|
Poco::FileOutputStream fos(fileFullName, std::ios::trunc);
|
|
fos << content;
|
|
fos.close();
|
|
|
|
int fd = ::open(fileFullName.c_str(), O_RDWR);
|
|
if (fd != -1)
|
|
{
|
|
::fsync(fd);
|
|
::close(fd);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
catch (Poco::Exception& e)
|
|
{
|
|
logger().error("writeFile file error: %s", std::string(e.what()));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static void delFile(const std::string& fileFullName)
|
|
{
|
|
if (!fileFullName.empty())
|
|
{
|
|
if (Poco::File(fileFullName).exists())
|
|
{
|
|
Poco::File(fileFullName).remove();
|
|
logger().debug("删除文件: %s", fileFullName);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|
|
#endif
|