Dan*_*ahy 150
在清单中设置正确的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
下面是一个以编程方式移动文件的函数
private void moveFile(String inputPath, String inputFile, String outputPath) {
InputStream in = null;
OutputStream out = null;
try {
//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
}
in = new FileInputStream(inputPath + inputFile);
out = new FileOutputStream(outputPath + inputFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file
out.flush();
out.close();
out = null;
// delete the original file
new File(inputPath + inputFile).delete();
}
catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
}
catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
删除文件使用
private void deleteFile(String inputPath, String inputFile) {
try {
// delete the original file
new File(inputPath + inputFile).delete();
}
catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
复印
private void copyFile(String inputPath, String inputFile, String outputPath) {
InputStream in = null;
OutputStream out = null;
try {
//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
}
in = new FileInputStream(inputPath + inputFile);
out = new FileOutputStream(outputPath + inputFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file (You have now copied the file)
out.flush();
out.close();
out = null;
} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
}
catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
Won*_*res 133
移动文件:
File from = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/kaic1/imagem.jpg");
File to = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/kaic2/imagem.jpg");
from.renameTo(to);
Run Code Online (Sandbox Code Playgroud)
asi*_*ura 36
移动文件的功能:
private void moveFile(File file, File dir) throws IOException {
File newFile = new File(dir, file.getName());
FileChannel outputChannel = null;
FileChannel inputChannel = null;
try {
outputChannel = new FileOutputStream(newFile).getChannel();
inputChannel = new FileInputStream(file).getChannel();
inputChannel.transferTo(0, inputChannel.size(), outputChannel);
inputChannel.close();
file.delete();
} finally {
if (inputChannel != null) inputChannel.close();
if (outputChannel != null) outputChannel.close();
}
}
Run Code Online (Sandbox Code Playgroud)
Com*_*are 24
使用标准的Java I/O.用于Environment.getExternalStorageDirectory()
访问外部存储的根目录(在某些设备上是SD卡).
dug*_*ggu 16
删除
public static void deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
deleteRecursive(child);
fileOrDirectory.delete();
}
Run Code Online (Sandbox Code Playgroud)
检查此链接以获取上述功能.
复制
public static void copyDirectoryOneLocationToAnotherLocation(File sourceLocation, File targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i = 0; i < sourceLocation.listFiles().length; i++) {
copyDirectoryOneLocationToAnotherLocation(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
Run Code Online (Sandbox Code Playgroud)
移动
移动只是将文件夹的一个位置复制到另一个位置然后删除它的文件夹
表现
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
Ken*_*Ken 10
权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)获取SD卡根文件夹:
Environment.getExternalStorageDirectory()
Run Code Online (Sandbox Code Playgroud)删除文件:这是关于如何删除根文件夹中所有空文件夹的示例:
public static void deleteEmptyFolder(File rootFolder){
if (!rootFolder.isDirectory()) return;
File[] childFiles = rootFolder.listFiles();
if (childFiles==null) return;
if (childFiles.length == 0){
rootFolder.delete();
} else {
for (File childFile : childFiles){
deleteEmptyFolder(childFile);
}
}
}
Run Code Online (Sandbox Code Playgroud)拷贝文件:
public static void copyFile(File src, File dst) throws IOException {
FileInputStream var2 = new FileInputStream(src);
FileOutputStream var3 = new FileOutputStream(dst);
byte[] var4 = new byte[1024];
int var5;
while((var5 = var2.read(var4)) > 0) {
var3.write(var4, 0, var5);
}
var2.close();
var3.close();
}
Run Code Online (Sandbox Code Playgroud)移动文件=复制+删除源文件
File from = new File(Environment.getExternalStorageDirectory().getAbsolutePath().getAbsolutePath()+"/kaic1/imagem.jpg");
File to = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/kaic2/imagem.jpg");
from.renameTo(to);
Run Code Online (Sandbox Code Playgroud)
在 Kotlin 中你可以使用 copyTo() 扩展函数,
sourceFile.copyTo(destFile, true)
Run Code Online (Sandbox Code Playgroud)
使用Square的Okio复制文件:
BufferedSink bufferedSink = Okio.buffer(Okio.sink(destinationFile));
bufferedSink.writeAll(Okio.source(sourceFile));
bufferedSink.close();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
152689 次 |
最近记录: |