package com.test.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class ReadWrite { public static void main(String[] args) { String inurl="D:/123.txt"; File infile=new File(inurl); String outurl="D:/asd.txt"; File outfile=new File(outurl); try { //如果写入的文件不存在创建新文件 if(!outfile.exists()){ outfile.createNewFile(); } //文件的输入流读取文件 FileInputStream in=new FileInputStream(infile); FileOutputStream out=new FileOutputStream(outfile); //读文件 BufferedReader read=new BufferedReader(new InputStreamReader(in)); //写文件 BufferedWriter write=new BufferedWriter(new OutputStreamWriter(out)); String temp=""; while((temp=read.readLine())!=null){ //写入文件 write.write(temp+" "); System.out.println("写入"+temp); } read.close(); write.close(); out.close(); in.close(); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }