hsm159
SimonHu-real
采纳率50%
2017-05-17 11:45 阅读 1.9k

JAVA中文件操作使用lastindexof获得后缀名的问题

2

代码如下,我输入E:\Application software\AUTO专杀.rar,追踪index函数返回的是6,也就是我的suffix是plication software\AUTO,试了很多别的文件路径,都找不到最后的那个".",请高手指教下为什么,是E:\的问题吗?

 package javaapplication1;

import java.io.*;
import java.text.DateFormat;
import java.util.Date;
import javafx.scene.shape.Path;

public class One {

    public static void main(String[] args) throws Exception {
        String path, Fsuffix;
        int index;
        Date d = new Date();
        String s;
        s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
        BufferedReader bin = new BufferedReader(new InputStreamReader(System.in, "gb2312"));
        path = bin.readLine();
        File f1 = new File(path);
        //System.out.println(path);
        //index = f1.getName().lastIndexOf(".");
        //Fsuffix = Path.
        File f2 = new File("d:\\" + s+"" +".rar");
        if (f2.exists()) {
            f2.delete();
        }
        f2.createNewFile();
        RandomAccessFile RR = new RandomAccessFile(f1, "r");
        RandomAccessFile RW = new RandomAccessFile(f2, "rw");
        TH_FILE_RW p1=new TH_FILE_RW(RR,RW,0,(long)(f1.length()/2));
        TH_FILE_RW p2=new TH_FILE_RW(RR,RW,1+(long)(f1.length()/2),f1.length());
        p1.start();
        p2.start();

    }
}
   class TH_FILE_RW extends Thread {
        RandomAccessFile RR;
        RandomAccessFile RW;
        long begin, end;

        public TH_FILE_RW(RandomAccessFile RR, RandomAccessFile RW, long begin, long end) {
            this.RR = RR;
            this.RW = RW;
            this.begin = begin;
            this.end = end;
        }
        long num = 0;
        byte bt[] = new byte[1024];

        public void run() {
            while (true) 
            {
                try{
                int count=RR.read(bt);
                num+=count;
                if(count<=0)
                {
                    break;
                }
                if(num>=(end-begin))
                {
                    count=count-(int)(num-(end-begin));
                    RW.write(bt, 0, count);
                    break;
                }
                RW.write(bt, 0, count);
                }catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
        }
    }

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • oyljerry oyljerry 2017-05-17 12:29

    从后往前查找.和\。得到之间的字符串

    点赞 评论 复制链接分享

相关推荐