java对文件的处理--分割文件

获取文件夹下的文件名

/**
	 * @Author:
	 * @Description:获取某个目录下所有直接下级文件,不包括目录下的子目录的下的文件,所以不用递归获取
	 * @Date:
	 */
	public static List<String> getFiles(String path) {
		List<String> files = new ArrayList<String>();
		File file = new File(path);
		File[] tempList = file.listFiles();

		for (int i = 0; i < tempList.length; i++) {
			if (tempList[i].isFile()) {
				//files.add(tempList[i].toString());
				//文件名,不包含路径
				String fileName = tempList[i].getName();
				files.add(fileName);
			}
			if (tempList[i].isDirectory()) {
				//这里就不递归了,
			}
		}
		return files;
	}

获取文件的长度

/**
	 * 获取文件长度
	 */
	@Override
	public long getFileLength(File file) {
		FileReader fr =  null;
		BufferedReader br = null;
		//文件大小
		long fileSize = 0;
		try {
			fr = new FileReader(file);
			br = new BufferedReader(fr);
			String line = br.readLine();
			//按行读取文件
			while(line != null){
				//计算文件大小
				fileSize += line.length();
				line = br.readLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			//关闭输入流
			try {
				if(br != null){
					br.close();
				}
				if(fr != null){
					fr.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//返回文件大小
		return fileSize;
	}

获取可以分割的文件数量

	/**
	 * 获取可以分割的文件数
	 */
	@Override
	public int getSplitFileNum(long fileByte,String fileParh, long size){
		fileByte = getFileLength(new File(fileParh));
		if(size < fileByte){
			if(fileByte % size == 0){
				return (int) (fileByte/size);
			}else{
				return (int) (fileByte/size) + 1;
			}
		}
		return 1;
	}

分割文件

 /**
     * 分割文件
     */
    @Override
    //public String[] splitFile(File srcFile, int splitFileNum) throws IOException {
    public String[] splitFile(File srcFile, int splitFileNum, long size) throws IOException {
        splitFileNum = getSplitFileNum(getFileLength(srcFile), srcFile.toString(), size);
        if (splitFileNum <= 0) {
            return null;
        }
        FileReader fr = null;
        BufferedReader br = null;
        long readNum = 0;
        String[] splits = new String[splitFileNum];
        try {
            fr = new FileReader(srcFile);
            br = new BufferedReader(fr);
            int i = 0;
            while (splitFileNum > i) {
                //分割后的文件名
                String name = null;
                //文件后缀
                String nameLast = null;
                if (srcFile.getName().indexOf(".") != -1) {
                    name = srcFile.getName().substring(0, srcFile.getName().indexOf("."));
                    int last = srcFile.getName().lastIndexOf(".");
//					System.out.println(i);
//					String string = str.substring(i);
                    nameLast = srcFile.getName().substring(last);

                } else {
                    name = srcFile.getName();
                }
                splits[i] = srcFile.getParent() + "/" + name + "_" + i + nameLast;
                File wfile = new File(splits[i]);
                if (!wfile.exists()) {
                    wfile.getParentFile().mkdirs();
                    wfile.createNewFile();
                }
                FileWriter fw = new FileWriter(wfile, false);
                BufferedWriter bw = new BufferedWriter(fw);
                String line = br.readLine();
                int flush = 0;
                while (line != null) {
                    if (line.trim().length() == 0) {
                        line = br.readLine();
                        continue;
                    }
                    readNum += line.length();
                    if (i + 1 == splitFileNum) {
                        bw.write(line);
                        bw.newLine();
                    } else {
                        if (readNum >= size) {
                            bw.write(line);
                            bw.newLine();
                            break;
                        } else {
                            bw.write(line);
                            bw.newLine();
                        }
                    }
                    line = br.readLine();
                    if (flush % 100 == 0) {
                        bw.flush();
                    }
                }
                bw.flush();
                fw.flush();
                bw.close();
                fw.close();
                readNum = 0;
                i++;
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) br.close();
                if (fr != null) fr.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                br = null;
                fr = null;
            }
        }
        return splits;
    }

提取文件名

	/**
	 * 获取文件名 (去除后缀)
	 * @param filename
	 * @return
	 */
	public static String getFileNameNoEx(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int dot = filename.lastIndexOf('.');
			if ((dot >-1) && (dot < (filename.length()))) {
				return filename.substring(0, dot);
			}
		}
		return filename;
	}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值