概述
方式一、方式二
java.io.FileReader,java.io.FileWriter
package com.it;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileReaderAndFileWriter {
public static void main(String[] args) throws IOException {
method01();
}
public static void method01() {
FileReader fr = new FileReader("BufferedReaderDemo.java");
FileWriter fw = new FileWriter("copy1.txt");
int ch;
while (-1 != (ch = fr.read())) {
System.out.print((char)ch);
fw.write((char)ch);
fw.flush();
}
fw.close();
fr.close();
}
public static void method02() {
FileReader fr = new FileReader("BufferedReaderDemo.java");
FileWriter fw = new FileWriter("copy2.txt");
int len;
char[] ch = new char[1024];
while (-1 != (len = fr.read(ch))) {
System.out.println(new String(ch, 0, len));
fw.write(new String(ch, 0, len));
fw.flush();
}
fw.close();
fr.close();
}
}
方式三、四、五
java.io.BufferedReader,java.io.BufferedWriter
package com.it;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedReaderAndBufferedWriterDemo {
public static void main(String[] args) throws IOException {
method03();
}
public static void method03() {
BufferedReader br = new BufferedReader(new FileReader("BufferedReaderDemo.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy3.txt"));
int ch;
while (-1 != (ch = br.read())) {
System.out.print((char)ch);
bw.write((char)ch);
bw.flush();
}
bw.close();
br.close();
}
public static void method04() {
BufferedReader br = new BufferedReader(new FileReader("BufferedReaderDemo.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy4.txt"));
int len;
char[] ch = new char[1024];
while (-1 != (len = br.read(ch))) {
System.out.print(new String(ch, 0, len));
bw.write(ch, 0, len);
bw.flush();
}
bw.close();
br.close();
}
public static void method05() {
BufferedReader br = new BufferedReader(new FileReader("BufferedReaderDemo.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy5.txt"));
String line;
while (null != (line = br.readLine())) {
System.out.println(line);
bw.write(line);
//bw.write("rn");
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
}
最后
以上就是心灵美果汁为你收集整理的【IO流】读写文件的五种方式的全部内容,希望文章能够帮你解决【IO流】读写文件的五种方式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复