将数据读写到文本文件

author author     2023-03-23     404

关键词:

I created this code for practice, and I looking for better and clear solution for the last instance where I can write or delete the data, but after I want to get back the content as array or the key as string. I want to use this write-read back with only one instance.
  1. // The input is a textfile:
  2. // key1=val1
  3. // key2=val2
  4. // key3=val3
  5. // key4=val4
  6. // .
  7. // .
  8. // .
  9.  
  10. // The class:
  11.  
  12. class UserID {
  13. private $UIDPath;
  14. private $UserUIDcontent;
  15.  
  16. public function __construct($UserID, $Action, $UID_KEY, $UID_VAL, $Back) {
  17. $UID_Content = $this -> UIDPath = 'the/path/to/input/text.txt';
  18. $UserUIDcontent = array();
  19.  
  20. if (file_exists($UID_Content) && (is_file($UID_Content))) {
  21. foreach (file($UID_Content) as $UIDlines) {
  22. list($key, $value) = explode('=', $UIDlines, 2) + array(NULL, NULL);
  23. if (trim($value) !== NULL) { $UserUIDcontent[$key] = trim($value); }
  24. }
  25.  
  26. if ($Action == 'Read') {
  27. if ($UID_KEY == NULL) {
  28. $this -> UserUIDcontent = $UserUIDcontent;
  29. } else {
  30. if (array_key_exists($UID_KEY, $UserUIDcontent)) {
  31. $this -> UserUIDcontent = $UserUIDcontent[$UID_KEY];
  32. } else {
  33. if ($UID_KEY == 'User') {
  34. return NULL;
  35. } else {
  36. unset($UserUIDcontent[$UID_KEY]);
  37. echo 'ERROR: The search key: ' . $UID_KEY . ' not available in Class: ' . __METHOD__;
  38. return;
  39. }
  40. }
  41. }
  42. } elseif ($Action == 'Write') {
  43. if (is_array($UID_KEY)) {
  44. if (count($UID_KEY) == count($UID_VAL)) {
  45. $UserUIDcontent = array_filter(array_merge($UserUIDcontent, array_combine($UID_KEY, $UID_VAL)));
  46. } else {
  47. echo 'ERROR: Keys: ' . count($UID_KEY) . ' and Values: ' . count($UID_VAL) . ' are not equal in Class: ' . __METHOD__;
  48. return;
  49. }
  50. } else {
  51. $UserUIDcontent[$UID_KEY] = $UID_VAL;
  52. }
  53. foreach ($UserUIDcontent as $key => $value) {
  54. if ($value !== NULL) {
  55. $UIDcontentTXT .= trim($key) . '=' . trim($value) . PHP_EOL;
  56. }
  57. }
  58. trim($UIDcontentTXT);
  59. $handle = fopen($UID_Content, "w");
  60. fwrite($handle, $UIDcontentTXT, strlen($UIDcontentTXT));
  61. fclose($handle);
  62.  
  63. if ($Back != NULL) {
  64. if (array_key_exists($Back, $UserUIDcontent)) {
  65. return $this -> $UserUIDcontent = $UserUIDcontent[$Back];
  66. } else {
  67. return $this -> $UserUIDcontent = $UserUIDcontent;
  68. }
  69. } else {
  70. return $this -> $UserUIDcontent = NULL;
  71. }
  72. } else {
  73. echo 'ERROR: The second parameter ' . $Action . ' not allowed in Class: ' . __METHOD__;
  74. return;
  75. }
  76. } else {
  77. echo 'ERROR: Required data source not found for Class: ' . __METHOD__;
  78. return;
  79. }
  80. }
  81.  
  82. public function result() {
  83. return $this -> UserUIDcontent;
  84. }
  85. }
  86.  
  87. // ---------------------------------
  88. // instances:
  89. // ---------------------------------
  90.  
  91. $UserData = new UserID($UserID, 'Read', NULL, NULL, NULL);
  92. $array = $UserData -> result(); // Read full array
  93. $val = $UserData -> result()[key2]; // Read one data
  94.  
  95. $UserData = new UserID($UserID, 'Read', 'key2', NULL, NULL);
  96. $val = $UserData -> result(); // Read one data
  97.  
  98. $UserData = new UserID($UserID, 'Write', array('key2', 'aaaaaaaaa', 'User'), array(NULL, 'bbbbbbbb', NULL), NULL);
  99. $UserData; // write or delete more than one data by array
  100.  
  101. $UserData = new UserID($UserID, 'Write', 'key2', NULL, NULL);
  102. $UserData; // write or delete one data
  103.  
  104. // this is the problem:
  105. $UserData = new UserID($UserID, 'Write', array('User', 'key2', 'key3', 'key15'), array('aaaa', NULL, 'aaa', 'value15'), 1);
  106. $array = $UserData -> Array; // Write AND Read back full array or one key

c++文件读写操作逐字符读取文本和逐行读取文本(代码片段)

...一行内容 C++文件读写操作(四)读取文件数据到临时 查看详情

文本读写与二进制读写速度对比pythonmatlab(代码片段)

...言Python对比方法测试结果Matlab对比方法测试结果总结引言数据读写的速度往往会占用大量的时间,因此对Python和Matlab中不同的数据输出方式的速度进行了简单对比。这是因为最近在写代码的过程中遇到一个问题,需要跨... 查看详情

c++:文件操作|读写文本文件(代码片段)

...文件操作类型2写文本文件3读文本文件程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放,通过文件可以将数据持久化。C++中对文件操作需要包含头文件<fstream>1文本文件操作类型ofstream:... 查看详情

