重写文件拆分 l 完成文件合并

本文介绍了一个使用Java实现的简单文件拆分与合并程序。该程序能够将大文件拆分成指定大小的小文件,并能将这些小文件重新合并成原始文件。程序包括用户交互界面,用于指定文件路径、拆分大小及合并后的文件名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package Jobday13_作业;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        System.out.println("请输入要拆分的文件:");
        String s = new Scanner(System.in).nextLine();
        File file = new File(s);
        if (!file.isFile()) {
            System.out.println("输入的不是文件");
            return;
        }
        System.out.println("拆分文件大小(KB):");
        long size = 1024L * new Scanner(System.in).nextLong();
        try {
            chai(file, size);
            System.out.println("拆分成功");
        } catch (Exception e) {
            System.out.println("拆分失败");
            e.printStackTrace();
        }

        System.out.println("请输入要合并的目录:");
        String s1 = new Scanner(System.in).nextLine();
        File file1 = new File(s1);
        if (!file1.isDirectory()) {
            System.out.println("输入的不是目录");
            return;
        }
        System.out.println("请输入要核并到的文件名");
        String s3 = new Scanner(System.in).nextLine();
        File file2 = new File(s3);
//        if (!file2.isFile()) {
//            System.out.println("你输入保存合成的文件的文件名不是文件");
//            return;
//        }
        try {
            merger(file1, file2);
            System.out.println("合并成功");
        } catch (Exception e) {
            System.out.println("合并失败");
            e.printStackTrace();
        }
    }

    private static void merger(File file1, File file2) throws Exception {

        File[] files = file1.listFiles();
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                String s11 = o1.getName().substring(o1.getName().lastIndexOf('.') + 1, o1.getName().length());
                String s22 = o2.getName().substring(o2.getName().lastIndexOf('.') + 1, o2.getName().length());
                return Integer.parseInt(s11) - Integer.parseInt(s22);
            }
        });
        if (files == null) {
            return;
        }
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file2));
        for (File f : files) {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
                
            }
            in.close();
        }
