【发布时间】:2014-08-27 12:04:14
【问题描述】:
大家好,我想在一定数量的单词后使用 java 拆分文件,并将其分成大量具有固定单词限制的文件。
到目前为止,我已经在计数行上拆分了文件。
`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());
}
}
}
`
现在我必须做同样的事情,但在数单词之后。
【问题讨论】:
-
好的,那你有什么尝试呢?有lots of resources 告诉您如何将字符串拆分为单词。您在哪里遇到了困难?
-
我理解错了,这堵代码与您的实际问题几乎没有关系,即“我如何标记字符串?” - 这个问题在这个网站上可能已经被问了几百次了?