大文件的拆分

面临的问题

当遇到大于2G的文件时,电脑自动的软件无法打开进行阅读,因此,我们需要将大文件分割成多个小文件进行存储;

解决方案

本文使用java来对文件进行分割,分割过程总共可以分为两步:

  1. 确认大文件中的行数:
    public class BeforeBreakFile {
        public static void main(String args[]) {
            long start = System.currentTimeMillis();
            try {
                FileReader read = new FileReader(
                	"D:\\dataset_TIST2015_Checkins.txt");
                BufferedReader br = new BufferedReader(read);
                String row;
    
                int rownum = 1;
                while ((row = br.readLine()) != null) {
                    rownum ++;
                }
                System.out.println("rownum="+rownum);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            long end = System.currentTimeMillis();
            long time = (end - start)/1000;
            System.out.println("时间:"+ time + "s");
        }
    }
    
  2. 读取大文件,并输出多个小文件
    public class InputDemo {
        public static void main(String[] args) throws IOException {
            int buffferSize = 20 * 1024* 1024; //设置读取文件缓存为20M
            //建立缓冲文本输入流
            //文件输入地址
            File file = new File("D:\\dataset_TIST2015_Checkins.txt"); 
            BufferedReader input = new BufferedReader(	
            	new FileReader(file),buffferSize);
    
            int splitNum = 5-1;//要分割的块数减一
            int fileLines = 6652727;//第一步中获得的文件行数!!
            long perSplitLines = fileLines / splitNum;//每个块的行数
            for (int i = 0; i <= splitNum; ++i)
            {
                //分割
                //每个块建立一个输出
                FileWriter output = new FileWriter(
                	"D:\\transfor" + i + "_TIST2015_Checkins.txt");
                String line = null;
                //逐行读取,逐行输出
                for (long lineCounter = 0; 
                	lineCounter < perSplitLines 
                	&& (line = input.readLine()) != null; 
                	++lineCounter)
                {//注意linux中换行是\n,widonws是\n\r
                    output.append(line + "\n\r");   
                }
                output.flush();
                output.close();
                output = null;
            }
        }
    }
    
在 Windows 系统下,拆分大文件可以通过多种方法实现,包括命令行工具、第三方软件以及编程语言实现的方式。以下是几种常见的拆分方法: ### 使用命令行工具 #### 使用 Git Bash 的 `split` 命令 如果你安装了 Git for Windows,那么 Git Bash 提供了 `split` 命令,可以方便地拆分大文件。例如,将一个大文件按 500MB 拆分: ```bash split myLargeFile.txt -b 500m ``` 也可以按行数进行拆分,例如每 10000 行生成一个文件: ```bash split myLargeFile.txt -l 10000 ``` 生成的文件名默认为 `xaa`、`xab`、`xac` 等形式,可以通过指定前缀和后缀来定制文件名。例如: ```bash split myLargeFile.txt -d -a 5 MySlice ``` 此命令将使用 `MySlice` 作为前缀,并以 5 位数字作为后缀 [^2]。 #### 使用 PowerShell PowerShell 也可以实现文件拆分功能,虽然其原生命令不支持直接拆分文件,但可以通过脚本实现。以下是一个简单的 PowerShell 脚本示例: ```powershell $filePath = "C:\path\to\your\file.txt" $outputPath = "C:\path\to\output\" $splitSize = 500MB # 每个拆分文件的大小 $fileStream = [System.IO.File]::OpenRead($filePath) $buffer = New-Object byte[] $splitSize $count = 0 while (($bytesRead = $fileStream.Read($buffer, 0, $splitSize)) -gt 0) { $outputFile = "$outputPath$filePrefix" + "{0:D5}" -f $count [System.IO.File]::WriteAllBytes($outputFile, $buffer[0..($bytesRead - 1)]) $count++ } $fileStream.Close() ``` ### 使用第三方软件 对于非技术用户,可以使用一些第三方软件来拆分大文件,例如: - **HJSplit**:一个免费的文件分割和合并工具,支持大文件拆分。 - **GSplit**:功能强大的文件分割工具,适合需要高级功能的用户。 - **PDF 编辑器**:对于 PDF 文件,可以使用 PDF 编辑器中的“拆分”功能选择特定页面进行拆分 [^1]。 ### 使用编程语言实现 如果需要更高的灵活性,可以使用编程语言(如 Python 或 C#)来实现文件拆分。 #### Python 示例 以下是一个简单的 Python 脚本,用于按指定大小拆分文件: ```python def split_file(file_path, chunk_size, output_path): with open(file_path, 'rb') as f: chunk = f.read(chunk_size) index = 0 while chunk: with open(f"{output_path}_part{index}", 'wb') as chunk_file: chunk_file.write(chunk) index += 1 chunk = f.read(chunk_size) # 拆分文件,每 500MB 生成一个文件 split_file("path/to/your/file.txt", 500 * 1024 * 1024, "path/to/output") ``` #### C# 示例 C# 中也可以通过流操作实现文件拆分: ```csharp using System; using System.IO; class Program { static void Main() { string filePath = @"C:\path\to\your\file.txt"; string outputPath = @"C:\path\to\output\"; int chunkSize = 500 * 1024 * 1024; // 500MB using (FileStream fs = new FileStream(filePath, FileMode.Open)) { byte[] buffer = new byte[chunkSize]; int bytesRead; int count = 0; while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0) { File.WriteAllBytes(outputPath + "part" + count++, buffer); } } } } ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值