- 浏览: 69857 次
- 性别:
- 来自: 西安
最新评论
-
610622106:
Iterator接口才有这三方法hasNext(),next( ...
java.lang.Iterable接口 - 循环打印MAP容器时候所想到的
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Scanner; public class CutFile { public static void cut(File file,int fileSize){ fileSize = fileSize * 1024; int count = (int)file.length()/fileSize; InputStream in = null; try { in = new FileInputStream(file); byte[] buffer = new byte[(int)file.length()]; in.read(buffer); for(int i = 0 ; i <count;i++){ String ext = getFileExt(file); String path = file.getPath()+""+i+ext; File file1 = new File(path); OutputStream out = new FileOutputStream(file1); int length = (int)(((i+1)*fileSize>file.length())?(file.length()-i*fileSize) :fileSize); int offset = i * fileSize; out.write(buffer,offset,length); out.close(); } System.out.println("切割完成!共生成"+count+"个文件"); } catch (IOException e) { e.printStackTrace(); }finally{ try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //获取文件后缀名 public static String getFileExt(File file){ String ext = ""; String fileName = file.getName(); if(fileName.contains(".")){ ext = fileName.substring(fileName.lastIndexOf("."), fileName.length()); } return ext; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //得到文件地址 String filePath = ""; File file = null; while(true){ System.out.println("请输入文件地址:"); filePath = scanner.next(); if(null!=filePath&&!"".equals(filePath)){ file = new File(filePath); if(!file.isFile()){ System.out.println("文件地址不正确!"); continue; } if(file.length()<=0){ System.out.println("文件太小了!"); continue; } break; } } //文件切割大小 int fileSize = 0; while(true){ System.out.println(file.getName()+"的大小是:"+file.length()/1024+"KB"); System.out.println("请输入切割大小(KB):"); fileSize = scanner.nextInt(); if(fileSize>0){ break; } } cut(file,fileSize); } }
用手机看小说,按的手疼,就做了这个,还是不太完善。
这个小程序切其他文件,打开后不正确,还得做个合并工具。
修正小数点问题
package com.wangning.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.NumberFormat; import java.util.Scanner; public class CutFile { public static void cut(File file,double fileSize){ fileSize = fileSize * 1024; double a = file.length()/fileSize; int count = (int)(file.length()/fileSize); if(a > (int) a ){ count +=1; } InputStream in = null; try { in = new FileInputStream(file); byte[] buffer = new byte[(int)file.length()]; in.read(buffer); for(int i = 0 ; i <count;i++){ String ext = getFileExt(file); String path = file.getPath()+""+i+ext; File file1 = new File(path); OutputStream out = new FileOutputStream(file1); int length = (int)(((i+1)*fileSize>file.length())?(file.length()-i*fileSize) :fileSize); int offset = i * (int) fileSize; out.write(buffer,offset,length); out.close(); } System.out.println("切割完成!共生成"+count+"个文件"); } catch (IOException e) { e.printStackTrace(); }finally{ try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //获取文件后缀名 public static String getFileExt(File file){ String ext = ""; String fileName = file.getName(); if(fileName.contains(".")){ ext = fileName.substring(fileName.lastIndexOf("."), fileName.length()); } return ext; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //得到文件地址 String filePath = ""; File file = null; while(true){ System.out.println("请输入文件地址:"); filePath = scanner.next(); if(null!=filePath&&!"".equals(filePath)){ file = new File(filePath); if(!file.isFile()){ System.out.println("文件地址不正确!"); continue; } if(file.length()<=0){ System.out.println("文件太小了!"); continue; } break; } } //文件切割大小 int fileSize = 0; while(true){ System.out.println(file.getName()+"的大小是:"+file.length()/1024+"KB"); System.out.println("请输入切割大小(KB)/每1000个汉字:"); fileSize = scanner.nextInt(); if(fileSize>0){ break; } } cut(file,fileSize*2); } }
发表评论
-
上传-代码
2012-08-24 15:05 864刚写的上传处理方式,想从代码,效率上分析代码的质量。 参考资 ... -
PD使用SQL生成PDM
2011-07-05 20:20 2079点击PD中的文件-Reverse Engineer-Datab ... -
HTML应用
2011-05-30 10:32 696mailto:点击调用本机的EMAIL程序 -
对Apache POI的使用
2011-05-23 14:37 1502public static void main(String[ ... -
oracleimp/exp命令使用
2011-04-14 15:40 937转自:http://www.examda.com/oracle ... -
Jquery js 学习
2010-12-06 11:54 0在Jquery库里,$就是jQuery的一个简写形式,如 ... -
配置SVN服务器
2010-07-30 00:25 1361原文来自:http://liushengjiao.boke ... -
ServU 自动离线问题
2010-07-22 09:47 1415最近项目里用到FTP服务器,部署的时候FTP用的是SERVU, ... -
Flex应用一:将Flex开发环境集成到MyEclipse
2010-01-07 14:04 2646刚接触RIA,看朋友用myeclipse整合FLEX叫个费劲, ... -
简单的网页计算器(JS、HTML)
2009-12-22 00:23 2380最近才学JS,做了个简单的网页计算器,除了js部分属性的使用, ... -
当被问到“为什么接口中的变量都用final修饰”时
2009-10-11 02:04 5658今天碰到这个问题时候,还真不好理解,只知道interface中 ...
相关推荐
手机电子书必不可少的工具,可以对文本文件进行方便切割.使用非常方便