1.以PrintWriter来实现写入
- import java.io.*;
-
- public class test {
-
-
-
-
- public static void main(String[] args) {
- String path="e://a.txt";
- try {
- FileWriter fw=new FileWriter(path,true);
- PrintWriter pw=new PrintWriter(fw);
- pw.println("世界");
- pw.close();
-
- fw.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }
import java.io.*;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
String path="e://a.txt";
try {
FileWriter fw=new FileWriter(path,true);
PrintWriter pw=new PrintWriter(fw);
pw.println("世界");
pw.close();
//bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.以BufferedWriter来实现写入
- import java.io.*;
-
- public class test {
-
- public static void main(String[] args) {
- String path="e://a.txt";
- try {
- FileWriter fw=new FileWriter(path,true);
- BufferedWriter bw=new BufferedWriter(fw);
- bw.newLine();
- bw.write("Hello");
- bw.newLine();
- bw.write("世界");
- bw.close();
- fw.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
import java.io.*;
public class test {
/** * @param args */
public static void main(String[] args) {
String path="e://a.txt";
try {
FileWriter fw=new FileWriter(path,true);
BufferedWriter bw=new BufferedWriter(fw);
bw.newLine();
bw.write("Hello");
bw.newLine();
bw.write("世界");
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- }
- 读文件用BufferedReader br = new BufferedReader( new FileReader(new File("e://a.txt")));
- br.readLine();