java txtreader_一个简单的Java读写文件例子
針對(duì)txt文件的讀寫可以使用java的讀寫操作。以下是一個(gè)簡(jiǎn)單的例子:
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();
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java txtreader_一个简单的Java读写文件例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java三角形剪角_大班数学:拼角剪角
- 下一篇: java徐仕锋_传智播客云计算开发 Ha