输出流写文件
package Test10;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("d:\\a.txt");
FileWriter fw2 = new FileWriter("a.txt");
fw.write("hello");
fw.flush();
fw.write("world");
fw.flush();
fw.close();
}
}
写数据的五种方法
package Test10;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo2 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("b.txt",true);
fw.write("hello");
fw.write("\r\n");
fw.flush();
fw.write("helloworld",2,5);
fw.write("\r\n");
fw.flush();
fw.write('a');
fw.write(97);
fw.write("\r\n");
fw.flush();
char[] chs = {'h','e','l','l','o'};
fw.write(chs,2,2);
fw.write("\r\n");
fw.flush();
fw.close();
}
}
输入流读文件
package Test10;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("b.txt");
int ch;
while((ch=fr.read()) != -1){
System.out.print((char)ch);
}
char[] chs = new char[1024];
int len;
while((len = fr.read(chs)) != -1){
System.out.println(new String(chs,0,len));
}
}
}
复制文件的五种方式
package Test10;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyFileTest {
public static void main(String[] args) throws IOException {
String fileReadName = "b.txt";
String fileWriteName = "copy.txt";
method1(fileReadName,fileWriteName);
}
public static void method1(String fileReadName,String fileWriteName) throws IOException{
FileReader fr = new FileReader(fileReadName);
FileWriter fw = new FileWriter(fileWriteName);
int ch;
while((ch=fr.read()) != -1){
fw.write(ch);
}
fw.close();
fr.close();
}
public static void method2(String fileReadName,String fileWriteName) throws IOException{
FileReader fr = new FileReader(fileReadName);
FileWriter fw = new FileWriter(fileWriteName);
char[] chs = new char[1024];
int len;
while((len=fr.read(chs)) != -1){
fw.write(chs, 0, len);
}
fw.close();
fr.close();
}
public static void method3(String fileReadName,String fileWriteName) throws IOException{
FileReader fr = new FileReader(fileReadName);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(fileWriteName);
BufferedWriter bw = new BufferedWriter(fw);
int ch;
while((ch=br.read()) != -1){
bw.write(ch);
}
bw.close();
br.close();
}
public static void method4(String fileReadName,String fileWriteName) throws IOException{
FileReader fr = new FileReader(fileReadName);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(fileWriteName);
BufferedWriter bw = new BufferedWriter(fw);
char[] chs = new char[1024];
int len;
while((len=br.read(chs)) != -1){
bw.write(chs,0,len);
}
bw.close();
br.close();
}
public static void method5(String fileReadName,String fileWriteName) throws IOException{
FileReader fr = new FileReader(fileReadName);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(fileWriteName);
BufferedWriter bw = new BufferedWriter(fw);
String str;
while((str=br.readLine()) != null){
bw.write(str);
bw.newLine();
}
bw.close();
br.close();
}
}