欢迎访问宙启技术站
智能推送

实现Java函数实现文件的读写操作

发布时间:2023-06-12 10:51:23

Java是一种面向对象的编程语言,具有很好的可移植性、可扩展性、安全性、可靠性等特点。文件的读写操作是Java编程中常用的操作,Java提供了一组丰富的API供我们进行文件操作,包括读写文件、创建文件、删除文件等基础操作。本文将介绍如何使用Java编写读写文件的代码。

一、读取文件

Java中读取文件可以使用FileInputStream、BufferedInputStream、Scanner等类实现。

1. FileInputStream类:

FileInputStream类用来打开一个文件并读取文件的数据。

示例代码:

public void readFile(String filePath) {
    FileInputStream fileInputStream = null;

    try {
        // 创建FileInputStream对象
        fileInputStream = new FileInputStream(filePath);

        int len = 0;
        byte[] buffer = new byte[1024];

        // 读取文件中的数据
        while ((len = fileInputStream.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, len));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileInputStream != null) {
            try {
                // 关闭文件
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

上述代码中,首先创建FileInputStream对象,在读取文件中的数据之后,使用了try-with-resource语法糖来自动关闭文件,代码可简化如下:

public void readFile(String filePath) {
    try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
        int len = 0;
        byte[] buffer = new byte[1024];

        while ((len = fileInputStream.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, len));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2. BufferedInputStream类

BufferedInputStream类是FileInputStream类的装饰器,它提供了一个缓冲区来提高文件读取的效率。

示例代码:

public void readFile(String filePath) {
    BufferedInputStream bufferedInputStream = null;

    try {
        // 创建BufferedInputStream对象
        bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));

        int len = 0;
        byte[] buffer = new byte[1024];

        // 读取文件中的数据
        while ((len = bufferedInputStream.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, len));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bufferedInputStream != null) {
            try {
                // 关闭文件
                bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

同样,也可以使用try-with-resource语法糖来自动关闭文件,代码如下:

public void readFile(String filePath) {
    try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath))) {
        int len = 0;
        byte[] buffer = new byte[1024];

        while ((len = bufferedInputStream.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, len));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

3. Scanner类

Scanner类是Java中一个非常实用的类,可以用来读取文件中的数据。

示例代码:

public void readFile(String filePath) {
    Scanner scanner = null;

    try {
        // 创建Scanner对象
        scanner = new Scanner(new File(filePath));

        // 读取文件中的数据
        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (scanner != null) {
            // 关闭文件
            scanner.close();
        }
    }
}

同样,也可以使用try-with-resource语法糖来自动关闭文件,代码如下:

public void readFile(String filePath) {
    try (Scanner scanner = new Scanner(new File(filePath))) {
        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

二、写入文件

Java中写入文件同样可以使用FileOutputStream、BufferedOutputStream、PrintWriter等类实现。

1. FileOutputStream类

FileOutputStream类用来打开一个文件并将数据写入这个文件。

示例代码:

public void writeFile(String filePath, String content) {
    FileOutputStream fileOutputStream = null;

    try {
        // 创建FileOutputStream对象
        fileOutputStream = new FileOutputStream(filePath);

        // 将数据写入文件
        fileOutputStream.write(content.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileOutputStream != null) {
            try {
                // 关闭文件
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

同样,也可以使用try-with-resource语法糖来自动关闭文件,代码如下:

public void writeFile(String filePath, String content) {
    try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
        fileOutputStream.write(content.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2. BufferedOutputStream类

BufferedOutputStream类是FileOutputStream类的装饰器,它提供了一个缓冲区来提高文件写入的效率。

示例代码:

public void writeFile(String filePath, String content) {
    BufferedOutputStream bufferedOutputStream = null;

    try {
        // 创建BufferedOutputStream对象
        bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath));

        // 将数据写入文件
        bufferedOutputStream.write(content.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bufferedOutputStream != null) {
            try {
                // 关闭文件
                bufferedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

同样,也可以使用try-with-resource语法糖来自动关闭文件,代码如下:

public void writeFile(String filePath, String content) {
    try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath))) {
        bufferedOutputStream.write(content.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

3. PrintWriter类

PrintWriter类是Java中一个非常实用的类,可以用来将数据写入文件中。

示例代码:

public void writeFile(String filePath, String content) {
    PrintWriter printWriter = null;

    try {
        // 创建PrintWriter对象
        printWriter = new PrintWriter(filePath);

        // 将数据写入文件
        printWriter.write(content);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (printWriter != null) {
            // 关闭文件
            printWriter.close();
        }
    }
}

同样,也可以使用try-with-resource语法糖来自动关闭文件,代码如下:

public void writeFile(String filePath, String content) {
    try (PrintWriter printWriter = new PrintWriter(filePath)) {
        printWriter.write(content);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

三、结论

Java中读写文件操作是一个非常基础的操作。通过本文的介绍,可以发现Java提供了多种读写文件的类,并且使用起来非常灵活,可以根据具体需求选择使用不同的类实现文件操作。在使用时,需要注意文件打开和关闭的问题,以免造成系统资源的浪费或数据丢失等问题。