Java语言中,读取文件里的内容到一个字典,字典交换键值,不能出现错误,写入文件,联合起来的写法
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class VariableSwapper {
public static void main(String[] args) throws IOException {
Map<String, String> variables = new HashMap<>();
Scanner scanner = new Scanner(new File("input.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split("=");
variables.put(parts[0], parts[1]);
}
scanner.close();
String temp = variables.get("a");
variables.put("a", variables.get("b"));
variables.put("b", variables.get("c"));
variables.put("c", temp);
StringBuilder output = new StringBuilder();
for (Map.Entry<String, String> entry : variables.entrySet()) {
output.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
}
Files.write(Paths.get("output.txt"), output.toString().getBytes());
}
}
以下是 Java 代码示例,实现读取文件内容到一个字典,交换字典的键值,并将交换后的字典写入文件的联合操作:
import java.io.*;
import java.util.*;
public class FileIOExample {
public static void main(String[] args) {
// 读取文件内容到一个字典
Map<String, String> dictionary = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
String line;
while ((line = br.readLine()) != null) {
String[] words = line.split("=");
dictionary.put(words[0], words[1]);
}
} catch (FileNotFoundException e) {
System.err.println("未找到文件!");
} catch (IOException e) {
System.err.println("读取文件失败!");
}
// 字典交换键值
Map<String, String> swappedDictionary = new HashMap<>();
for (Map.Entry<String, String> entry : dictionary.entrySet()) {
swappedDictionary.put(entry.getValue(), entry.getKey());
}
// 将交换后的字典写入文件
try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
for (Map.Entry<String, String> entry : swappedDictionary.entrySet()) {
bw.write(entry.getKey() + "=" + entry.getValue() + "\n");
}
} catch (IOException e) {
System.err.println("写入文件失败!");
}
}
}
上述代码首先读取名为 "input.txt" 的文件中的内容到一个字典(使用 HashMap 实现),然后交换字典的键值,存储到另一个字典中。最后,将交换后的字典写入名为 "output.txt" 的文件中,格式同样采用 key=value 的形式。需要注意的是,在使用 I/O 操作时,应该始终捕获可能出现的异常,以确保程序不会因为意外情况而崩溃。
不知道你这个问题是否已经解决, 如果还没有解决的话:/**
* 线程池
*/
private static ThreadPoolExecutor executor =
new ThreadPoolExecutor(
// 核心线程数和最大线程数
2, 3,
// 线程空闲后的存活时间
60L, TimeUnit.SECONDS,
// 有界阻塞队列
new LinkedBlockingQueue<Runnable>(5)
);
我可以给出一个实现代码,请参考如下:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try {
// 读取文件内容
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
while (line != null) {
sb.append(line);
line = reader.readLine();
}
// 字典交换键值
Map<Character, Character> dict = new HashMap<>();
dict.put('a', 'b');
dict.put('b', 'a');
dict.put('c', 'd');
dict.put('d', 'c');
String content = sb.toString();
StringBuilder result = new StringBuilder();
for (char c : content.toCharArray()) {
if (dict.containsKey(c)) {
result.append(dict.get(c));
} else {
result.append(c);
}
}
// 写入文件
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
writer.write(result.toString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这段代码做了如下几步:
读取文件内容,可以使用BufferedReader
类实现,不仅能够按行读取,而且可以提高读取文件的效率。
字典交换键值,这里我们使用了HashMap
类来实现,将需要交换的键值对作为参数传入。
读取到的文件内容中,将每一个字符交换键值后组成新的字符串。
将新的字符串写入文件中,这里使用了BufferedWriter
类来实现。
如果还有什么不清楚的地方,欢迎咨询。