「文件分割java」文件分割符

博主:adminadmin 2022-12-29 09:48:08 59

今天给各位分享文件分割java的知识,其中也会对文件分割符进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA怎么实现按行数分割文件

import java.io.*;

public class SegFile{

/**

*根据需求,直接调用静态方法start来执行操作

*参数:

* rows 为多少行一个文件 int 类型

* sourceFilePath 为源文件路径 String 类型

* targetDirectoryPath 为文件分割后存放的目标目录 String 类型

* ---分割后的文件名为索引号(从0开始)加'_'加源文件名,例如源文件名为test.txt,则分割后文件名为0_test.txt,以此类推

*/

public static void start(int rows,String sourceFilePath,String targetDirectoryPath){

File sourceFile = new File(sourceFilePath);

File targetFile = new File(targetDirectoryPath);

if(!sourceFile.exists()||rows=0||sourceFile.isDirectory()){

System.out.println("源文件不存在或者输入了错误的行数");

return;

}

if(targetFile.exists()){

if(!targetFile.isDirectory()){

System.out.println("目标文件夹错误,不是一个文件夹");

return;

}

}else{

targetFile.mkdirs();

}

try{

BufferedReader br = new BufferedReader(new FileReader(sourceFile));

BufferedWriter bw = null;

String str = "";

String tempData = br.readLine();

int i=1,s=0;

while(tempData!=null){

str += tempData+"\r\n";

if(i%rows==0){

bw = new BufferedWriter(new FileWriter(new File(targetFile.getAbsolutePath()+"/"+s+"_"+sourceFile.getName())));

bw.write(str);

bw.close();

str = "";

s += 1;

}

i++;

tempData = br.readLine();

}

if((i-1)%rows!=0){

bw = new BufferedWriter(new FileWriter(new File(targetFile.getAbsolutePath()+"/"+s+"_"+sourceFile.getName())));

bw.write(str);

bw.close();

br.close();

s += 1;

}

System.out.println("文件分割结束,共分割成了"+s+"个文件");

}catch(Exception e){}

}

//测试

public static void main(String args[]){

SegFile.start(11,"d:/test/test.txt","d:/test/test/");

}

}

/*

把代码改了下,先前的代码在行数刚好分完的情况下会多分一个空白文件,现在不存在这个问题了

*/

如何用Java分割大txt文件

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class FileCutter { /** * *sourceFile:源文件的路径 *targetDirectory:保存文件的目录(例:'C:\\') *prefix:是分割后文件的前缀(例:'2015-09-09') *size:是分隔后单一文件的大小单位是2kb的倍数,size传10,分割后单一文件就是20K。传100,文件就是2M一个。 * **/ public static void cutToMoreFile(String sourceFile, String targetDirectory, String prefix, int size) { //加载源文件 File source = new File(sourceFile); InputStream in = null; OutputStream out = null; int len = 0; int fileIndex = 1; //设置一次加载的大小 byte[] buffer = new byte[2048]; try { //把源文件读到InputStream中 in = new FileInputStream(source); //循环 while(true) { //分割后的文件流 out = new FileOutputStream(targetDirectory + File.separator + prefix + fileIndex++ + ".txt"); for(int i = 0; i size; i++) { //如果文件读取完就退回方法。 if((len = in.read(buffer)) != -1) { //写入分割后的文件 out.write(buffer, 0, len); }else { //执行finally内容后,退出方法 return; } } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { //关系流 in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

java文本分割并存入数组

以下代码仅供参考

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {

Ant[] ants;

public Main() {

ants = new Ant[100];

}

public static void main(String[] args) {

File f = new File("D:\\123.txt");

Main main = new Main();

main.decodeFile(f);

for (int i = 0, k = main.ants.length; i  k; i++) {

System.out.println(main.ants[i]);

}

}

public boolean decodeFile(File f) {

BufferedReader br;

try {

br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));

String lineString = "";

int txtNameIndex;

Ant ant;

String[] antString;

String txtName;

String[] childString;

int index = 0;

while ((lineString = br.readLine()) != null  index  100) {

antString = lineString.split("#");

ant = new Ant();

txtNameIndex = antString[0].indexOf(" ");

txtName = antString[0].substring(0, txtNameIndex);

antString[0] = antString[0].substring(txtNameIndex + 1);

for (int i = 0, k = antString.length; i  k; i++) {

ant = new Ant();

childString = antString[i].split("/");

ant.txtID = index + 1;

ant.txtName = txtName;

ant.keyword = childString[0];

ant.style = childString[1];

ant.wWeight = Double.parseDouble(childString[2]);

ant.wCount = Integer.parseInt(childString[3]);

ants[index] = ant;

index++;

}

}

br.close();

} catch (IOException e) {

e.printStackTrace();

return false;

}

return true;

}

}

class Ant {

int txtID;

String txtName;

String keyword;

String style;

double wWeight;

int wCount;

@Override

public String toString() {

return "[txtID:" + txtID + ",txtName:" + txtName + ",keyword:" + keyword + ",style:" + style + ",wWeight:"

+ wWeight + ",wCount:" + wCount + "]";

}

}

用java怎么分割dat文件

用java怎么分割dat文件

//读取时, 只要 readFile("C:\\test.dat");

public String readFile(String path) throws IOException...{

File file=new File(path);

if(!file.exists()||file.isDirectory())

throw new FileNotFoundException();

BufferedReader br=new BufferedReader(new FileReader(file));

String temp=null;

StringBuffer sb=new StringBuffer();

temp=br.readLine();

while(temp!=null)...{

sb.append(temp+" ");

temp=br.readLine();

}

return sb.toString();

}

如何用java把任意大小的文件分割为10份,然后再合并

java.io.File 得到文件长度,分成10份的字节数(不完全是平均的)。。。。。使用FileInputStream读入、FileOutputStream写到10外文件 。。。。。。。。。。。。

合并 时,文件流 反操作 。。。。。。

java下文本文件读取并分割

package demo;

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class DemoApp {

public static void main(String[] args) throws Exception {

// 指定的文件

File file = new File("E:\\Workspaces\\eclipse3.7\\Demo\\src\\test.txt");

// 装载list

ListString list = new ArrayListString();

// 读取文件

FileInputStream fis = new FileInputStream(file);

BufferedReader br = new BufferedReader(new InputStreamReader(fis));

StringBuffer buffer = new StringBuffer();

String line;

int i = 0;

while ((line = br.readLine()) != null) {

System.out.println(line);

if (i == 0) {

buffer.append(line + "\n");

} else {

// 判断截取点

if (line.substring(0, 1).equals("")) {

list.add(buffer.toString());

buffer = new StringBuffer();

buffer.append(line + "\n");

} else {

buffer.append(line + "\n");

}

}

i++;

}

if (line == null) {

list.add(buffer.toString());

}

// test

System.out.println("--------------------------");

for(int j=0; jlist.size(); j++) {

System.out.println( j + ":   " + list.get(j));

System.out.println("-----------------------");

}

}

}

文件分割java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于文件分割符、文件分割java的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-29,除非注明,否则均为首码项目网原创文章,转载请注明出处。