Java_文件操作

2019-08-15  本文已影响0人  书虫大王X
1.知识点:
2.知识点的运用:

1.I/O对象:不属于内存对象,创建后需要自己关闭。
2.创建文件:

        //创建文件完整路径
        String path = "文件目录";
        //拼接目录
        File file = new File(path.concat("\\1.txt"));

        //判断文件是否存在
        if (file.exists() == false){
           try {
               file.createNewFile();
               //异常处理
           }catch (IOException e){
               System.out.println("IO异常了");
           }
        }

3.创建目录:
mkdirs()可以建立多级文件夹, mkdir()只会建立一级的文件夹, 如下:

new File("/tmp/one/two/three").mkdirs();
//执行后, 会建立tmp/one/two/three四级目录
new File("/tmp/one/two/three").mkdir();
//则不会建立任何目录, 因为找不到/tmp/one/two目录, 结果返回false
// throws IOException :抛出异常
public void createDirectory() throws IOException {
        File f = new File("文件目录");
        f.mkdir();
    }

4.字节流与字符流的区别:

5.字节流:

//例1:
FileInputStream fs = new FileInputStream("文件路径");
byte[ ] name = new byte[12];
int count = fs.read(name);
fs.close();
  //例2:
FileInputStream fs = new FileInputStream("文件路径");
long len = 0;
while((len = fs.read()) != -1) {
    char c = (char) len;
    System.out.print(c);
}
fs.close();
    public void class() throws IOException {
        FileOutputStream fos = new FileOutputStream("文件路径");
        fos.write("123".getBytes());
        byte[ ] text = {'1','2','3'};
        fos.write(text);
        fos.close();
    }
        FileInputStream fis = new FileInputStream("文件路径");
        BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] b = new byte[1024];
        while(bis.read(b) != -1) {
            System.out.println(new String(b));
        }
        bis.close();
        fis.close();
    public void class() throws IOException {
        FileOutputStream fos = new FileOutputStream("文件路径");
        BufferedOutputStream bop = new BufferedOutputStream(fos);
        bop.write("123".getBytes());
        byte[ ] in = new byte[1024];
        while(bop.write(in) != -1){
               bop.write(in,0,bop.write(in));
        }
        bop.close();
        fos.close();
    }
public class MyClass {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("文件路径");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        person m = new person ();
        m.name = "小王";
        m.age = 20;
        oos.writeObject(m);
        oos.close();
        fos.close();  
    }
}

public class person implements Serializable {
    public String  name;
    public int age;
}
        FileInputStream fos = new FileInputStream("文件路径");
        ObjectInputStream oos = new ObjectInputStream(fos);
        person m = new person ();
        m = (person) oos.readObject();
        System.out.println(m);
        oos.close();
        fos.close();

7.字符流:

        FileReader fr = new FileReader("文件路径");
        int len = 0;
                //一个字符一个字符的读
        while((len = fr.read()) != -1) {
            System.out.print((char)len);
        }
        fr.close();
image.png
    public void class() throws Exception {
        FileWriter fw = new FileWriter("文件路径");
        fw.write("abc");
        fw.close();
    }
image.png
    public void class() throws Exception {
        FileReader fw = new FileReader("文件路径");
        BufferedReader br = new BufferedReader(fw);
        String str = null;
        while((str = br.readLine())!= null){
            System.out.println(str);
        }
        br.close();
        fw.close();
    }
FileWriter fw = new FileWriter("文件路径");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("java开发");
bw.close();
fw.close();
上一篇 下一篇

猜你喜欢

热点阅读