public class text3 { public static void main(String[] args) { try { BufferedReader fis = new BufferedReader(new FileReader("D:\\Java\\JavaSpace\\java_text\\src\\java_text\\Source.txt")); BufferedWriter ois = new BufferedWriter(new FileWriter("D:\\Java\\JavaSpace\\java_text\\src\\java_text\\dost.txt")); String line; int num = 0; while ((line = fis.readLine()) != null) { num++; ois.write(num+": "+line+"\n"); } fis.close(); ois.close(); } catch (IOException e) { e.printStackTrace(); } } }结果是1: 鏄ユ檽 2: 鏄ョ湢涓嶈鏅擄紝 3: 澶勫闂诲暭楦熴?? 4: 澶滄潵椋庨洦澹帮紝 5: 鑺辫惤鐭ュ灏戙??怎么解决
时间: 2024-02-04 09:01:47 浏览: 101
你的代码输出的结果中出现了乱码,是因为文件的编码格式与代码中的编码格式不一致。可以尝试在代码中指定文件编码格式,例如:
```java
BufferedReader fis = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\Java\\JavaSpace\\java_text\\src\\java_text\\Source.txt"), "UTF-8"));
BufferedWriter ois = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Java\\JavaSpace\\java_text\\src\\java_text\\dost.txt"), "UTF-8"));
```
这样可以将文件的编码格式设置为UTF-8,与代码中的编码格式一致,就可以避免出现乱码了。
相关问题
package java_text; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class text3 { public static void main(String[] args) { try { BufferedReader fis = new BufferedReader(new FileReader("D:\\Java\\JavaSpace\\java_text\\src\\java_text\\Source.txt")); BufferedWriter ois = new BufferedWriter(new FileWriter("D:\\Java\\JavaSpace\\java_text\\src\\java_text\\dost.txt")); String line; int num = 0; while ((line = fis.readLine()) != null) { num++; ois.write(num + ": " + line+"\n"); } fis.close(); ois.close(); } catch (IOException e) { e.printStackTrace(); } } }为什么在dost文件里是乱码
这是因为在写入文件时,没有指定写入文件的编码格式。在这个例子中,文件的编码格式使用的是系统默认值,这可能会导致乱码。为了解决这个问题,你可以在创建 BufferedWriter 对象时,指定写入文件的编码格式,例如:
```
BufferedWriter ois = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Java\\JavaSpace\\java_text\\src\\java_text\\dost.txt"), "UTF-8"));
```
这样写入文件的编码格式就是 UTF-8,而不会出现乱码。
修正以下代码:import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Sever { // 保存所有用户的用户名和密码 private static Map<String, String> userMap = new HashMap<>(); // 指定的目录 private static File rootDir = null; // 保存所有登录的用户信息 private static Map<String, String> loginInfo = new HashMap<>(); public static void main(String[] args) throws Exception { // 从配置文件中读取用户信息和目录信息 BufferedReader reader = new BufferedReader(new FileReader("config.txt")); String line = null; while ((line = reader.readLine()) != null) { if (line.startsWith("user:")) { String[] user = line.split(":"); userMap.put(user[1], user[2]); } if (line.startsWith("dir:")) { String[] dir = line.split(":"); rootDir = new File(dir[1]); } } reader.close(); // 启动服务器 ServerSocket serverSocket = new ServerSocket(8000); int count = 0; System.out.println("服务器启动成功!"); while (true) { // 监听客户端连接 Socket socket = serverSocket.accept(); count++; System.out.println("第" + count + "个客户端连接成功!"); // 处理客户端请求 new Thread(new ServerThread(socket)).start();
以下是代码修正:
```java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Server {
// 保存所有用户的用户名和密码
private static Map<String, String> userMap = new HashMap<>();
// 指定的目录
private static File rootDir = null;
// 保存所有登录的用户信息
private static Map<String, String> loginInfo = new HashMap<>();
public static void main(String[] args) throws Exception {
// 从配置文件中读取用户信息和目录信息
BufferedReader reader = new BufferedReader(new FileReader("config.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
if (line.startsWith("user:")) {
String[] user = line.split(":");
userMap.put(user[1], user[2]);
}
if (line.startsWith("dir:")) {
String[] dir = line.split(":");
rootDir = new File(dir[1]);
}
}
reader.close();
// 启动服务器
ServerSocket serverSocket = new ServerSocket(8000);
int count = 0;
System.out.println("服务器启动成功!");
while (true) {
// 监听客户端连接
Socket socket = serverSocket.accept();
count++;
System.out.println("第" + count + "个客户端连接成功!");
// 处理客户端请求
new Thread(new ServerThread(socket, userMap, rootDir, loginInfo)).start();
}
}
}
class ServerThread implements Runnable {
private Socket socket;
private Map<String, String> userMap;
private File rootDir;
private Map<String, String> loginInfo;
public ServerThread(Socket socket, Map<String, String> userMap, File rootDir, Map<String, String> loginInfo) {
this.socket = socket;
this.userMap = userMap;
this.rootDir = rootDir;
this.loginInfo = loginInfo;
}
@Override
public void run() {
try {
// 获取输入输出流
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
// 接收客户端请求
String request = reader.readLine();
if (request == null) {
return;
}
System.out.println("客户端请求:" + request);
// 登录处理
if (request.startsWith("login:")) {
String[] login = request.split(":");
String username = login[1];
String password = login[2];
if (userMap.containsKey(username) && userMap.get(username).equals(password)) {
loginInfo.put(socket.getInetAddress().getHostAddress(), username);
writer.println("login success");
} else {
writer.println("login fail");
}
}
// 列出目录内容处理
else if (request.equals("list")) {
String username = loginInfo.get(socket.getInetAddress().getHostAddress());
if (username == null) {
writer.println("not login");
} else {
File[] files = rootDir.listFiles();
StringBuilder sb = new StringBuilder();
sb.append("total ").append(files.length).append("\n");
for (File file : files) {
sb.append(file.getName()).append("\n");
}
writer.println(sb.toString());
}
}
// 下载文件处理
else if (request.startsWith("get:")) {
String filename = request.substring(4);
String username = loginInfo.get(socket.getInetAddress().getHostAddress());
if (username == null) {
writer.println("not login");
} else {
File file = new File(rootDir, filename);
if (!file.exists()) {
writer.println("file not exist");
} else if (!file.isFile()) {
writer.println("not a file");
} else {
writer.println("file start");
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
socket.getOutputStream().write(buffer, 0, len);
}
fis.close();
socket.getOutputStream().flush();
writer.println("file end");
}
}
}
// 未知请求处理
else {
writer.println("unknown request");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
在主程序中,将 ServerThread 构造函数中需要的参数传入,确保 ServerThread 中可以使用这些参数。
在 ServerThread 中,将所有需要使用的变量都作为成员变量传入构造函数中,确保可以在 run 方法中使用。
另外,在处理下载文件请求时,需要将文件内容以字节流的形式写入输出流,否则客户端无法正确接收到文件。
阅读全文