`
qhuwn
  • 浏览: 69857 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

txt小说切割器

阅读更多
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);   
    }   
}  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics