File.hx 3.0 KB

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