FileWriter
package com.yuzhenc.io;
import java.io.*;
public class Test03 {
public static void main(String[] args) throws IOException {
File file = new File("d:\\tmp\\test.sql");
FileWriter fileWriter = new FileWriter(file);
String str = "select * from tmp;\nselect * from a;\n";
for (int i = 0; i < str.length(); i++) {
fileWriter.write(str.charAt(i));
}
fileWriter.close();
FileWriter fileWriter1 = new FileWriter(file,true);
char[] chars = str.toCharArray();
fileWriter1.write(chars);
fileWriter1.close();
}
}
FileReader
package com.yuzhenc.io;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test04 {
public static void main(String[] args) throws IOException {
File file = new File("d:\\tmp\\test.sql");
FileReader fileReader = new FileReader(file);
int n;
while((n = fileReader.read()) != -1) {
System.out.print((char)n);
}
fileReader.close();
System.out.println("--------------------------");
FileReader fileReader1 = new FileReader(file);
FileReader fileReader2 = new FileReader(file);
char[] chars = new char[5];
int n1;
while((n1 = fileReader1.read(chars)) != -1) {
for (int i = 0; i < n1; i++) {
System.out.print(chars[i]);
}
}
System.out.println("--------------------------");
while((n1 = fileReader2.read(chars)) != -1) {
String str = new String(chars,0, n1);
System.out.print(str);
}
fileReader1.close();
fileReader2.close();
}
}
文本文件复制
package com.yuzhenc.io;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test05 {
public static void main(String[] args) throws IOException {
File file = new File("d:\\tmp\\testcopy.sql");
File file1 = new File("d:\\tmp\\test.sql");
FileWriter fileWriter = new FileWriter(file);
FileReader fileReader = new FileReader(file1);
char[] chars = new char[5];
int len;
while((len = fileReader.read(chars)) != -1) {
String str = new String(chars,0,len);
fileWriter.write(str);
}
fileWriter.close();
fileReader.close();
}
}