File.hx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C)2005-2019 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. var f = read(path, false);
  27. var ret = f.readAll().toString();
  28. f.close();
  29. return ret;
  30. }
  31. public static function saveContent(path:String, content:String):Void {
  32. var f = write(path, false);
  33. f.writeString(content);
  34. f.close();
  35. }
  36. public static function getBytes(path:String):haxe.io.Bytes {
  37. var f = read(path, true);
  38. var ret = f.readAll();
  39. f.close();
  40. return ret;
  41. }
  42. public static function saveBytes(path:String, bytes:haxe.io.Bytes):Void {
  43. var f = write(path, true);
  44. f.writeBytes(bytes, 0, bytes.length);
  45. f.close();
  46. }
  47. public static function read(path:String, binary:Bool = true):FileInput {
  48. try {
  49. return new FileInput(new java.io.RandomAccessFile(new java.io.File(path), "r"));
  50. } catch (e:Dynamic) // swallow checked exceptions
  51. {
  52. throw e;
  53. }
  54. }
  55. public static function write(path:String, binary:Bool = true):FileOutput {
  56. var f = new java.io.File(path);
  57. if (f.exists()) {
  58. f.delete();
  59. }
  60. try {
  61. return new FileOutput(new java.io.RandomAccessFile(f, "rw"));
  62. } catch (e:Dynamic) // swallow checked exceptions
  63. {
  64. throw e;
  65. }
  66. }
  67. public static function append(path:String, binary:Bool = true):FileOutput {
  68. var f = new java.io.File(path);
  69. try {
  70. var ra = new java.io.RandomAccessFile(f, "rw");
  71. if (f.exists()) {
  72. ra.seek(f.length());
  73. }
  74. return new FileOutput(ra);
  75. } catch (e:Dynamic) // swallow checked exceptions
  76. {
  77. throw e;
  78. }
  79. }
  80. public static function update(path:String, binary:Bool = true):FileOutput {
  81. var f = new java.io.File(path);
  82. try {
  83. var ra = new java.io.RandomAccessFile(f, "rw");
  84. return new FileOutput(ra);
  85. } catch (e:Dynamic) // swallow checked exceptions
  86. {
  87. throw e;
  88. }
  89. }
  90. public static function copy(srcPath:String, dstPath:String):Void {
  91. var r:FileInput = null;
  92. var w:FileOutput = null;
  93. try {
  94. r = read(srcPath);
  95. w = write(dstPath);
  96. w.writeInput(r);
  97. r.close();
  98. w.close();
  99. } catch (e:Dynamic) {
  100. if (r != null)
  101. r.close();
  102. if (w != null)
  103. w.close();
  104. throw e;
  105. }
  106. }
  107. }