针对txt文件的读写可以使用java的读写操作。以下是一个简单的例子:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class TXTReaderAndWriter {
public static void main(String args[]) {
String path = "test.txt";
readtxt(path);
writetxt(path);
}
private static void readtxt(String path) {
try {
InputStream in = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(in,
"gb2312"));
String tmp = "";
while ((tmp = br.readLine()) != null) {
System.out.println(tmp);
}
br.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writetxt(String path) {
try {
OutputStream ou = new FileOutputStream(path);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ou,
"gb2312"));
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in, "utf-8"));
String tmp = "";
while ((tmp = br.readLine()) != null && !tmp.equals("exit")) {
bw.write(tmp);
bw.flush();
bw.newLine();
}
br.close();
bw.close();
ou.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class TXTReaderAndWriter {
public static void main(String args[]) {
String path = "test.txt";
readtxt(path);
writetxt(path);
}
private static void readtxt(String path) {
try {
InputStream in = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(in,
"gb2312"));
String tmp = "";
while ((tmp = br.readLine()) != null) {
System.out.println(tmp);
}
br.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writetxt(String path) {
try {
OutputStream ou = new FileOutputStream(path);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ou,
"gb2312"));
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in, "utf-8"));
String tmp = "";
while ((tmp = br.readLine()) != null && !tmp.equals("exit")) {
bw.write(tmp);
bw.flush();
bw.newLine();
}
br.close();
bw.close();
ou.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}