2014-08-27 3 views
-3

안녕하세요 남자 특정 숫자의 단어 다음에 java를 사용하여 파일을 분할하고 수정 단어 제한이있는 많은 수의 파일로 나누고 싶습니다.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()); 
      } 

      } 


} 

는 지금은 같지만 단어를 계산 한 후 작업을 수행해야합니다.

+0

좋아, 그래서 당신은 무엇을 시도? 문자열을 단어로 분할하는 방법을 알려주는 [많은 자원] (http://stackoverflow.com/questions/11726023/split-string-into-individual-words-java)이 있습니다. 너 어디서 이걸 구현 했니? –

+0

나는이 코드의 벽이 실제 질문과는 거의 관계가 없다는 것을 나의 이해에 착오가있다. "어떻게 문자열을 토큰 화합니까?" - 아마도이 사이트에서 몇 백 번 묻는 질문일까요? –

답변

0

파일에서 문자를 읽은 다음 공백 문자로 구분 기호로 분리하고 단어를 세고 단어 cont를 파일에 기록합니다.


FileReader reader = 새 FileReader (새 파일 ("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); 
    }