docs
This commit is contained in:
@@ -77,7 +77,7 @@
|
||||
<li><span class="stability_undefined"><a href="#files_files_isemptydir_path">files.isEmptyDir(path)</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_files_join_parent_child">files.join(parent, child)</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_files_create_path">files.create(path)</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_files_createifnotexists_path">files.createIfNotExists(path)</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_files_createwithdirs_path">files.createWithDirs(path)</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_files_exists_path">files.exists(path)</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_files_ensuredir_path">files.ensureDir(path)</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_files_read_path_encoding_utf_8">files.read(path[, encoding = "utf-8"])</a></span></li>
|
||||
@@ -96,6 +96,7 @@
|
||||
<li><span class="stability_undefined"><a href="#files_files_remove_path">files.remove(path)</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_files_removedir_path">files.removeDir(path)</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_files_getsdcardpath">files.getSdcardPath()</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_files_cwd">files.cwd()</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_files_listdir_path_filter">files.listDir(path[, filter])</a></span></li>
|
||||
<li><span class="stability_undefined"><a href="#files_open_path_mode_r_encoding_utf_8_buffersize_8192">open(path[, mode = "r", encoding = "utf-8", bufferSize = 8192])</a></span></li>
|
||||
</ul>
|
||||
@@ -123,68 +124,97 @@
|
||||
<div id="apicontent">
|
||||
<h1>Files<span><a class="mark" href="#files_files" id="files_files">#</a></span></h1>
|
||||
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>files模块提供了一些常见的文件处理,包括文件读写、移动、复制、删掉等。</p>
|
||||
<p>一次性的文件读写可以直接使用<code>files.read()</code>, <code>files.write()</code>, <code>files.append()</code>等方便的函数,但如果需要频繁读写或随机读写,则使用<code>open()</code>函数打开一个文件对象来操作文件,并在操作完毕后调用<code>close()</code>函数关闭文件。</p>
|
||||
<h2>files.isFile(path)<span><a class="mark" href="#files_files_isfile_path" id="files_files_isfile_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>返回路径path是否是文件。</p>
|
||||
<h2>files.isDir(path)<span><a class="mark" href="#files_files_isdir_path" id="files_files_isdir_path">#</a></span></h2>
|
||||
<pre><code>log(files.isDir("/sdcard/文件夹/")); //返回false
|
||||
log(files.isDir("/sdcard/文件.txt")); //返回true
|
||||
</code></pre><h2>files.isDir(path)<span><a class="mark" href="#files_files_isdir_path" id="files_files_isdir_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>返回路径path是否是文件夹。</p>
|
||||
<h2>files.isEmptyDir(path)<span><a class="mark" href="#files_files_isemptydir_path" id="files_files_isemptydir_path">#</a></span></h2>
|
||||
<pre><code>log(files.isDir("/sdcard/文件夹/")); //返回true
|
||||
log(files.isDir("/sdcard/文件.txt")); //返回false
|
||||
</code></pre><h2>files.isEmptyDir(path)<span><a class="mark" href="#files_files_isemptydir_path" id="files_files_isemptydir_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>返回文件夹path是否为空文件夹。如果该路径并非文件夹,则直接返回false。</p>
|
||||
</div><p>返回文件夹path是否为空文件夹。如果该路径并非文件夹,则直接返回<code>false</code>。</p>
|
||||
<h2>files.join(parent, child)<span><a class="mark" href="#files_files_join_parent_child" id="files_files_join_parent_child">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 父目录路径</li>
|
||||
<li><code>child</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 子路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
||||
</ul>
|
||||
</div><p>连接两个路径并返回,例如<code>files.join("/sdcard/", "1.txt")</code>返回"/sdcard/1.txt"。</p>
|
||||
<h2>files.create(path)<span><a class="mark" href="#files_files_create_path" id="files_files_create_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>创建一个文件并返回是否创建成功。</p>
|
||||
<h2>files.createIfNotExists(path)<span><a class="mark" href="#files_files_createifnotexists_path" id="files_files_createifnotexists_path">#</a></span></h2>
|
||||
</div><p>创建一个文件或文件夹并返回是否创建成功。如果文件已经存在,则直接返回<code>false</code>。</p>
|
||||
<pre><code>files.create("/sdcard/新文件夹/");
|
||||
</code></pre><h2>files.createWithDirs(path)<span><a class="mark" href="#files_files_createwithdirs_path" id="files_files_createwithdirs_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>创建一个文件并返回是否创建成功。</p>
|
||||
<h2>files.exists(path)<span><a class="mark" href="#files_files_exists_path" id="files_files_exists_path">#</a></span></h2>
|
||||
</div><p>创建一个文件或文件夹并返回是否创建成功。如果文件所在文件夹不存在,则先创建他所在的一系列文件夹。如果文件已经存在,则直接返回<code>false</code>。</p>
|
||||
<pre><code>files.createWithDirs("/sdcard/新文件夹/新文件夹/新文件夹/1.txt");
|
||||
</code></pre><h2>files.exists(path)<span><a class="mark" href="#files_files_exists_path" id="files_files_exists_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>返回在路径path处的文件是否存在。</p>
|
||||
<h2>files.ensureDir(path)<span><a class="mark" href="#files_files_ensuredir_path" id="files_files_ensuredir_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
</ul>
|
||||
</div><p>确保路径path所在的文件夹存在。</p>
|
||||
</div><p>确保路径path所在的文件夹存在。如果该路径所在文件夹不存在,则创建该文件夹。</p>
|
||||
<p>例如对于路径"/sdcard/Download/ABC/1.txt",如果/Download/文件夹不存在,则会先创建Download,再创建ABC文件夹。</p>
|
||||
<h2>files.read(path[, encoding = "utf-8"])<span><a class="mark" href="#files_files_read_path_encoding_utf_8" id="files_files_read_path_encoding_utf_8">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 字符编码</li>
|
||||
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 字符编码,可选,默认为utf-8</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
||||
</ul>
|
||||
</div><p>读取文本文件path的所有内容并返回一个字符串。</p>
|
||||
<h2>files.readBytes(path)<span><a class="mark" href="#files_files_readbytes_path" id="files_files_readbytes_path">#</a></span></h2>
|
||||
</div><p>读取文本文件path的所有内容并返回。如果文件不存在,则抛出<code>FileNotFoundException</code>。</p>
|
||||
<pre><code>log(files.read("/sdcard/1.txt"));
|
||||
</code></pre><h2>files.readBytes(path)<span><a class="mark" href="#files_files_readbytes_path" id="files_files_readbytes_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <span class="type"><byte[]></span></li>
|
||||
</ul>
|
||||
</div><p>读取文件path的所有内容并返回一个字节数组。</p>
|
||||
<p>注意,该数组是Java的数组,不具有JavaScript数组的函数。</p>
|
||||
<h2>files.write(path, text[, encoding = "utf-8"])<span><a class="mark" href="#files_files_write_path_text_encoding_utf_8" id="files_files_write_path_text_encoding_utf_8">#</a></span></h2>
|
||||
</div><p>读取文件path的所有内容并返回一个字节数组。如果文件不存在,则抛出<code>FileNotFoundException</code>。</p>
|
||||
<p>注意,该数组是Java的数组,不具有JavaScript数组的forEach, slice等函数。</p>
|
||||
<p>一个以16进制形式打印文件的例子如下:</p>
|
||||
<pre><code>var data = files.readBytes("/sdcard/1.png");
|
||||
var sb = new java.lang.StringBuilder();
|
||||
for(var i = 0; i < data.length; i++){
|
||||
sb.append(data[i].toString(16));
|
||||
}
|
||||
log(sb.toString());
|
||||
</code></pre><h2>files.write(path, text[, encoding = "utf-8"])<span><a class="mark" href="#files_files_write_path_text_encoding_utf_8" id="files_files_write_path_text_encoding_utf_8">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li><code>text</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 要写入的文本内容</li>
|
||||
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 字符编码</li>
|
||||
</ul>
|
||||
</div><p>把text写入到文件path中。如果文件存在则覆盖,不存在则创建。</p>
|
||||
<h2>files.writeBytes(path, bytes)<span><a class="mark" href="#files_files_writebytes_path_bytes" id="files_files_writebytes_path_bytes">#</a></span></h2>
|
||||
<pre><code>var text = "文件内容";
|
||||
//写入文件
|
||||
files.write("/sdcard/1.txt", text);
|
||||
//用其他应用查看文件
|
||||
app.viewFile("/sdcard/1.txt");
|
||||
</code></pre><h2>files.writeBytes(path, bytes)<span><a class="mark" href="#files_files_writebytes_path_bytes" id="files_files_writebytes_path_bytes">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li><code>bytes</code> <span class="type"><byte[]></span> 字节数组,要写入的二进制数据</li>
|
||||
@@ -197,7 +227,12 @@
|
||||
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 字符编码</li>
|
||||
</ul>
|
||||
</div><p>把text追加到文件path的末尾。如果文件不存在则创建。</p>
|
||||
<h2>files.appendBytes(path, text[, encoding = 'utf-8'])<span><a class="mark" href="#files_files_appendbytes_path_text_encoding_utf_8" id="files_files_appendbytes_path_text_encoding_utf_8">#</a></span></h2>
|
||||
<pre><code>var text = "追加的文件内容";
|
||||
files.append("/sdcard/1.txt", text);
|
||||
files.append("/sdcard/1.txt", text);
|
||||
//用其他应用查看文件
|
||||
app.viewFile("/sdcard/1.txt");
|
||||
</code></pre><h2>files.appendBytes(path, text[, encoding = 'utf-8'])<span><a class="mark" href="#files_files_appendbytes_path_text_encoding_utf_8" id="files_files_appendbytes_path_text_encoding_utf_8">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li><code>bytes</code> <span class="type"><byte[]></span> 字节数组,要写入的二进制数据</li>
|
||||
@@ -207,75 +242,99 @@
|
||||
<div class="signature"><ul>
|
||||
<li><code>fromPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 要复制的原文件路径</li>
|
||||
<li><code>toPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 复制到的文件路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>复制文件。例如<code>files.copy("/sdcard/1.txt", "/sdcard/Download/1.txt")</code>。</p>
|
||||
</div><p>复制文件,返回是否复制成功。例如<code>files.copy("/sdcard/1.txt", "/sdcard/Download/1.txt")</code>。</p>
|
||||
<h2>files.move(fromPath, toPath)<span><a class="mark" href="#files_files_move_frompath_topath" id="files_files_move_frompath_topath">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>fromPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 要移动的原文件路径</li>
|
||||
<li><code>toPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 移动到的文件路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>移动文件,返回是否移动成功。例如<code>files.move("/sdcard/1.txt", "/sdcard/Download/1.txt")</code>会把1.txt文件从sd卡根目录移动到Download文件夹。</p>
|
||||
<h2>files.rename(path, newName)<span><a class="mark" href="#files_files_rename_path_newname" id="files_files_rename_path_newname">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 要重命名的原文件路径</li>
|
||||
<li><code>newName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 要重命名的新文件名</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>重命名文件,并返回是否重命名成功。例如<code>files.rename("/sdcard/1.txt", "2.txt")</code>。</p>
|
||||
<h2>files.renameWithoutExtension(path, newName)<span><a class="mark" href="#files_files_renamewithoutextension_path_newname" id="files_files_renamewithoutextension_path_newname">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 要重命名的原文件路径</li>
|
||||
<li><code>newName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 要重命名的新文件名</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>重命名文件,不包含拓展名,并返回是否重命名成功。例如<code>files.rename("/sdcard/1.txt", "2")</code>会把1.txt重命名为2.txt。</p>
|
||||
</div><p>重命名文件,不包含拓展名,并返回是否重命名成功。例如<code>files.rename("/sdcard/1.txt", "2")</code>会把"1.txt"重命名为"2.txt"。</p>
|
||||
<h2>files.getName(path)<span><a class="mark" href="#files_files_getname_path" id="files_files_getname_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
||||
</ul>
|
||||
</div><p>返回文件的文件名。例如<code>files.getName("/sdcard/1.txt")</code>返回"1.txt"。</p>
|
||||
<h2>files.getNameWithoutExtension(path)<span><a class="mark" href="#files_files_getnamewithoutextension_path" id="files_files_getnamewithoutextension_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
||||
</ul>
|
||||
</div><p>返回不含拓展名的文件的文件名。例如<code>files.getName("/sdcard/1.txt")</code>返回"1"。</p>
|
||||
<h2>files.getExtension(path)<span><a class="mark" href="#files_files_getextension_path" id="files_files_getextension_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
||||
</ul>
|
||||
</div><p>返回文件的拓展名。例如<code>files.getExtension("/sdcard/1.txt")</code>返回"txt"。</p>
|
||||
<h2>files.remove(path)<span><a class="mark" href="#files_files_remove_path" id="files_files_remove_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>删除文件或<strong>空文件夹</strong>,返回是否删除成功。</p>
|
||||
<h2>files.removeDir(path)<span><a class="mark" href="#files_files_removedir_path" id="files_files_removedir_path">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
||||
</ul>
|
||||
</div><p>删除文件夹,如果文件夹不为空,则删除该文件夹的所有内容再删除该文件夹,返回是否全部删除成功。</p>
|
||||
<h2>files.getSdcardPath()<span><a class="mark" href="#files_files_getsdcardpath" id="files_files_getsdcardpath">#</a></span></h2>
|
||||
<p>返回SD卡路径。所谓SD卡,即外部存储器。</p>
|
||||
<div class="signature"><ul>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
||||
</ul>
|
||||
</div><p>返回SD卡路径。所谓SD卡,即外部存储器。</p>
|
||||
<h2>files.cwd()<span><a class="mark" href="#files_files_cwd" id="files_files_cwd">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
||||
</ul>
|
||||
</div><p>返回脚本的"当前工作文件夹路径"。该路径指的是,如果脚本本身为脚本文件,则返回这个脚本文件所在目录;否则返回<code>null</code>获取其他设定路径。</p>
|
||||
<p>例如,对于脚本文件"/sdcard/脚本/1.js"运行<code>files.cwd()</code>返回"/sdcard/脚本/"。</p>
|
||||
<h2>files.listDir(path[, filter])<span><a class="mark" href="#files_files_listdir_path_filter" id="files_files_listdir_path_filter">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 路径</li>
|
||||
<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 过滤函数,可选。接收一个String参数(文件名),返回一个Boolean值。</li>
|
||||
<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 过滤函数,可选。接收一个<code>string</code>参数(文件名),返回一个<code>boolean</code>值。</li>
|
||||
</ul>
|
||||
</div><p>列出文件夹path下的满足条件的文件和文件夹的名称的数组。如果不加filter参数,则返回所有文件和文件夹。</p>
|
||||
<p>例如,获取sdcard目录下的txt文件为</p>
|
||||
<pre><code>var txtFiles = files.listDir("/sdcard/", function(name){
|
||||
return name.endsWith(".txt") && files.isFile("/sdcard/" + name);
|
||||
<p>列出sdcard目录下所有文件和文件夹为:</p>
|
||||
<pre><code>var arr = files.listDir("/sdcard/");
|
||||
log(arr);
|
||||
</code></pre><p>列出脚本目录下所有js脚本文件为:</p>
|
||||
<pre><code>var dir = "/sdcard/脚本/";
|
||||
var jsFiles = files.listDir(dir, function(name){
|
||||
return name.endsWith(".js") && files.isFile(files.join(dir, name));
|
||||
});
|
||||
log(jsFiles);
|
||||
</code></pre><h2>open(path[, mode = "r", encoding = "utf-8", bufferSize = 8192])<span><a class="mark" href="#files_open_path_mode_r_encoding_utf_8_buffersize_8192" id="files_open_path_mode_r_encoding_utf_8_buffersize_8192">#</a></span></h2>
|
||||
<div class="signature"><ul>
|
||||
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 文件路径,例如"/sdcard/1.txt"。</li>
|
||||
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 文件打开模式,包括:<ul>
|
||||
<li>"r": 只读模式。该模式下只能对文件执行<strong>文本</strong>读取操作。</li>
|
||||
<li>"w": 只写模式。该模式下只能对文件执行<strong>文本</strong>覆盖写入操作。</li>
|
||||
<li>"a": 附加模式。该模式下将会把写入的文本附加到文件末尾。<br>目前暂不支持二进制模式,随机读写模式。</li>
|
||||
<li>"r": 只读文本模式。该模式下只能对文件执行<strong>文本</strong>读取操作。</li>
|
||||
<li>"w": 只写文本模式。该模式下只能对文件执行<strong>文本</strong>覆盖写入操作。</li>
|
||||
<li>"a": 附加文本模式。该模式下将会把写入的文本附加到文件末尾。 </li>
|
||||
<li>"rw": 随机读写文本模式。该模式下将会把写入的文本附加到文件末尾。<br>目前暂不支持二进制模式,随机读写模式。</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 字符编码。</li>
|
||||
<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><Number></a> 文件读写的缓冲区大小。</li>
|
||||
<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> 文件读写的缓冲区大小。</li>
|
||||
</ul>
|
||||
</div><p>打开一个文件。根据打开模式返回不同的文件对象。包括:</p>
|
||||
<ul>
|
||||
|
||||
Reference in New Issue
Block a user