Java回炉之IO流(二)使用规律
[toc]
源代码下载
文本文件到文本文件
public class TextFile2TextFile {
public static void main(String[] args) {
try {
//因为是文本文件,所以用FileReader和FileWriter
FileReader fileReader = new FileReader("a.txt");
FileWriter fileWriter = new FileWriter("b.txt");
//为了高效,使用带缓冲区的流
BufferedReader bufferedReader=new BufferedReader(fileReader);
BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
//写入方式:以字节为单位写入数据
int b=0;
while((b=bufferedReader.read())!=-1){
bufferedWriter.write(b);
}
//写入方式优化:以行为单位写入数据
String string=null;
while((string= bufferedReader.readLine())!=null){
bufferedWriter.write(string);
}
//最后记着从缓冲区写入到文件并关闭流
bufferedWriter.flush();
bufferedWriter.close();
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
键盘输入到文本文件
public class Keyboard2TextFile {
public static void main(String[] args) {
try {
//输入是键盘,使用System.in
InputStream inputStream =System.in;
//输出是文本文件,使用FileWriter
FileWriter fileWriter = new FileWriter("test.txt");
//为了高效,将输入流封装为转换流
InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
//为了高效,使用缓冲流再次封装
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
//写入方式:以字节为单位写入数据
int b=0;
while((b=bufferedReader.read())!=-1){
bufferedWriter.write(b);
}
//写入方式优化:以行为单位写入数据
String string=null;
while((string= bufferedReader.readLine())!=null){
bufferedWriter.write(string);
}
//最后记着从缓冲区写入到文件并关闭流
bufferedWriter.flush();
bufferedWriter.close();
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
文本文件到屏幕
public class TextFile2Screen {
public static void main(String[] args) {
try {
//输入是文件,使用FileReader
FileReader fileReader= new FileReader("test.txt");
//输出是屏幕,使用System.out
OutputStream outputStream=System.out;
//为了高效,将输出流封装为转换流
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
//为了高效,使用缓冲流再次封装
BufferedReader bufferedReader=new BufferedReader(fileReader);
BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
//写入方式:以字节为单位写入数据
int b=0;
while((b=bufferedReader.read())!=-1){
bufferedWriter.write(b);
}
//写入方式优化:以行为单位写入数据
String string=null;
while((string= bufferedReader.readLine())!=null){
bufferedWriter.write(string);
}
//最后记着从缓冲区写入到文件并关闭流
bufferedWriter.flush();
bufferedWriter.close();
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
键盘到屏幕
public class Keyboard2Screen {
public static void main(String[] args) {
try {
//输入是键盘,使用System.in
InputStream inputStream =System.in;
//输出是屏幕,使用System.out
OutputStream outputStream=System.out;
//为了高效,将输入流封装为转换流
InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(outputStream);
//为了高效,使用缓冲流再次封装
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
//写入方式:以字节为单位写入数据
int b=0;
while((b=bufferedReader.read())!=-1){
bufferedWriter.write(b);
}
//写入方式优化:以行为单位写入数据
String string=null;
while((string= bufferedReader.readLine())!=null){
bufferedWriter.write(string);
}
//最后记着从缓冲区写入到文件并关闭流
bufferedWriter.flush();
bufferedWriter.close();
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
带字符集的文件到文件
public class TextFile2TextFileWithCharset {
public static void main(String[] args) {
try {
//输入是文本文件,所以用FileReader
FileReader fileReader = new FileReader("a.txt");
//输出虽是文本文件,但制定了字符集则不能使用FileWriter,需要使用OutputStreamWriter
OutputStreamWriter outputStreamWriter= new OutputStreamWriter(new FileOutputStream("b.txt"),"utf-8");
//为了高效,使用带缓冲区的流
BufferedReader bufferedReader=new BufferedReader(fileReader);
BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
//写入方式:以字节为单位写入数据
int b=0;
while((b=bufferedReader.read())!=-1){
bufferedWriter.write(b);
}
//写入方式优化:以行为单位写入数据
String string=null;
while((string= bufferedReader.readLine())!=null){
bufferedWriter.write(string);
}
//最后记着从缓冲区写入到文件并关闭流
bufferedWriter.flush();
bufferedWriter.close();
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}