日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文件的拆分

發布時間:2025/3/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文件的拆分 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在文件傳輸過程中,經常需要將一個文件拆分成多個較小的文件,然后利用多線程傳輸這些小文件,最后再對這些小文件進行合并。這里先給出文件拆分的一個demo,稍后將會給出文件合并的介紹。

/*
?* To change this template, choose Tools | Templates
?* and open the template in the editor.
?*/
package hfut.wst.io;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
?*
?* @author 風停心止
?*/
public class RandomAccessFileDemo {

??? public static void main(String[] args) {
??????? try {
??????????? RandomAccessFile file = new RandomAccessFile("randomFile1.txt", "rws");//創建一個隨即讀寫文件
??????????? String[] names = {"zhangsan is me\r\n", "lisi is my deskmate\r\n", "wangwu is my best friend\r\n"};//待寫入內容
??????????? String[] chineseName = {"張三\r\n", "李四r\n", "王五\r\n"};//待寫入內容
??????????? for (String name : names) {//寫入英文內容
??????????????? file.seek(file.length());//將文件指針定位在文件結尾,實現追加功能
??????????????? byte[] nameByte = name.getBytes();
??????????????? file.write(nameByte);//寫入內容
??????????? }
??????????? for (String name : chineseName) {//寫入中文內容
??????????????? file.seek(file.length());
??????????????? byte[] nameByte = name.getBytes();
??????????????? file.write(nameByte);
??????????? }
??????????? System.out.println(file.length());
??????????? file.seek(0);//將文件指針置為文件開始
??????????? String test = "";
??????????? while ((test = file.readLine()) != null) {
??????????????? String line = new String(test.getBytes("ISO-8859-1"));//讀取文件內容并正確處理亂碼和編碼問題
??????????????? System.out.println(line);
??????????? }
//??????????? int seekIndex = 0;
//??????????? byte[] b = new byte[256];
//??????????? while (seekIndex <= file.length()) {
//??????????????? file.seek(seekIndex);
//??????????????? file.read(b);
//??????????????? System.out.println(b);
//??????????????? seekIndex += b.length;
//??????????? }
??????????? int blockSize = 256;//每個文件塊的大小
??????????? int blockNum = (int) file.length() / blockSize + 1;//計算文件可以拆分的數目
??????????? File[] blocks = new File[blockNum];//將拆分小文件放在某一數組中
??????????? byte[] block = new byte[blockSize];//定義內容數組
??????????? file.seek(0);//從文件頭開始拆分
??????????? int fileIndex = 1;
??????????? int index = 0;
??????????? while (file.getFilePointer() < file.length()) {
??????????????? String path = "block" + fileIndex + ".txt";
??????????????? File temp = new File(path);//創建小文件
??????????????? file.read(block);//讀取blockSize字節內容到block中
??????????????? DataOutputStream dos = new DataOutputStream(new FileOutputStream(temp));//創建小文件輸出流
??????????????? dos.write(block);//向小文件中寫入拆分內容
??????????????? blocks[index] = temp;//繼續進行拆分
??????????????? fileIndex++;
??????????????? index++;
??????????????? //System.out.println(path + " is created");
??????????????? dos.close();//關閉輸入流,否則下面將無法刪除小文件
??????????? }
??????????? System.out.println(block.length);
??????????? for (File f : blocks) {//刪除小文件
??????????????? if (f.exists()) {
??????????????????? System.out.println(f.getAbsoluteFile());//顯示拆分小文件路徑
??????????????????? if (!f.delete()) {
??????????????????????? System.out.println("請關閉使用該文件的所有進程或者流!!");
??????????????????? } else {
??????????????????????? System.out.println(f.getName()+" 成功被刪除!");
??????????????????? }
??????????????? }
??????????? }
??????????? file.close();
??????? } catch (IOException e) {
??????????? e.printStackTrace();
??????? }
??? }
}
?

轉載于:https://blog.51cto.com/wwssttt/410312

總結

以上是生活随笔為你收集整理的文件的拆分的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。