字符流的缓冲区按行读取
public class Test10 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
字符流缓冲区读取并写入
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("ss.txt"));
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine(); //写出回车换行符
//bw.write("\r\n");
/*newline()方法与\r\n的区别
* newLine()是跨平台的方法
* \r\n只支持的是windows系统
*
* */
}
bw.close();
br.close();
//readline()是属于bufferReader的,newLine()方法是属于bufferWriter类的
}
将文件中的文件中的信息倒着写入另一个文件
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("rever.txt"));
ArrayList<String> list = new ArrayList<>();
String line;
while((line=br.readLine())!=null){
list.add(line);
}
for(int i =list.size()-1;i>=0;i--){
bw.write(list.get(i));
bw.newLine();
}
bw.close();
br.close();
}