import java.io.*; public class IOlianxi1 { public static void main(String[] args) throws IOException { System.out.println("the path: "+System.getProperty("user.dir")); CreateFile("C:/123.txt"); readFile("C:/AbsPath.txt"); writeFile2("C:/RelPath.txt","abcdfdfdfdfddde"); readFile("C:/AbsPath.txt"); copy(); } public static boolean CreateFile(String destFileName) throws IOException{ File file = new File(destFileName); if(file.exists()){ System.out.println("Fail! Here it is!"); return false; } file.createNewFile(); System.out.println("success!"); return true; } public static void readFile(String fileName)throws IOException{ File file = new File(fileName); BufferedReader reader = new BufferedReader(new FileReader(file)); String br=null; while((br=reader.readLine())!=null){ System.out.println(br); } reader.close(); } public static void writeFile2(String filePath,String str)throws IOException{ PrintWriter pw = new PrintWriter(new FileOutputStream(filePath)); pw.print(str); pw.close(); } public void writeFile(String filePath,StringBuffer text)throws IOException{ BufferedWriter rw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath))); rw.write(new String(text)); rw.close(); } public static void copy_1() throws IOException { FileReader fr=new FileReader("FileReaderDemo2.java"); FileWriter fw=new FileWriter("FileReaderDemo2.txt"); int ch=0; while((ch=fr.read())!=-1)//循环读取,每次读取一个字符,到达文件尾则返回-1 { fw.write(ch);//取从第一个到第num个字符,避免有数据不足3个字符的情况 } fw.close(); fr.close(); } public static void copy() throws IOException { FileReader fr=new FileReader("C:/AbsPath.txt"); FileWriter fw=new FileWriter("C:/out.txt"); char[] buf=new char[1024]; int num=0; while((num=fr.read(buf))!=-1) { fw.write(buf,0,num); } fr.close(); fw.close(); } }