Java编程的中间,编写代码从文件已经读取了字典,然后字典添加内容,内容是重复的,怎么都把它们添加到字典里呢
不知道你这个问题是否已经解决, 如果还没有解决的话:记得导包 ,所需要的包 : hsweb-utils-3.0.0.jar commons-io-2.5.jar
package hello2;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.hswebframework.utils.file.EncodingDetect;
public class t4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String filePath="C:\\Users\\asus\\Desktop\\cs2.doc"; //需要判断的文件的路径
//获得文件编码
String fileEncode=EncodingDetect.getJavaEncode(filePath);
System.out.println("文件编码格式:" + fileEncode); //输出判断的文件编码格式
//根据文件编码获得文件内容
try {
String fileContent=FileUtils.readFileToString(new File(filePath),fileEncode); //这个一段可以不用
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
jar包下载 :
commons-io-2.5.jar 提取码:948zProblem: Question Title: How to repeatedly add the contents of a previously read dictionary file to a dictionary in Java programming?
Question Content: I am writing Java code where I have already read a dictionary from a file. Now I want to repeatedly add these contents to a dictionary, regardless of whether they already exist in the dictionary. How can I achieve this functionality?
Solution: To achieve the functionality of repeatedly adding the contents of a previously read dictionary file to a dictionary, you can follow these steps:
Step 1: Read the dictionary file First, you need to read the contents of the dictionary file into a List or an array. Here's an example of reading the contents from a file using BufferedReader:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DictionaryReader {
public static List<String> readDictionaryFile(String filePath) throws IOException {
List<String> dictionary = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
dictionary.add(line);
}
reader.close();
return dictionary;
}
}
Step 2: Create or initialize a dictionary You can use a Map implementation, such as HashMap, to store the dictionary words as keys. The values can be set to some default values or left empty. Here's an example of creating a dictionary:
import java.util.HashMap;
import java.util.Map;
public class Dictionary {
public static void main(String[] args) {
Map<String, Integer> dictionary = new HashMap<>();
// Add some default values
dictionary.put("apple", 0);
dictionary.put("banana", 0);
// Alternatively, you can leave the dictionary empty initially
// Rest of the code here...
}
}
Step 3: Add the contents of the dictionary file to the dictionary Iterate over the previously read dictionary file and add each word to the dictionary. Here's an example of adding the contents to the dictionary:
import java.util.List;
import java.util.Map;
public class Dictionary {
public static void main(String[] args) {
Map<String, Integer> dictionary = new HashMap<>();
// Add some default values
dictionary.put("apple", 0);
dictionary.put("banana", 0);
// Alternatively, you can leave the dictionary empty initially
// Read the dictionary file
try {
List<String> dictionaryFromFile = DictionaryReader.readDictionaryFile("dictionary.txt");
// Add the contents of the dictionary file to the dictionary
for (String word : dictionaryFromFile) {
// Check if the word already exists in the dictionary
if (!dictionary.containsKey(word)) {
// Add the word to the dictionary
dictionary.put(word, 0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
// Rest of the code here...
}
}
By following these steps, you will be able to repeatedly add the contents of a previously read dictionary file to a dictionary in Java programming.
关键步骤是:
使用 Map 表示字典
读取文件,拆分单词,添加到 Map 中
从控制台读取单词,添加到 Map 中
Map 中键为单词,值为出现次数
Map<String, Integer> map = new HashMap<>();
// 读取文件,拆分为单词
Scanner scanner = new Scanner(new File("dictionary.txt"));
while(scanner.hasNext()) {
String word = scanner.next();
map.put(word, map.getOrDefault(word, 0) + 1);
}
scanner.close();
// 从控制台读取单词
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
String word = in.next();
map.put(word, map.getOrDefault(word, 0) + 1);
}
in.close();