package org.hanqi.ex;
import java.io.*;
public class TestFile2 {
public static void main(String[] args) {
try {
FileOutputStream fos=new FileOutputStream("d:\\test.txt");
String str="大家下午好!";
fos.write(str.getBytes());//覆盖写入
//追加写入
fos.close();
FileInputStream fis=new FileInputStream("d:\\test.txt");
byte[]b=new byte[200];
int i=fis.read(b);
String str1=new String(b,0,i);
System.out.println("读取内容:"+str1);
fis.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
package org.hanqi.ex;
import java.io.*;
public class TestFile3 {
public static void main(String[] args) {
try {
//读取
FileReader fr=new FileReader("d:\\test.txt");
char[]c=new char[200];
int i=fr.read(c);
String str=new String(c,0,i);
System.out.println("读取内容"+str);
fr.close();
//写入
FileWriter fw=new FileWriter("d:\\test.txt",true);
fw.write("\n新写入的内容");
fw.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}