JAVA API 学习笔记——File&IO流

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、File:

Java文件类以抽象的方式代表文件名和目录路径名。该类主要用于文件和目录的创建、文件的查找和文件的删除等。

1. File(String pathname) :

通过将给定路径名字符串转换成抽象路径名来创建一个新 File 实例。

2. 常用方法:

方法描述
public String getParent()返回此抽象路径名的父路径名的路径名字符串,如果此路径名没有指定父目录,则返回 null。
public boolean isDirectory()测试此抽象路径名表示的文件是否是一个目录。
public boolean exists()测试此抽象路径名表示的文件或目录是否存在。
public long length()返回由此抽象路径名表示的文件的长度。
    @Test
    public void FileTest(){

        File file = new File("D:\\Project\\Backend\\Java\\java_idea\\Test\\src\\com\\geogle\\exception");
        System.out.println(file.isDirectory());
        System.out.println(file.exists());
        System.out.println(file.getParent());
        System.out.println(file.length());
输出:
true
true
D:\Project\Backend\Java\java_idea\Test\src\com\geogle
4096

    }

**3.String[ ] list()&File[ ] list(): **

public String[ ] list():
返回由此抽象路径名所表示的目录中的文件和目录的名称所组成字符串数组。

    @Test
    public void FileTest(){

        File dir = new File("D:\\Project\\Backend\\Java\\java_idea\\Test\\src\\com\\geogle\\exception");
        String[] dirList = dir.list();
        for (String Name:dirList
             ) {
            System.out.println(Name);
        }
    }
    
输出:
Application.java
BbCodeEnum.java
BbException.java
ErrorCode.java
Main.java

public File[ ] list():
返回一个抽象路径名数组,这些路径名表示此抽象路径名所表示目录中的文件。

    @Test
    public void FileTest(){

        File dir = new File("D:\\Project\\Backend\\Java\\java_idea\\Test\\src\\com\\geogle\\exception");
        File[] FileList = dir.listFiles();
        for (File fileList:FileList
             ) {
            //返回每个文件的绝对路径
            System.out.println(fileList.getAbsolutePath());
        }
    }
输出:
D:\Project\Backend\Java\java_idea\Test\src\com\geogle\exception\Application.java
D:\Project\Backend\Java\java_idea\Test\src\com\geogle\exception\BbCodeEnum.java
D:\Project\Backend\Java\java_idea\Test\src\com\geogle\exception\BbException.java
D:\Project\Backend\Java\java_idea\Test\src\com\geogle\exception\ErrorCode.java
D:\Project\Backend\Java\java_idea\Test\src\com\geogle\exception\Main.java

二、IO流:

1.Input&Output:

Input:
在这里插入图片描述

Output:

在这里插入图片描述

2.FileInputStream&FileOutputStream:

FileInputStream:

public void close() throws IOException{}:
关闭此文件输入流并释放与此流有关的所有系统资源。抛出IOException异常。

public int read(int r)throws IOException{}:
这个方法从 InputStream 对象读取指定字节的数据。返回为整数值。返回下一字节数据,如果已经到结尾则返回-1。

public int read(byte[] r) throws IOException{}:
这个方法从输入流读取r.length长度的字节。返回读取的字节数。如果是文件结尾则返回-1。

FileOutputStream:

public void close() throws IOException{}:
关闭此文件输入流并释放与此流有关的所有系统资源。抛出IOException异常。

public void write(int w)throws IOException{}
这个方法把指定的字节写到输出流中。

public void write(byte[] w){}:
把指定数组中w.length长度的字节写到OutputStream中。

代码如下:

package com.geogle.IO;

import org.junit.Test;

import java.io.*;
import java.util.Arrays;

public class TestIO {
    @Test
    public void inputTest() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("iofile/kimodi.txt");
        int by;
        while((by = fileInputStream.read())!=-1){
            System.out.println((char)by);

        }
        fileInputStream.close();
}

    @Test
    public void outputTest() throws IOException{
        FileOutputStream fileOutputStream =new FileOutputStream("iofile/memeda.txt");
        byte[] bytes = "kemodi".getBytes();
        for(int x = 0;x< bytes.length;x++){
            fileOutputStream.write(bytes[x]);
        }
        fileOutputStream.close();

    }
    @Test
    public void copyFileBase()throws IOException{
        FileInputStream fileInputStream = new FileInputStream("iofile/io-ins.gif");
        FileOutputStream fileOutputStream = new FileOutputStream("iofile/copy.gif");

        int by = 0;
        byte[] buff = new byte[1024];
        while((by = fileInputStream.read(buff)) != -1){
            fileOutputStream.write(buff,0,by);
        }
        fileInputStream.close();
        fileOutputStream.close();

    }

}

3.BufferedInputStream&BufferedOutputStream:

BufferedInputStream和BufferedOutputStream方法解决了输出输入的速度问题,数据块状流动,加快数据传输;

@Test
    public void copyFileBuffer()throws IOException{
        FileInputStream fileInputStream = new FileInputStream("iofile/kimodi.txt");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("iofile/copykimodi.txt"));

        int by;
        while((by = bufferedInputStream.read())!= -1){
            System.out.print((char)by);
            bufferedOutputStream.write(by);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }

三、Reader&Writer:

FileReader类从InputStreamReader类继承而来。该类按字符读取流中数据。可以通过以下几种构造方法创建需要的对象。

在给定从中读取数据的 File 的情况下创建一个新 FileReader。

FileReader&FileWriter:


    @Test
    public void testFileReader() throws IOException {
        FileReader fileReader = new FileReader("iofile/kimodi.txt");
        int ch;
        while((ch = fileReader.read())!=-1){
            System.out.print((char)ch);
        }
        fileReader.close();
    }
    @Test
    public void testFileWriter() throws IOException {
        FileWriter fileWriter = new FileWriter("iofile/1.txt");
        fileWriter.write("hello kimodi");
        fileWriter.close();
    }

BufferedReader&BufferedWriter(即升级版):

@Test
    public void testBufferedWriter() throws IOException {
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("iofile/2.txt"));
    bufferedWriter.write("1234");
    bufferedWriter.newLine();
    bufferedWriter.write("5678");
    bufferedWriter.close();
    }
    @Test
    public void testBufferedReader() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("iofile/2.txt"));
        String str;
        while((str = bufferedReader.readLine())!=null){
            System.out.println(str);
        }
        bufferedReader.close();
    }

四、Common:

实际开发过程使用的IO流方式:

package com.geogle.IO;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;


public class TestCommons {
    @Test
    public void testCommons() throws IOException {
        File file = new File("iofile/3.txt");
        if(!file.exists()){
            file.createNewFile();
        }
       FileUtils.writeLines(file,new ArrayList(Collections.singleton("")),true);
    }
}

利用动态数组ArrayList:

package com.geogle.IO;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.sql.SQLClientInfoException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;


public class TestCommons {

    @Test
    public void testCommons() throws IOException {
        File file = new File("iofile/3.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        ArrayList<Student> arrayList = new ArrayList<>();
        arrayList.add(new Student("doinb","5117"));
       FileUtils.writeLines(file,arrayList,true);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值