/**
* 文件分隔
*/
package com.basic.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author Administrator
*
*/
public class FileSeparator {
/**
* 按字节分隔
* @param sourceFileUrl 源文件路径如:c:/a.txt
* @param targetFileUrl 目标文件路径文件夹(最好在盘符下,我没有创建文件夹): c:/
*/
public void separateFileByByte(String sourceFileUrl, String targetFileUrl) {
File sourceFile = new File(sourceFileUrl);
String sourceFileName = sourceFile.getName().split(".txt")[0]; //获得源文件名称
try {
FileInputStream fis = new FileInputStream(sourceFile); //源文件文件流
int temp;
FileOutputStream fos = null; //目标文件文件流
int size = 0; //记录文件大小
int countFile = 1; //子文件数目
File targetFile = null;
do {
if (size % 100 == 0) {
String targetFileName = targetFileUrl + sourceFileName + countFile + ".txt"; //目标文件名称,以源文件名称加数据命名
targetFile = new File(targetFileName);
if (fos != null) {
fos.flush(); //刷新该流的缓冲
fos.close(); //关闭流
}
fos = new FileOutputStream(targetFile);
countFile++;
}
temp = fis.read(); //读文件
size++;
fos.write(temp); //写文件
} while (fis.available()>0); //判断源文件是否还有流,即有没有读完
fis.close();
fos.flush();
fos.close();
System.out.println("File separate End!" );
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 按字符分隔
* @param sourceFileUrl
* @param targetFileUrl
*/
public void separateFileByChar(String sourceFileUrl, String targetFileUrl) {
File sourceFile = new File(sourceFileUrl);
String sourceFileName = sourceFile.getName().split(".txt")[0]; //获得源文件名称
try {
FileReader fr = new FileReader(sourceFile); //源文件文件流
System.out.println("encoding:" + fr.getEncoding());
int temp;
FileWriter fw = null; //目标文件文件流
int size = 0; //记录文件大小
int countFile = 1; //子文件数目
File targetFile = null;
do {
if (size % 100 == 0) {
String targetFileName = targetFileUrl + sourceFileName + countFile + ".txt"; //目标文件名称,以源文件名称加数据命名
targetFile = new File(targetFileName);
if (fw != null) {
fw.flush(); //刷新该流的缓冲
fw.close();//关闭流
}
fw = new FileWriter(targetFile);
countFile++;
}
temp = fr.read();//读文件
size++;
fw.write(temp);//写文件
} while (temp != -1);//判断源文件是否还有流,即有没有读完
fr.close();
fw.flush();
fw.close();
System.out.println("File separate End!" );
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String sourceFileUrl = null;
String targetFileUrl = null;
//输入源文件路径
System.out.println("Source File Url: \t");
try {
sourceFileUrl = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//输入目标文件路径
System.out.println("\nTarget File Url: \t");
try {
targetFileUrl = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Source File: " + sourceFileUrl + "\nTarget File: " + targetFileUrl);
FileSeparator fs = new FileSeparator();
fs.separateFileByByte(sourceFileUrl, targetFileUrl);
}
}
程序二:
/**
* 用缓冲流实现文件分隔
*/
package com.basic.io;
import java.io.*;
public class TestBufferStream {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("c:/Java编码规范.txt"));
BufferedWriter bw = null;
String s = null; //单行内容
int countLine = 0; //总行数
int countFile = 1; //子文件数
while((s=br.readLine())!=null){
if (countLine % 50 == 0) {
if (bw != null) {
bw.flush();
bw.close();
}
bw = new BufferedWriter(new FileWriter("c:/Java编码规范" + countFile + ".txt"));
countFile++;
}
bw.write(s);
bw.newLine();
System.out.println(s);
countLine++;
}
System.out.println("countLine:" + countLine);
bw.close();
if(countLine != 0){
bw.flush();
bw.close();
}
} catch (IOException e) { e.printStackTrace();}
}
}
程序三:
/**
* 用字符流实现文件分隔
*/
package com.basic.io;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author Administrator
*
*/
public class TestFileReader {
public static final int charSize = 1024;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileReader fr = new FileReader("c:/Java编码规范.txt");
FileWriter fw = null;
char[] bfc = new char[charSize];
int temp; //单个字符
int countWord = 0; //字符数
int countFile = 1; //子文件数
while((temp=fr.read())!=-1){
if (countWord % charSize == 0) {
fw = new FileWriter("c:/Java编码规范" + countFile + ".txt");
countFile++;
}
bfc[countWord] = (char)temp;
if (countWord == charSize -1) {
fw.write(bfc);
fw.flush();
fw.close();
countWord = 0;
} else {
countWord++;
}
}
fr.close();
if (countWord > 0) {
fw.write(bfc);
fw.flush();
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
实现文件分隔功能
最新推荐文章于 2023-09-08 09:44:49 发布