import java.io.*;
class TestIO
{
public static void main(String[] args) throws Exception
{
FileReader fr=new FileReader("wo.txt");
BufferedReader br=new BufferedReader(fr);
StringBuffer buf=new StringBuffer();
int words = 0;
int ip = 0;
String ln=null;
boolean wordflag = false;
char c[]={};
while((ln=br.readLine())!=null){
buf.append(" "+ln);
String str=buf.toString();
c=str.toCharArray();
System.out.println(str);}
for(int i=0; i<c.length; i++){
if((c[i]>='a' && c[i] <= 'z') || (c[i] >= 'A' && c[i] <= 'Z')){
if(wordflag) {
continue;
}else{
words++;
}
wordflag = true;
}else{
wordflag = false;
if(c[i] != ' ')
ip++;
}
}
System.out.println("words=" + words);
System.out.println("ip=" + ip);
}
}
class TestIO
{
public static void main(String[] args) throws Exception
{
FileReader fr=new FileReader("wo.txt");
BufferedReader br=new BufferedReader(fr);
StringBuffer buf=new StringBuffer();
int words = 0;
int ip = 0;
String ln=null;
boolean wordflag = false;
char c[]={};
while((ln=br.readLine())!=null){
buf.append(" "+ln);
String str=buf.toString();
c=str.toCharArray();
System.out.println(str);}
for(int i=0; i<c.length; i++){
if((c[i]>='a' && c[i] <= 'z') || (c[i] >= 'A' && c[i] <= 'Z')){
if(wordflag) {
continue;
}else{
words++;
}
wordflag = true;
}else{
wordflag = false;
if(c[i] != ' ')
ip++;
}
}
System.out.println("words=" + words);
System.out.println("ip=" + ip);
}
}
import java.util.StringTokenizer;class TodenTest
{
public static void main(String[] args)
{ String tok= "''?.,;:!--";
int wn=0,mn=0;
String ln=null,str=null;
try{
FileReader fr=new FileReader("wo.txt");
BufferedReader br=new BufferedReader(fr);
StringBuffer buf=new StringBuffer();
while((ln=br.readLine())!=null){
buf.append(" "+ln);
str=buf.toString();
StringTokenizer words=new StringTokenizer(str);
StringTokenizer s=new StringTokenizer(str,tok);
wn=words.countTokens();
mn=s.countTokens();
}
System.out.println("words="+wn+'\n'+"s="+mn+'\n'+str);
}catch (Exception e){}
}
}
直接读取string 对象。我想省去下边代码:
StringBuffer buf=new StringBuffer();
while((ln=br.readLine())!=null){
buf.append(" "+ln);