首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

IO源读取文件

2013-11-03 
IO流读取文件import java.io.BufferedReaderimport java.io.BufferedWriterimport java.io.Fileimport

IO流读取文件

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;public class FileOperate {public static void main(String[] args) throws Exception {readAndWriteBy();readAndWrite();}//读取文件,再写入文件public static void readAndWriteBy() throws Exception{//一种FileInputStream f = new FileInputStream("d:/1.txt");InputStreamReader isr = new InputStreamReader(f,"GBK");BufferedReader br = new BufferedReader(isr);OutputStream fop = new FileOutputStream("d:/2.txt");OutputStreamWriter osr = new OutputStreamWriter(fop,"GBK");BufferedWriter bw = new BufferedWriter(osr);String line="" ;while((line=br.readLine())!=null){bw.write(line);}bw.flush();bw.close();br.close();}public static void readAndWrite() throws Exception{//二种File file = new File("d:/1.txt");BufferedReader br = new BufferedReader(new FileReader(file));BufferedWriter bw = new BufferedWriter(new FileWriter("d:/3.txt"));String line="" ;while((line=br.readLine())!=null){bw.write(line);}bw.flush();bw.close();br.close();}}

热点排行