在java中的某些单词限制后拆分文件

时间:2014-08-27 12:04:15

标签: java arrays file

嘿伙计我想在一定数量的单词之后使用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());  
             }  

            }  


}

`

现在我必须做同样的事情,但在计算完毕后。

1 个答案:

答案 0 :(得分:0)

从文件中读取字符并将其作为分隔符分隔在空格字符上并计算单词并根据单词cont写入文件。


FileReader reader = new FileReader(new file(“FILE PATH”));

    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);
     }