如何统计一篇英文出现的字符个数和某个单词的出现次数最后生成该文本的密文
“An injury-time penalty from substitute Francesco Totti took ten-man
Italy past Australia and into the last eight of the 2006 FIFA World Cup?
in Kaiserslautern on Monday, 26 June 2006.
Totti, a 75th-minute replacement for Alessandro Del Piero,
drove the spot-kick high into the net after Fabio Grosso had gone down
under a Lucas Neill challenge just seconds before the final whistle.
Italy had been reduced to ten men following Marco Materazzi's red card
five minutes into the second half and at that stage their followers
might have feared a repeat of their loss to Guus Hiddink's Korea
Republic at this stage four years ago. Instead, Totti's strike
leaves the Azzurri looking forward to a quarter-final tie against
either Switzerland or Ukraine in Hamburg on Friday.
As for Hiddink's Australia team, they can head for home proud of
their performance in Germany, having reached the Round of 16 on
their first outing on the world stage in 32 years. ”
将上述文字生成TXT文档,并通过输入流传入Java程序中,要求:
1、统计出字符个数
2、统计出“Italy”出现的次数
3、将所有的“Italy”替换成为“Germany”
4、生成该文本的密文(密文是每个字符后续第三个字符。比如:A-----D;B-----E)
我是新手 实在写不来 怎么写怎么错 求高人指点啊~~~~ java
[解决办法]
后面懒得加注释了 你应该看的懂
import java.io.*;
public class text
{
public static String s = "An injury-time penalty from substitute Francesco Totti took ten-man " +
"Italy past Australia and into the last eight of the 2006 FIFA World Cup? " +
"in Kaiserslautern on Monday, 26 June 2006. " +
"Totti, a 75th-minute replacement for Alessandro Del Piero, " +
"drove the spot-kick high into the net after Fabio Grosso had gone down " +
"under a Lucas Neill challenge just seconds before the final whistle. " +
"Italy had been reduced to ten men following Marco Materazzi's red card " +
"five minutes into the second half and at that stage their followers " +
"might have feared a repeat of their loss to Guus Hiddink's Korea " +
"Republic at this stage four years ago. Instead, Totti's strike " +
"leaves the Azzurri looking forward to a quarter-final tie against " +
"either Switzerland or Ukraine in Hamburg on Friday. " +
"As for Hiddink's Australia team, they can head for home proud of " +
"their performance in Germany, having reached the Round of 16 on " +
"their first outing on the world stage in 32 years.";
public static File file = new File("C:\\text.txt");//生成文件
public static String s1 = "Italy";//需处理的字符串
public static String s2 = "Germany";//需替换的字符串
/**
* 创建文件、写入内容
*/
public static void createFile(String s){
try
{
RandomAccessFile raf = new RandomAccessFile(file,"rw");//打开文件
raf.writeBytes(s);//写入内容
}
catch ( IOException e )
{
e.printStackTrace(); //To change body of catch statement use File
[解决办法]
Settings
[解决办法]
File Templates.
}
}
/**
* 读取文件中的内容
*/
public static String readFile(File file){
InputStreamReader read = null;
String line = null;
if (file.isFile() && file.exists()) {//判断是否存在
try
{
read = new InputStreamReader(new FileInputStream(file), "GBK");
BufferedReader bufferedReader = new BufferedReader(read);
line = bufferedReader.readLine();
read.close();
}
catch ( IOException e )
{
e.printStackTrace(); //To change body of catch statement use File
[解决办法]
Settings
[解决办法]
File Templates.
}
}else{
System.out.println("找不到指定的文件!");
}
return line;
}
public static void main (String args[]){
createFile(s);//第一步生成文件
StringBuffer rf = new StringBuffer(readFile( file));//第二部读取文件中的内容
System.out.println("原始内容:"+rf);//输出原始内容
System.out.println("文件长度:" + rf.length());
int times = 0;
for (int i=0;i<rf.length()-s1.length()-1;i++){
String s3 = rf.substring(i,i+s1.length()).toString();
if(s3.equals(s1 )){
rf.replace(i,i+s1.length(),s2);
times++;
}
}
System.out.println(s1+"出现次数:"+times);
System.out.println("修改后内容:"+rf);
}
}