整理代码
This commit is contained in:
commit
c7b4e178ec
364 changed files with 59691 additions and 0 deletions
71
util/FileUtil.h
Normal file
71
util/FileUtil.h
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#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
|
||||
Loading…
Add table
Add a link
Reference in a new issue