java IO常用类和方法

来源:互联网 发布:结婚网络请柬 编辑:程序博客网 时间:2024/06/02 03:47

File类

构造函数:
String filename = "file_path";File file = new File(filename);
常用方法:
  • exists() 判断文件是否存在
  • createNewFile() 建立File对象对应的路径的文件
  • isFile() 判断File对象对应的路径是不是一个文件
  • delete() 删除文件
  • mkdirs() 建立多层目录
File fp = new File("test.txt");if(fp.exists()){    System.out.println("yes");}else{    System.out.println("no");}boolean flag = fp.createNewFile();if(flag){    System.out.println("create");}else{    System.out.println("fail");}if(fp.isFile()){    System.out.println("is a file");}else{    System.out.println("not a file");}File dir = new File("./one/two/three");flag = dir.mkdirs();if(flag){    System.out.println("make dirs");}else {    System.out.println("file to make");}


文件读写

  • FileInputStream 字节输入流
  • FileOutputStream 字节输出流
  • FileReader 字符输入流
  • FileWriter 字符输出流

前边两个是处理字节的,意味着只能处理英文字母和标点。
后边两个是处理字符的,可以处理unicode字符。

class MyFileStream{    private FileInputStream in = null;    private FileOutputStream out = null;    void write(String filename, String txt){        try {            out = new FileOutputStream(filename);            for(int i=0; i<txt.length(); ++i){                out.write(txt.charAt(i));            }        }catch (IOException e){            e.printStackTrace();        } finally {            if(out != null){                try {out.close();}                catch (IOException e){e.printStackTrace();}            }        }    }    String read(String filename){        int c;        String ret = "";        try {            in = new FileInputStream(filename);            while((c=in.read()) != -1){                System.out.println((char)c);                ret += (char)c;            }        }catch (IOException e){            e.printStackTrace();        }finally {            if(in != null){               catch (IOException e){e.printStackTrace();}            }        }        return ret;    }    void copy(String file1, String file2){        try{            in = new FileInputStream(file1);            out = new FileOutputStream(file2);            int c;            while((c=in.read()) != -1){                out.write(c);            }        }catch (IOException e){            e.printStackTrace();        } finally {            if(in != null){                try {in.close();}                catch (IOException e){e.printStackTrace();}            }            if(out != null){               catch (IOException e){e.printStackTrace();}            }        }    }}public static void main(String args[]) throws IOException{    String f1 = "in.txt";    String f2 = "out.txt";    String t1 = "goodmorning, motherfucker!";    MyFileStream o1 = new MyFileStream();    o1.write(f1, t1);    System.out.println(o1.read(f1));    o1.copy(f1, f2);}
class MyFileReader{    private FileReader in = null;    private FileWriter out = null;    void write(String filename, String txt){        try {            out = new FileWriter(filename);            for(int i=0; i<txt.length(); ++i){                out.write(txt.charAt(i));            }        }catch (IOException e){            e.printStackTrace();        } finally {            if(out != null){                try {out.close();}                catch (IOException e){e.printStackTrace();}            }        }    }    String read(String filename){        int c;        String ret = "";        try {            in = new FileReader(filename);            while((c=in.read()) != -1){                ret += (char)c;            }        }catch (IOException e){            e.printStackTrace();        }finally {            if(in != null){                catch (IOException e){e.printStackTrace();}            }        }        return ret;    }    void copy(String file1, String file2){        try{            in = new FileReader(file1);            out = new FileWriter(file2);            int c;            while((c=in.read()) != -1){                out.write(c);            }        }catch (IOException e){            e.printStackTrace();        } finally {            if(in != null){                try {in.close();}                catch (IOException e){e.printStackTrace();}            }            if(out != null){                catch (IOException e){e.printStackTrace();}            }        }    }}public static void main(String args[]) throws IOException{    String f1 = "in.txt";    String f2 = "out.txt";    String t2 = "苟利国家生死以,岂因祸福趋避之。";    MyFileReader o2 = new MyFileReader();    o2.write(f1, t2);    System.out.println(o2.read(f1));    o2.copy(f1, f2);}
  1. 值得注意的是read()返回一个int, 需要强制类型转换为byte或者char(在字节流中可以是byte也可以是char,但在字符流中一定要有char)。

  2. new对象时要注意处理异常抛出。

  3. FileOutputStream.write()只能写byte和int。

  4. FileWriter.write()能写char数组,int和String。


字符流和字节流的转换

  • InputStreamReader
  • OutputStreamReader

构造函数:

InputStreamReader cin = new InputStreamReader(new FileInputStream(f1));OutputStreamWriter cout = new OutputStreamWriter(new FileOutputStream(f1));

构造函数的参数为FileInputStream实例,即一个字节流实例。



通常情况下,我们将InputStreamReader作为参数,构造一个BufferedReader

InputStreamReader cin = new InputStreamReader(new FileInputStream(f1));        BufferedReader bfr = new BufferedReader(cin);        String line;        while((line = bfr.readLine()) != null){            System.out.println(line);        }