2024-10-16 16:33:54
package file.system.demo.exception;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileManpulation {
public static List<String > getLowerCaseWords(File file) {
Scanner scanner = null;
Pattern pattern = Pattern.compile("[a-zA-Z]+");
String text = "";
List<String > words = new ArrayList<>();
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(scanner!=null){
while(scanner.hasNextLine()){
text+=scanner.nextLine();
}
scanner.close();
}
//System.out.println(text);
Matcher matcher = pattern.matcher(text);
while (matcher.find()){
words.add(matcher.group().toLowerCase());
}
return words;
}
/**
*
* @param words
* @param file 输入文件
* void
*/
public static void WriteToFile(List<String> words ,File file){
Collections.sort(words);//排序
FileWriter writer=null;
try {
writer = new FileWriter(file);
for (String word : words) {
writer.write(word+" ");
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static class InnerTest{
public static void main(String[] args) {
File file = new File("D:\\test.txt");
List<String> words=getLowerCaseWords(file);
WriteToFile(words,new File("D:\\in.txt"));
}
}
}
2024-10-16 17:08:16
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 2016/11/22 09:59
*
* @author 3306 TODO 怀疑人生
*/
public class MyFileReader {
public static void main(String[] args) {
String path = "d://test.txt";//文件路径
List<String> englishList = getEnglishCharacter(path);
display(englishList);
}
/**
* 获取文件内英文字符串
*
* @param path 文件路径
* @return list of String
*/
private static List<String> getEnglishCharacter(String path) {
List<String> charList = new ArrayList<>();
try {
File file = new File(path);
if (!file.exists()) {//文件不存在
throw new NullPointerException();
}
FileReader fileReader = new FileReader(file);
Scanner scanner = new Scanner(fileReader);
while (scanner.hasNext()) {
String str = scanner.nextLine();
String[] chars = str.split("\\s+");//根据空格切割,获取每一个字符
for (String eachChar : chars) {
if (isEnglishCharacter(eachChar)) {//英文字符串
charList.add(eachChar.toLowerCase());//改变成小写字母
}
}
}
scanner.close();//关闭流,节省资源
fileReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return charList;
}
/**
* 判断字符串是不是英文字符串
*
* @param str 字符串
* @return 是则返回true, 否则返回false
*/
private static boolean isEnglishCharacter(String str) {
if (null == str || str.trim().equals("")) {
return false;
}
String regex = "^[a-z]+$";
return str.matches(regex);
}
/**
* 显示内容
*
* @param list 字符串列表
*/
private static void display(List<String> list) {
for (String each : list) {
System.out.println(each);
}
}
}
对了大神 ,输出的数据要按字母表排序到新文档中,标题我少打了,能帮忙改下吗?万分感谢!
//已经写好,但字符限制,所以上传到文件
感谢!写的很好了,不过排序要求是每个单词首字母升序,我运行下好像不是这么排的。
2024-10-16 17:35:54
首先你的判断字符串是否含有英文单词
if (str.charAt(i) <= 'z' && str.charAt(i) >= 'a') {
//如果判断是英文字符,则变成大写,
str .toUpperCase();
}
2024-10-16 17:20:56
少了个排序,输出的内容按首字母顺序排序到新文档里