文件的拆分
在文件傳輸過程中,經常需要將一個文件拆分成多個較小的文件,然后利用多線程傳輸這些小文件,最后再對這些小文件進行合并。這里先給出文件拆分的一個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
總結
- 上一篇: 机械迷城攻略2
- 下一篇: (第一篇)FFilmation Glos