1.ReaderDemo01
package cn.kongxh.io3;
import java.io.File ;
import java.io.Reader ;
import java.io.FileReader ;
public class ReaderDemo01{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
Reader input = null ;
input = new FileReader(f) ;
char c[] = new char[1024] ;
int len = input.read(c) ;
input.close() ;
System.out.println("内容为:" + new String(c,0,len)) ;
}
}
2.ReaderDemo02
package cn.kongxh.io3;
import java.io.File ;
import java.io.Reader ;
import java.io.FileReader ;
public class ReaderDemo02{
public static void main(String args[]) throws Exception{
File f= new File("d:" + File.separator + "test.txt") ;
Reader input = null ;
input = new FileReader(f) ;
char c[] = new char[1024] ;
int temp = 0 ;
int len = 0 ;
while((temp=input.read())!=-1){
c[len] = (char)temp ;
len++ ;
}
input.close() ;
System.out.println("内容为:" + new String(c,0,len)) ;
}
}
3.一行一行地读
public static void main(String[] args) throws IOException {
File file = new File("D:\\helloWorld.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String str;
while ((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}
}
4.写内容
public static void main(String[] args) throws IOException {
File file = new File("D:\\helloWorlds.txt ");
if(!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file,true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.append("hello");
bufferedWriter.close();
fileWriter.close();
}