2
0

File.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. try
  54. {
  55. return new FileInput( new java.io.RandomAccessFile(new java.io.File(path), "r") );
  56. }
  57. catch (e:Dynamic) //swallow checked exceptions
  58. {
  59. throw e;
  60. }
  61. }
  62. public static function write( path : String, binary : Bool = true ) : FileOutput
  63. {
  64. var f = new java.io.File(path);
  65. if (f.exists())
  66. {
  67. f.delete();
  68. }
  69. try
  70. {
  71. return new FileOutput( new java.io.RandomAccessFile(f, "rw") );
  72. }
  73. catch (e:Dynamic) //swallow checked exceptions
  74. {
  75. throw e;
  76. }
  77. }
  78. public static function append( path : String, binary : Bool = true ) : FileOutput
  79. {
  80. var f = new java.io.File(path);
  81. try
  82. {
  83. var ra = new java.io.RandomAccessFile(f, "rw");
  84. if (f.exists())
  85. {
  86. ra.seek(f.length());
  87. }
  88. return new FileOutput( ra );
  89. }
  90. catch (e:Dynamic) //swallow checked exceptions
  91. {
  92. throw e;
  93. }
  94. }
  95. public static function copy( srcPath : String, dstPath : String ) : Void
  96. {
  97. var r:FileInput = null;
  98. var w:FileOutput = null;
  99. try
  100. {
  101. r = read(srcPath);
  102. w = write(dstPath);
  103. w.writeInput(r);
  104. r.close();
  105. w.close();
  106. }
  107. catch (e:Dynamic)
  108. {
  109. if (r != null) r.close();
  110. if (w != null) w.close();
  111. throw e;
  112. }
  113. }
  114. }