File.hx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C)2005-2012 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. import sys.io.FileInput;
  24. /**
  25. API for reading and writing to files.
  26. See `sys.FileSystem` for the complementary file system API.
  27. **/
  28. @:coreApi
  29. class File {
  30. public static function getContent( path : String ) : String
  31. {
  32. var f:python.lib.io.TextIOBase = cast python.lib.Builtin.open(path, "r", -1, "utf-8");
  33. var content = f.read(-1);
  34. f.close();
  35. return content;
  36. }
  37. public static function saveContent( path : String, content : String ) : Void {
  38. var f:python.lib.io.TextIOBase = cast python.lib.Builtin.open(path, "w", -1, "utf-8");
  39. f.write(content);
  40. f.close();
  41. }
  42. public static function getBytes( path : String ) : haxe.io.Bytes {
  43. var f:python.lib.io.RawIOBase = cast python.lib.Builtin.open(path, "rb", -1);
  44. var size = f.read(-1);
  45. var b = haxe.io.Bytes.ofData(size);
  46. f.close();
  47. return b;
  48. }
  49. public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void {
  50. var f:python.lib.io.RawIOBase = cast python.lib.Builtin.open(path, "wb", -1);
  51. f.write(bytes.getData());
  52. f.close();
  53. }
  54. public static function read( path : String, binary : Bool = true ) : FileInput {
  55. var mode = if (binary) "rb" else "r";
  56. var f:python.lib.io.RawIOBase = cast python.lib.Builtin.open(path, mode, -1);
  57. return new FileInput(f);
  58. }
  59. public static function write( path : String, binary : Bool = true ) : FileOutput {
  60. var mode = if (binary) "wb" else "w";
  61. var f:python.lib.io.RawIOBase = cast python.lib.Builtin.open(path, mode, -1);
  62. return new FileOutput(f);
  63. }
  64. public static function append( path : String, binary : Bool = true ) : FileOutput {
  65. var mode = if (binary) "ab" else "a";
  66. var f:python.lib.io.RawIOBase = cast python.lib.Builtin.open(path, mode, -1);
  67. return new FileOutput(f);
  68. }
  69. public static function copy( srcPath : String, dstPath : String ) : Void
  70. {
  71. return python.lib.ShUtil.copy(srcPath, dstPath);
  72. }
  73. }