String s="[姓名1;学号1;年龄1;性别1;手机1;住址1;,姓名2;学号2;年龄2;性别2;手机2;住址2;]";
Pattern pn=Patterncompile("[^\\[,](;){6}");
Matcher mh=pnmatcher(s);
while(mhfind())
Systemoutprintln(mhgroup());
}import javaioBufferedReader;
import javaioFileReader;
import javaioIOException;
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("c:/testtxt"));
StringBuilder sb = new StringBuilder();
while (true) {
String line = brreadLine();
if (line == null)
break;
sbappend(line);
}
brclose();
String content = sbtoString();
Systemoutprintln("Character count is:" + (contentlength() - contentreplaceAll("[a-zA-Z]", "")length()));
Systemoutprintln("Number count is:" + (contentlength() - contentreplaceAll("[0-9]", "")length()));
Systemoutprintln("Special count is :" + contentreplaceAll("[\\w ]", "")length());
}
}
----------------------------------
c盘下新建个testtxt文件,内容:
abc123!@#
----------------------------------
输出:
Character count is:3
Number count is:3
Special count is :3import javaioFile;
import javaioFileInputStream;
import javaioFileWriter;
import javautilScanner;
public class Test2 {
public static void main(String[] args) throws Exception{
Scanner san = new Scanner(Systemin);
Systemoutprintln("Input the text:");
String strText = sannextLine();
String[] strArr = strTextsplit(" ");
File file = new File("filetxt");
FileWriter input = new FileWriter(file);
inputwrite(strText);
inputclose();
Systemoutprintln("单词的数目" + strArrlength);
}
}
import javaioBufferedReader;
import javaioFile;
import javaioFileInputStream;
import javaioInputStreamReader;
import javautilArrayList;
import javautilCollections;
import javautilList;
public class sss {
/
@param args
/
public static void main(String[] args) {
// TODO Auto-generated method stub
List list = new ArrayList();
try {
String encoding = "GBK";
File file = new File("d:\\logtxt");
if (fileisFile() && fileexists()) { //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReaderreadLine()) != null) {
Systemoutprintln(lineTxt);
String str = lineTxt;
List listOne = new ArrayList();
for (int i = 0; i < strlength(); i++) {
listOneadd(strcharAt(i));
}
int a = Collectionsfrequency(listOne, 'a');
//
//
Systemoutprintln(str+"===>统计数量:a:"+a);
}
readclose();
} else {
Systemoutprintln("找不到指定的文件");
}
} catch (Exception e) {
Systemoutprintln("读取文件内容出错");
eprintStackTrace();
}
}
}
#include <stdioh>#include <stringh>
int c1[128]={0},c2[128]={0};
int func(const char filename,int no)
{
FILE fp = NULL;
fp = fopen(filename,"r");
if (fp==NULL)
{
printf("open file 1 failed\r\n");
return -1;
}else
{
char ch;
while(!feof(fp))
{
ch=fgetc(fp);
if(no==1)
c1[ch]++;
if(no==2)
c2[ch]++;
}
fclose(fp);
}
return 0;
}
int main()
{
int i=0,xt=0,bt =0;
func("123txt",1);
func("456txt",2);
for (i=0;i<128;i++)
{
if (c1[i]!=0 && c2[i]!=0)
{
if (c1[i]==c2[i])
{
xt++;
}else
{
bt++;
}
}
}
printf("same [%d] diff[%d]\r\n",xt,bt);
return 0;
}
same [9] diff[5]
Press any key to continue
123txt内容 qwfs12342rsfsgfb2 236#$%^&(sdf
456txt内容 sg234y#$%^&9gdfiodfjnhdsdf sdfsdf
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)