자바(java)로만 WordCount 만들어보기
import java.util.StringTokenizer;
import java.util.Scanner;
import java.io.*;
public class assignment {
public static void main(String args[]) {
BufferedReader in = null;
try {
BufferedReader br = new BufferedReader(new FileReader("test1130.txt"));
FileOutputStream output = new FileOutputStream("convert.txt");
while(true) {
String line = br.readLine();
if (line==null) break;
//System.out.print(line);
//System.out.println(line);
output.write(line.getBytes());
}
br.close();
output.close();
in = new BufferedReader(new FileReader("convert.txt"));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("\\W");
int [] arr = new int [splited.length];
for(int i = 0; i < splited.length ; i++){
for(int j = 0 ; j < splited.length ; j++){
if(splited[i].equals(splited[j]))
arr[i]++;
}
}
for(int i = 0; i < splited.length ; i++){
for(int j = i+1 ; j < splited.length ; j++){
if(splited[i].equals(splited[j]))
arr[j] = 0;
}
if(arr[i] != 0)
System.out.println(splited[i] + " : " +arr[i]);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
}
}
import java.util.Scanner;
import java.io.*;
public class assignment {
public static void main(String args[]) {
BufferedReader in = null;
try {
BufferedReader br = new BufferedReader(new FileReader("test1130.txt"));
FileOutputStream output = new FileOutputStream("convert.txt");
while(true) {
String line = br.readLine();
if (line==null) break;
//System.out.print(line);
//System.out.println(line);
output.write(line.getBytes());
}
br.close();
output.close();
in = new BufferedReader(new FileReader("convert.txt"));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("\\W");
int [] arr = new int [splited.length];
for(int i = 0; i < splited.length ; i++){
for(int j = 0 ; j < splited.length ; j++){
if(splited[i].equals(splited[j]))
arr[i]++;
}
}
for(int i = 0; i < splited.length ; i++){
for(int j = i+1 ; j < splited.length ; j++){
if(splited[i].equals(splited[j]))
arr[j] = 0;
}
if(arr[i] != 0)
System.out.println(splited[i] + " : " +arr[i]);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
}
}
test1130.txt
----------------------------------------------------------------------------------------------
How many tests have you taken?
How many tests are you going to take?
To take tests or not, that is the question.
How many tests have you taken?
How many tests are you going to take?
To take tests or not, that is the question.
How many tests have you taken?
How many tests are you going to take?
To take tests or not, that is the question.
How many tests have you taken?
How many tests are you going to take?
To take tests or not, that is the question.
How many tests have you taken?
How many tests are you going to take?
To take tests or not, that is the question.
How many tests have you taken?
How many tests are you going to take?
To take tests or not, that is the question.
How many tests have you taken?
How many tests are you going to take?
To take tests or not, that is the question.
How many tests have you taken?
How many tests are you going to take?
To take tests or not, that is the question.
How many tests have you taken?
How many tests are you going to take?
To take tests or not, that is the question.
How many tests have you taken?
How many tests are you going to take?
To take tests or not, that is the question.
---------------------------------------------------------------------------------------------------
convert.txt
---------------------------------------------------------------------------------------------------
How many tests have you taken?How many tests are you going to take?To take tests or not, that is the question.How many tests have you taken?How many tests are you going to take?To take tests or not, that is the question.How many tests have you taken?How many tests are you going to take?To take tests or not, that is the question.How many tests have you taken?How many tests are you going to take?To take tests or not, that is the question.How many tests have you taken?How many tests are you going to take?To take tests or not, that is the question.How many tests have you taken?How many tests are you going to take?To take tests or not, that is the question.How many tests have you taken?How many tests are you going to take?To take tests or not, that is the question.How many tests have you taken?How many tests are you going to take?To take tests or not, that is the question.How many tests have you taken?How many tests are you going to take?To take tests or not, that is the question.How many tests have you taken?How many tests are you going to take?To take tests or not, that is the question.
---------------------------------------------------------------------------------------------------
1. 우선 test1130.txt 파일을 읽어서 getBytes() 메소드를 사용하여 위와같이 convert.txt 파일에 저장한다.
2. 그리고 다시 convert.txt를 읽어서 split을 해주고, 각각의 스플릿된 것을 비교 해가며 같을 경우 arr를 증가시킨다.
댓글
댓글 쓰기