java文件及读写操作

来源:互联网 发布:中文安卓编程开创e4a 编辑:程序博客网 时间:2024/06/06 01:04
import java.io.File;import java.io.FilenameFilter;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.BufferedReader;import java.io.BufferedWriter;public class FileTest{public static void main(String[] args){/*String parentPath = "c:/1";File parent = new File(parentPath);String child = "abc.txt";File file = new File(parent, child);*//*String pathname = "c:/1/abc.txt";File file = new File(pathname);*///第三种构造函数/*String parent = "c:/1/";String child = "abc.txt";File file = new File(parent, child);if(file.exists() == false){file.createNewFile();}*///File file = new File("c:/1/");/*String[] list = file.list();for(String name : list){System.out.println(name);}*///File[] list = file.listFiles();//for(File f : list){//System.out.println(f.getName());//System.out.println(f.getPath());//System.out.println(f.getParent());//System.out.println(f.getParentFile().getName());//System.out.println(f.getParentFile().getPath());//}/*File file = new File("c:/1/abc.txt");file.delete();*//* File file = new File("c:" + File.separator + "1");String[] list = file.list(new FilenameFilter(){public boolean accept(File dir, String name){if(name.endsWith(".java123")){return true;}return false;}});String[] list1 = {};System.out.println(list1.length);for(String item : list){System.out.println(item);} *//*File file = new File("c:/2");System.out.println(file.isDirectory());if(file.isDirectory()){System.out.println("文件夹已存在");}else{file.mkdir();}*//*FileInputStream fs = null;try{fs = new FileInputStream("c:/1/abc.txt");//abc.txt必须存在,否则Exception in thread "main" java.io.FileNotFoundExceptionbyte[] bytes = new byte[1024];bytes[0] = '$';int realCount = 0;while((realCount = fs.read(bytes, 1, 1023)) != -1){String str = new String(bytes, 0, realCount);System.out.println(str);}}catch(Exception e){e.printStackTrace();}finally{try{fs.close();}catch(Exception e){e.printStackTrace();}}*//*FileOutputStream fo = null;try{String str = "关于南海问题,\r\n中国是有权抵制的。";fo = new FileOutputStream("c:/1/abc.txt");fo.write(str.getBytes());}catch(Exception e){e.printStackTrace();}finally{try{fo.close();}catch(Exception e){e.printStackTrace();}}*//*FileInputStream fis = null;FileOutputStream fos = null;try{fis = new FileInputStream("c:/1/K.jpg");fos = new FileOutputStream("c:/1/L.png");byte[] bytes = new byte[2018];int realCount = 0;while((realCount = fis.read(bytes)) != -1){fos.write(bytes, 0, realCount);fos.flush();}}catch(Exception e){e.printStackTrace();}finally{try{fos.close();fis.close();}catch(Exception e){e.printStackTrace();}}*//*FileReader fr = null;FileWriter fw = null;try{fr = new FileReader("c:/1/readme.txt");fw = new FileWriter("c:/1/abc.txt");char[] chars = new char[1024];int realCount = 0;while((realCount = fr.read(chars)) != -1){fw.write(chars, 0, realCount);fw.flush();}}catch(Exception e){e.printStackTrace();}finally{try{fr.close();fw.close();}catch(Exception e){e.printStackTrace();}}*///如果字符文件比较小,可以通过使用BufferedReader, BufferedWriter提高读写速度BufferedReader br = null;BufferedWriter bw = null;String str = null;try{br = new BufferedReader(new FileReader("c:/1/readme.txt"));bw = new BufferedWriter(new FileWriter("c:/1/abc.txt"));while((str = br.readLine()) != null){bw.write(str + "\r\n");bw.flush();}}catch(Exception e){e.printStackTrace();}finally{try{br.close();bw.close();}catch(Exception e){e.printStackTrace();}}}}

0 0