java中特定字数限制后拆分文件
嘿,伙计们,我想在一定数量的单词之后使用java分割一个文件,并使用fix words limit将其分割为大量文件 到现在为止,我已经在数行上拆分了文件java中特定字数限制后拆分文件,java,arrays,file,Java,Arrays,File,嘿,伙计们,我想在一定数量的单词之后使用java分割一个文件,并使用fix words limit将其分割为大量文件 到现在为止,我已经在数行上拆分了文件 `package fileSplitting; import java.io.*; import java.util.Scanner; public class Split { public Split() { // TODO Auto-generated constructor stub }
`package fileSplitting;
import java.io.*;
import java.util.Scanner;
public class Split {
public Split() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
// Reading file and getting no. of files to be generated
String inputfile = "externalFiles/data.txt"; // Source File Name.
double nol = 20.0; // No. of lines to be split and saved in each output file.
File file = new File(inputfile);
Scanner scanner = new Scanner(file);
int count = 0;
while (scanner.hasNextLine())
{
scanner.nextLine();
count++;
}
System.out.println("Lines in the file: " + count); // Displays no. of lines in the input file.
double temp = (count/nol);
int temp1=(int)temp;
int nof=0;
if(temp1==temp)
{
nof=temp1;
}
else
{
nof=temp1+1;
}
System.out.println("No. of files to be generated :"+nof); // Displays no. of files to be generated.
//---------------------------------------------------------------------------------------------------------
// Actual splitting of file into smaller files
FileInputStream fstream = new FileInputStream(inputfile); DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine;
for (int j=1;j<=nof;j++)
{
System.out.println("No.of time I have entered :"+j);
FileWriter fstream1 = new FileWriter("splittedFiles/File"+j+".html"); // Destination File Location
BufferedWriter out = new BufferedWriter(fstream1);
for (int i=1;i<=nol;i++)
{
strLine = br.readLine();
if (strLine!= null)
{
out.write(strLine);
if(i!=nol)
{
out.newLine();
}
}
}
out.close();
}
in.close();
}catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
}
`
现在,我必须做同样的事情,但要在计算单词数之后。从文件中读取字符,并将它们拆分为空格字符作为分隔符,然后计算单词数,并根据单词cont写入文件 FileReader reader=新建FileReader新建FileFILE路径
char [] charBuff = new char [1024];
int eof = 0;
int count2 = 0;
int count1 = 0;
String [] words = new String [1024];
int wc = 0;
while ( (eof = reader.read(charBuff)) >0)
{
char c=' ';
char [] tmpChars = new char [256];
while (count1 < charBuff.length)
{
c= charBuff[count1];
if(Character.getType(c) == 15)
break;
if(c == ' ')
{
words [wc] = new String(tmpChars,0,count2);
//You can write the tmpChars to a file a file without converting it to a string
FileWriter fWriter = new FileWriter(new File("DIR:\\FILE NAME"+wc+".txt"));
fWriter.write(tmpChars);
fWriter.close();
wc++;
count1++;
count2 = 0;
tmpChars = new char [256];
continue;
}
tmpChars [count2] = c;
count2++;
count1++;
}
}
for(String i: words)
{
System.out.println("Word - "+i);
}
好吧,那你想做什么呢?有一些函数告诉您如何将字符串拆分为单词。你在哪里被困在实现这一点上?我的理解是不是错了,这堵代码墙与你的实际问题(如何标记字符串)关系不大这个问题可能在这个网站上被问了几百次?