JAVA每日一题18

    题目:在很多操作系统中,文件名长度都有限制,所以在文件名后面添加_old有时可能会失败。修改避免覆盖文件的程序实例,将一个三位数添加到文件名的末尾,以此来和已存在的文件向区别,而不仅仅是添加_old。程序应该检查存在文件的文件名后面的三位数,并用一定数量的增加值将其替换,以使其具有唯一性。(也就是说,将最后三位数增加1,直到产生了具有唯一性的文件名为止。)

 

import java.io.File;
import java.io.FileOutputStream;
 

import java.io.FileNotFoundException;

public class AvoidOverwritingFile {
  public static void main(String[] args) {
    String filepath = "C:/Beg Java Stuff/myFile.txt";

    if(args.length>0) {                            
      filepath = args[0];  
      }                      
      
    File aFile = new File(filepath);
    FileOutputStream outputFile = null;          // Stores the stream reference
    if (aFile.isFile()) {
      File newFile = aFile;                      // Start with the original file

      // We will append three digits to the file to make it unique
      do {
        String name = newFile.getName();         // Get the name of the file
        int period =  name.indexOf('.');         // Find the separator for the extension
        if(period == -1) {                         
          newFile = new File(newFile.getParent(), extendName(name));       // ***
        } else {
          newFile = new File(newFile.getParent(), 
                           extendName(name.substring(0, period)) 
                           + name.substring(period));
        }
      } while(!aFile.renameTo(newFile));         // Stop when renaming works
    } 

    // Now we can create the new file
    try {
      // Create the stream opened to append data
      outputFile = new FileOutputStream(aFile);
      System.out.println(aFile.getName()+" output stream created");
    } catch (FileNotFoundException e) {
      e.printStackTrace(System.err);
    } 
    System.exit(0);
  }
  // *** new method to adding or increment three digits at the end of a name starting at "000"
  private static String extendName(String name) {
    StringBuffer newName = new StringBuffer(name);              // We will create the new name in this buffer
    String digits = newName.substring(newName.length()-3,newName.length());   // Get last three characters
    int number = 0;
    try {
      number = Integer.parseInt(digits);                                      // Parse the last 3 characters
      ++number;                                                               // We got an integer so increment it
      newName.delete(newName.length()-3,newName.length());
    } catch(NumberFormatException nfe) {                                      // Last 3 characters not an integer
      // This will be thrown if digits is not a parsable integer
      // but if we just do nothing here execution will continue
      // with number as its initial value, which is zero.
    }
    digits = String.valueOf(number);                // Get number as a string 
    assert digits.length() < 4;                     // Make sure we don't run over three digits!     
   
    // We can now create the new name by appending "000" and replacing some or all of these
    // by the digits in the String, digits.   
    return newName.append("000").replace(newName.length()-digits.length(),newName.length(), digits).toString();    
  }
}

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值