去年4月份的时候,我有一个任务,让我写个下载保存文件的方法,如果文件的名字存在,就加“-1”,如果仍然存在,就在-后面的数字加1,例如:文件名:Keep_On_It.mp3,第一次下载是Keep_On_It.mp3,第二次下载名字就保存成:Keep_On_It-1.mp3,第三次下载名字就保存成:Keep_On_It-2.mp3,第四次下载名字就保存为:Keep_On_It-3.mp3,以此类推的形式一直实现。当时我用的方法比较笨,可读性特别差,当时用了半天就实现了,就是截取字符串进行比较。最近研究源码下载的时候发现可以用for循环来实现,可读性强,容易理解,算法简练,不愧为谷歌设计出来的,简单明了,容易理解。
转载请标明出处:http://blog.csdn.net/wdaming1986/article/details/7342559
现在给大家分享一下:先看效果图片:
如果内存卡的这个目录只有这些MP3:
再次保存的名字就是最后一个Keep_It_On-3.mp3了
下面把代码奉上:
在CopyFileApp的工程里:
一、在com.cn.daming.copyfile包下的CopyFileAppActivity.java类中的代码:
- package com.cn.daming.copyfile;
- import java.io.File;
- import java.util.Random;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.SystemClock;
- import android.util.Log;
- import android.widget.TextView;
- public class CopyFileAppActivity extends Activity {
- /** Called when the activity is first created. */
- private final static String SDCARD_PATH="/mnt/sdcard/download/mp3/Keep_It_On.mp3";
- private final static String FILENAME_SEQUENCE_SEPARATOR = "-";
- private static Random sRandom = new Random(SystemClock.uptimeMillis());
- private final static String LOG = "wangxianming";
- private TextView m_TextView;
- private TextView m_TextView_full;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- m_TextView = (TextView)findViewById(R.id.text_view);
- m_TextView_full = (TextView)findViewById(R.id.text_view2);
- Log.v(LOG, " ---->"+SystemClock.uptimeMillis());
- // Check to see if we have an SDCard
- String status = Environment.getExternalStorageState();
- if (!status.equals(Environment.MEDIA_MOUNTED)) {
- int title;
- String msg;
- // Check to see if the SDCard is busy, same as the music app
- if (status.equals(Environment.MEDIA_SHARED)) {
- msg = getString(R.string.download_sdcard_busy_dlg_msg);
- title = R.string.download_sdcard_busy_dlg_title;
- } else {
- msg = getString(R.string.download_no_sdcard_dlg_msg, "no sdcard");
- title = R.string.download_no_sdcard_dlg_title;
- }
- new AlertDialog.Builder(this)
- .setTitle(title)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setMessage(msg)
- .setPositiveButton(R.string.ok, null)
- .show();
- return;
- }
- File mp3File = new File(SDCARD_PATH);
- if(mp3File != null && mp3File.exists()) {
- String fileNamePath = mp3File.getAbsolutePath();
- String filename = fileNamePath.substring(0, fileNamePath.lastIndexOf("."));
- String extension = fileNamePath.substring(fileNamePath.lastIndexOf("."), fileNamePath.length());
- m_TextView.setText(filename + extension);
- m_TextView_full.setText(chooseUniqueFilename(filename, extension));
- }
- }
- //This is method is core
- private String chooseUniqueFilename(String filename, String extension) {
- String fullFilename = filename + extension;
- if(!new File(fullFilename).exists()) {
- return fullFilename;
- }
- filename = filename + FILENAME_SEQUENCE_SEPARATOR;
- /*
- * This number is used to generate partially randomized filenames to avoid
- * collisions.
- * It starts at 1.
- * The next 9 iterations increment it by 1 at a time (up to 10).
- * The next 9 iterations increment it by 1 to 10 (random) at a time.
- * The next 9 iterations increment it by 1 to 100 (random) at a time.
- * ... Up to the point where it increases by 100000000 at a time.
- * (the maximum value that can be reached is 1000000000)
- * As soon as a number is reached that generates a filename that doesn't exist,
- * that filename is used.
- * If the filename coming in is [base].[ext], the generated filenames are
- * [base]-[sequence].[ext].
- */
- int sequence = 1;
- for (int magnitude = 1; magnitude < 1000000000; magnitude *= 10) {
- for (int iteration = 0; iteration < 9; ++iteration) {
- fullFilename = filename + sequence + extension;
- if (!new File(fullFilename).exists()) {
- return fullFilename;
- }
- Log.v(LOG, "file with sequence number " + sequence + " exists");
- sequence += sRandom.nextInt(magnitude) + 1;
- }
- }
- return fullFilename;
- }
- }
二、layout目录下的main.xml中的代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dip"
- android:gravity="center"
- android:text="@string/hello" />
- <TextView
- android:id="@+id/text_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dip"
- android:text=""
- />
- <TextView
- android:id="@+id/text_view2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dip"
- android:text="" />
- </LinearLayout>
三、values目录下的strings.xml中的代码:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">This is daming Original</string>
- <string name="app_name">CopyFileApp</string>
- <string name="download_sdcard_busy_dlg_title" product="default" msgid="6877712666046917741">"SD 卡不可用"</string>
- <string name="download_sdcard_busy_dlg_msg">"SD 卡正忙。要允许下载,请在通知中选择“关闭 USB 存储设备”。"</string>
- <string name="download_no_sdcard_dlg_msg" product="default" msgid="2616399456116301518">"需要有 SD 卡才能下载</string>
- <string name="download_no_sdcard_dlg_title" product="default" msgid="605904452159416792">"无 SD 卡"</string>
- <string name="cancel" msgid="3017274947407233702">"取消"</string>
- <string name="ok" msgid="1509280796718850364">"确定"</string>
- </resources>