对于任何文件,不管有没有扩展名,都可以读写。切记,最后要.close();,否则效果出不来。
读操作:
package com.wjy.read; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader; public class FileReader {
public void readContent(){
String content=null;
BufferedReader br=null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:/svndemo/repository/conf/passwd"))));
while ((content=br.readLine())!=null) {
System.out.println(content);
}
br.close();
} catch (Exception e) {
// TODO: handle exception
} }
}
写操作;
package com.wjy.write; import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter; public class FileWriter { public void writeContent(){
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:/svndemo/repository/conf/authzz")));
bw.write("xxx");
bw.close(); } catch (Exception e) {
// TODO: handle exception
} } }
main函数:
package com.wjy.main; import com.wjy.read.FileReader;
import com.wjy.write.FileWriter; public class MainOption {
public static void main(String args[]){
// TxtReader reader=new TxtReader();
// reader.readContent();
FileWriter writer=new FileWriter();
writer.writeContent();
}
}