JAVA读取java.txt中内容,并查找该文件中java字符串的个数,并将查找结果保存到“result.txt”中。谢谢了
3个回答
展开全部
Use this one, it works fine under JDK 1.5
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class StringCount {
public static void main(String[] args) throws IOException {
File inFile = new File("java.txt");
File outputFile = new File("result.txt");
String fileContextStr = readFile(inFile);
String[] ary = fileContextStr.split("\\s+");
int count = 0;
for(String item: ary){
if(item.equals("java")){
count++;
}
}
System.out.println("Total " + count + " string found in file "+ inFile.getName());
writeToResultFile(outputFile, count);
}
private static void writeToResultFile(File file, int result)
throws IOException {
FileOutputStream fo = new FileOutputStream(file);
fo.write(String.valueOf(result).getBytes());
fo.close();
}
private static String readFile(File file) throws FileNotFoundException,
IOException {
StringBuffer sb = new StringBuffer();
FileReader fl = new FileReader(file);
BufferedReader bf = new BufferedReader(fl);
String context = null;
do {
context = bf.readLine();
if (context == null) {
break;
} else if (!context.trim().equals("")) {
sb.append(context + " ");
}
} while (context != null);
bf.close();
return sb.toString();
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class StringCount {
public static void main(String[] args) throws IOException {
File inFile = new File("java.txt");
File outputFile = new File("result.txt");
String fileContextStr = readFile(inFile);
String[] ary = fileContextStr.split("\\s+");
int count = 0;
for(String item: ary){
if(item.equals("java")){
count++;
}
}
System.out.println("Total " + count + " string found in file "+ inFile.getName());
writeToResultFile(outputFile, count);
}
private static void writeToResultFile(File file, int result)
throws IOException {
FileOutputStream fo = new FileOutputStream(file);
fo.write(String.valueOf(result).getBytes());
fo.close();
}
private static String readFile(File file) throws FileNotFoundException,
IOException {
StringBuffer sb = new StringBuffer();
FileReader fl = new FileReader(file);
BufferedReader bf = new BufferedReader(fl);
String context = null;
do {
context = bf.readLine();
if (context == null) {
break;
} else if (!context.trim().equals("")) {
sb.append(context + " ");
}
} while (context != null);
bf.close();
return sb.toString();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-01-04
展开全部
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class StrCount{
public static void main(String[] args) throws IOException {
char[] arr = null;// 存放key
StringBuffer sb = null;
String str;
int value = 0;
FileReader fr = new FileReader("java.txt");// 读取文件的地址.
BufferedReader br = new BufferedReader(fr);
Map<Character, Integer> map = new TreeMap<Character, Integer>();
while (null != (str = br.readLine())) {
arr = str.toCharArray();// 转换成数组然后遍历它放入
for (int i = 0; i < arr.length; i++) {
if (map.get(arr[i]) == null)
map.put(arr[i], 1);
else {
map.put(arr[i], map.get(arr[i]) + 1);
}
}
}
br.close();
Set set = map.keySet();
Character[] keys = new Character[set.size()];
Integer[] Indexs = new Integer[set.size()];
for (Iterator iter = set.iterator(); iter.hasNext();) {
Character key = (Character) iter.next();
Integer index = (Integer) map.get(key);
keys[value] = key;
Indexs[value] = index;
++value;
}
FileOutputStream fos = new FileOutputStream("result.txt");
for (int i = 0; i < keys.length; i++) {
fos.write(keys[i]);
fos.write(Indexs[i]);
}
fos.close();
System.out.println("result.txt 文件已经生成!");
}
}
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class StrCount{
public static void main(String[] args) throws IOException {
char[] arr = null;// 存放key
StringBuffer sb = null;
String str;
int value = 0;
FileReader fr = new FileReader("java.txt");// 读取文件的地址.
BufferedReader br = new BufferedReader(fr);
Map<Character, Integer> map = new TreeMap<Character, Integer>();
while (null != (str = br.readLine())) {
arr = str.toCharArray();// 转换成数组然后遍历它放入
for (int i = 0; i < arr.length; i++) {
if (map.get(arr[i]) == null)
map.put(arr[i], 1);
else {
map.put(arr[i], map.get(arr[i]) + 1);
}
}
}
br.close();
Set set = map.keySet();
Character[] keys = new Character[set.size()];
Integer[] Indexs = new Integer[set.size()];
for (Iterator iter = set.iterator(); iter.hasNext();) {
Character key = (Character) iter.next();
Integer index = (Integer) map.get(key);
keys[value] = key;
Indexs[value] = index;
++value;
}
FileOutputStream fos = new FileOutputStream("result.txt");
for (int i = 0; i < keys.length; i++) {
fos.write(keys[i]);
fos.write(Indexs[i]);
}
fos.close();
System.out.println("result.txt 文件已经生成!");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果是我,我会用str.splite("java").length-1来计算字符串中"java"的个数。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询