out.close();
    }

    private static void chai(File file, long size) throws Exception {
        String name = file.getName();
        File dir = zhunBeiMulu(file);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream out = null;
        long byteCount = 0;
        int fileCount = 0;
        int b;
        while ((b = in.read()) != -1) {
            if (out == null || byteCount == size) {
                if (out != null) {
                    out.close();
                }
                out = new BufferedOutputStream(new FileOutputStream(new File(dir, name + "." + (++fileCount))));
                byteCount = 0;
            }
            out.write(b);
            byteCount++;
        }
        in.close();
        out.close();
    }

    private static File zhunBeiMulu(File file) {
        File dir = new File(file.getAbsolutePath() + "_spilt");
        if (!dir.exists()) {
            dir.mkdirs();
        } else {
            clear(dir);
        }
        return dir;
    }

    private static void clear(File dir) {
        File[] files = dir.listFiles();
        if (files == null) {
            return;
        }
        for (File f : files) {
            if (f.isFile()) {
                f.delete();
            } else {
                clear(f);// 清空f目录
                f.delete();// 删除目录

            }
        }

    }
}
 

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MaxSize 200 typedef struct student { char no[14]; // 学号 char name[21]; // 姓名 int cour[6]; // 课程成绩 int sum; // 总分 int schoolrank; // 校排名 int classrank; // 班排名 char classs[3]; // 班级 } stud; typedef struct { stud data[MaxSize]; int length; } SList; // 初始化顺序表 void InitList(SList *&L) { L = (SList *)malloc(sizeof(SList)); if (L == NULL) { printf("内存分配失败!\n"); exit(1); } L->length = 0; } // 计算字符串的显示宽度 int getDisplayWidth(char *str) { int width = 0; while (*str) { if ((unsigned char)*str > 127) { // 中文字符 width += 2; str += 2; } else { width += 1; str++; } } return width; } // 添加空格补齐到指定宽度 void padSpaces(char *buffer, int targetWidth) { int currentWidth = getDisplayWidth(buffer); int spacesNeeded = targetWidth - currentWidth; if (spacesNeeded > 0) { for (int i = 0; i < spacesNeeded; i++) { strcat(buffer, " "); } } } // 求总分 void CalSum(SList *&L) { for (int i = 0; i < L->length; i++) { int s = 0; for (int j = 0; j < 6; j++) s += L->data[i].cour[j]; L->data[i].sum = s; } } // 创建顺序表 void CreateList(SList *&L, char *na) { FILE *fp; if ((fp = fopen(na, "r")) == NULL) { printf("打开文件失败!文件不存在或不可读!\n"); exit(1); } char line[256]; fgets(line, sizeof(line), fp); // 跳过表头 int i = 0; while (i < MaxSize && fscanf(fp, "%s", L->data[i].name) == 1) { // 处理特殊姓名 if (strcmp(L->data[i].name, "努尔扎提·乃比2024035843049") == 0 || strcmp(L->data[i].name, "穆合塔尔·艾力2024035843050") == 0) { // 读取完整姓名和学号 char fullName[50]; strcpy(fullName, L->data[i].name); fscanf(fp, "%s", L->data[i].no); // 合并姓名 sprintf(L->data[i].name, "%s", fullName); } else { fscanf(fp, "%s", L->data[i].no); } // 提取班级信息 strncpy(L->data[i].classs, L->data[i].no + 8, 2); L->data[i].classs[2] = '\0'; // 读取成绩 for (int j = 0; j < 6; j++) { if (fscanf(fp, "%d", &L->data[i].cour[j]) != 1) { printf("读取成绩数据错误!\n"); fclose(fp); exit(1); } } // 跳过可能存在的错误数据 fscanf(fp, "%*d%*d"); i++; } L->length = i; fclose(fp); } // 按总分排序 void SortByScore(SList *&L) { for (int i = 0; i < L->length - 1; i++) { int k = i; for (int j = i + 1; j < L->length; j++) if (L->data[j].sum > L->data[k].sum) k = j; if (k != i) { stud temp = L->data[i]; L->data[i] = L->data[k]; L->data[k] = temp; } } } // 记录校排名 void Srank(SList *&L) { if (L->length == 0) return; L->data[0].schoolrank = 1; for (int i = 1; i < L->length; i++) { if (L->data[i].sum == L->data[i-1].sum) L->data[i].schoolrank = L->data[i-1].schoolrank; else L->data[i].schoolrank = i + 1; } } // 按班级和总分排序 void SortByClassAndScore(SList *&L) { for (int i = 0; i < L->length - 1; i++) { int k = i; for (int j = i + 1; j < L->length; j++) { int cmp = strcmp(L->data[j].classs, L->data[k].classs); if (cmp < 0 || (cmp == 0 && L->data[j].sum > L->data[k].sum)) { k = j; } } if (k != i) { stud temp = L->data[i]; L->data[i] = L->data[k]; L->data[k] = temp; } } } // 计算班级排名 void CRank(SList *&L) { if (L->length == 0) return; int currentRank = 1; char currentClass[3] = ""; strcpy(currentClass, L->data[0].classs); L->data[0].classrank = 1; for (int i = 1; i < L->length; i++) { if (strcmp(L->data[i].classs, currentClass) != 0) { // 新班级 strcpy(currentClass, L->data[i].classs); currentRank = 1; L->data[i].classrank = 1; } else { // 同班级 if (L->data[i].sum == L->data[i-1].sum) { L->data[i].classrank = L->data[i-1].classrank; } else { L->data[i].classrank = currentRank + 1; } } currentRank = L->data[i].classrank; } } // 输出到文件(严格对齐) void DispList(SList *&L, char *na) { FILE *fp; if ((fp = fopen(na, "w+")) == NULL) { printf("打开文件失败!打开的文件不存在!"); exit(1); } // 计算最大姓名宽度 int maxNameWidth = 0; for (int i = 0; i < L->length; i++) { int width = getDisplayWidth(L->data[i].name); if (width > maxNameWidth) maxNameWidth = width; } maxNameWidth += 2; // 添加额外间距 // 输出表头 fprintf(fp, "%-*s %-15s", maxNameWidth, "姓名", "学号"); fprintf(fp, "%6s%6s%6s%6s%6s%6s", "高数", "线代", "英语", "体育", "数据", "职规"); fprintf(fp, "%7s%9s%9s\n", "总分", "校排名", "班排名"); for (int i = 0; i < L->length; i++) { // 处理姓名对齐 char nameBuffer[50]; strcpy(nameBuffer, L->data[i].name); padSpaces(nameBuffer, maxNameWidth); // 处理学号对齐 char noBuffer[20]; strcpy(noBuffer, L->data[i].no); fprintf(fp, "%-*s %-15s", maxNameWidth, nameBuffer, noBuffer); // 输出成绩 - 右对齐 for (int j = 0; j < 6; j++) { fprintf(fp, "%6d", L->data[i].cour[j]); } // 输出总分和排名 - 右对齐 fprintf(fp, "%7d%9d%9d\n", L->data[i].sum, L->data[i].schoolrank, L->data[i].classrank); } fclose(fp); } // 释放内存 void DestroyList(SList *&L) { free(L); L = NULL; } int main() { SList *L1; InitList(L1); char filename1[30]; printf("请输入你要打开的文件名:"); scanf("%s", filename1); CreateList(L1, filename1); CalSum(L1); // 先按总分排序计算校排名 SortByScore(L1); Srank(L1); // 再按班级和总分排序计算班排名 SortByClassAndScore(L1); CRank(L1); char filename2[30]; printf("请输入你要写入的文件名:"); scanf("%s", filename2); DispList(L1, filename2); DestroyList(L1); return 0; } 学生名字无法严格左对齐喔
最新发布
06-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值