通过消除重复将数据从文本文件加载到mysql数据库

】通过消除重复将数据从文本文件加载到mysql数据库【英文标题】:Loadingdatafromtextfiletomysqldatabasebyeliminatingduplicates【发布时间】:2011-12-1405:56:49【问题描述】:我想将数据从文本文件加载到数据库,如果数据已经存在,我需要在... 查看详情

将文本框中的数据保存到文本文件中

】将文本框中的数据保存到文本文件中【英文标题】:Savingdatafromtextboxesintotextfile【发布时间】:2014-04-0223:35:32【问题描述】:我正在尝试为我编写一个程序应该比它证明的要容易得多。我有一个带有文本框的表单,要求用户输... 查看详情

使用 SaveFileDialog 将数据保存到文本文件?

】使用SaveFileDialog将数据保存到文本文件?【英文标题】:SaveDatatotexttileusingSaveFileDialog?【发布时间】:2013-03-1421:31:37【问题描述】:我已经查看了MSDNExample,但我仍然遇到问题。我创建了一个超级简单的程序来将两个数字相乘,... 查看详情

使用python将整个文本文件加载到数据库中

】使用python将整个文本文件加载到数据库中【英文标题】:Loadentiretextfileindatabaseusingpython【发布时间】:2015-02-1607:06:01【问题描述】:我想将具有由分隔符||分隔的两个字段的整个文本文件加载到我的netezza数据库中。由于数据集... 查看详情

oracle 将文本文件中的数据加载到表中

】oracle将文本文件中的数据加载到表中【英文标题】:Loadingdatafromatextfiletoatableinoracle【发布时间】:2010-09-2010:30:41【问题描述】:我有2个问题。除了使用SQL加载器之外,还有其他方法可以将数据从.txt文件加载到Oracle中的表吗?... 查看详情

使用 oracle sql developer 将 utf-8 数据导出到文本文件

】使用oraclesqldeveloper将utf-8数据导出到文本文件【英文标题】:exportutf-8datatotextfilewithoraclesqldeveloper【发布时间】:2009-11-2603:52:01【问题描述】:我需要以插入语句的形式将urf-8数据导出到文本文件。我已经尝试过OracleSQL开发人员... 查看详情

如何将文本文件中的不同数据类型添加到数组中?

】如何将文本文件中的不同数据类型添加到数组中?【英文标题】:HowdoIadddifferentdatatypesfromtextfileintoanarray?【发布时间】:2022-01-1105:16:27【问题描述】:我正在尝试将文本文件中的这些数据类型添加到数组中,但出现超出范围的... 查看详情

将文本文件中的数据提取到结构中

】将文本文件中的数据提取到结构中【英文标题】:Pulldatafromtextfileintoastruct【发布时间】:2013-10-1207:39:23【问题描述】:我目前在尝试使用结构从文本文件中提取数据然后将其存储到向量中时遇到问题。但无论我做什么,除非... 查看详情

如何将GUI中的数据保存到文本文件(java)

】如何将GUI中的数据保存到文本文件(java)【英文标题】:HowtosavethedataintheGUItothetextfile(java)【发布时间】:2021-06-0613:34:21【问题描述】:我尝试将数据(用户将在TextField中输入的数据)保存到文本文件中。但是它只会说错误。... 查看详情

如何将文本文件中的原始数据加载到熊猫数据框中?

】如何将文本文件中的原始数据加载到熊猫数据框中?【英文标题】:howtoloadrawdatainatextfileintopandasdataframe?【发布时间】:2018-05-1106:13:58【问题描述】:我的数据在一个文本文件中,格式如下:标题1:废话标题2:废话heading3:bla... 查看详情

尝试将抓取的 Web 数据保存到文本文件时出错

】尝试将抓取的Web数据保存到文本文件时出错【英文标题】:Errortryingtosavescrapedwebdatatoatextfile【发布时间】:2022-01-1705:25:28【问题描述】:我是新手,最近开始使用Python。我正在尝试将检索到的Twitter关注者从网络保存到文本文... 查看详情

如何将文本文件中的数据复制到剪贴板? [复制]

】如何将文本文件中的数据复制到剪贴板?[复制]【英文标题】:Howtocopydatainatextfiletotheclipboard?[duplicate]【发布时间】:2015-08-1306:10:03【问题描述】:我想将文本文件中的数据复制到剪贴板。例如,这与您可以在记事本中打开的... 查看详情

将每个 excel 列数据保存到单独的文本文件中

】将每个excel列数据保存到单独的文本文件中【英文标题】:Savingeachexcelcolumndatatoseparatetextfiles【发布时间】:2018-12-0814:14:04【问题描述】:有没有一种简单的方法可以将填充在N列中的Excel数据保存到N个相应的文本文件中?如果N... 查看详情

从文本文件加载数据然后将其存储到数据库中的最快方法

】从文本文件加载数据然后将其存储到数据库中的最快方法【英文标题】:Fastestwaytoloaddatafromtextfilethenstoreitintodatabase【发布时间】:2011-02-1113:56:22【问题描述】:我有问题。我正在开发一个项目,但我被困在这部分:我想从文... 查看详情

将数据作为文本文件从 spark 保存到 hdfs

】将数据作为文本文件从spark保存到hdfs【英文标题】:Savedataastextfilefromsparktohdfs【发布时间】:2016-07-2805:03:28【问题描述】:我使用pySpark和sqlContext使用以下查询处理数据:(sqlContext.sql("selectLastUpdate,Count(1)asCount"fromtemp_t).rdd.coalesc... 查看详情