File.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C)2005-2018 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.io;
  23. @:coreApi
  24. class File {
  25. public static function getContent( path : String ) : String
  26. {
  27. var f = read(path, false);
  28. var ret = f.readAll().toString();
  29. f.close();
  30. return ret;
  31. }
  32. public static function saveContent( path : String, content : String ) : Void
  33. {
  34. var f = write(path, false);
  35. f.writeString(content);
  36. f.close();
  37. }
  38. public static function getBytes( path : String ) : haxe.io.Bytes
  39. {
  40. var f = read(path, true);
  41. var ret = f.readAll();
  42. f.close();
  43. return ret;
  44. }
  45. public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void
  46. {
  47. var f = write(path, true);
  48. f.writeBytes(bytes, 0, bytes.length);
  49. f.close();
  50. }
  51. public static function read( path : String, binary : Bool = true ) : FileInput
  52. {
  53. #if std_buffer //standardize 4kb buffers
  54. var stream = new cs.system.io.FileStream(path, Open, Read, ReadWrite, 4096);
  55. #else
  56. var stream = new cs.system.io.FileStream(path, Open, Read, ReadWrite);
  57. #end
  58. return new FileInput(stream);
  59. }
  60. public static function write( path : String, binary : Bool = true ) : FileOutput
  61. {
  62. #if std_buffer //standardize 4kb buffers
  63. var stream = new cs.system.io.FileStream(path, Create, Write, ReadWrite, 4096);
  64. #else
  65. var stream = new cs.system.io.FileStream(path, Create, Write, ReadWrite);
  66. #end
  67. return new FileOutput(stream);
  68. }
  69. public static function append( path : String, binary : Bool = true ) : FileOutput
  70. {
  71. #if std_buffer //standardize 4kb buffers
  72. var stream = new cs.system.io.FileStream(path, Append, Write, ReadWrite, 4096);
  73. #else
  74. var stream = new cs.system.io.FileStream(path, Append, Write, ReadWrite);
  75. #end
  76. return new FileOutput(stream);
  77. }
  78. public static function update( path : String, binary : Bool = true ) : FileOutput
  79. {
  80. if (!FileSystem.exists(path)) {
  81. write(path).close();
  82. }
  83. #if std_buffer //standardize 4kb buffers
  84. var stream = new cs.system.io.FileStream(path, OpenOrCreate, Write, ReadWrite, 4096);
  85. #else
  86. var stream = new cs.system.io.FileStream(path, OpenOrCreate, Write, ReadWrite);
  87. #end
  88. return new FileOutput(stream);
  89. }
  90. public static function copy( srcPath : String, dstPath : String ) : Void
  91. {
  92. cs.system.io.File.Copy(srcPath, dstPath, true);
  93. }
  94. }