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

Java函数中如何操作文件读写?

发布时间:2023-06-07 08:30:13

Java是一种非常强大的编程语言,它具有读写文件的特点。在Java中,我们可以使用多种方式进行文件的读写操作,这些方式包括使用Java IO、NIO、文件通道等。接下来,我们将详细介绍Java中文件的读写操作。

Java IO

Java IO包括了读取和写入文件,以及读取和写入其他输入/输出设备的类。其中,最常用的类是FileInputStream、FileOutputStream、BufferedReader和BufferedWriter。

1.读文件

使用Java IO读取文件的方法很简单,只需要使用FileInputStream类,然后通过它的方法读取数据即可。例如:

FileInputStream fis = new FileInputStream("file.txt");

byte[] buffer = new byte[1024];

int len = 0;

while ((len = fis.read(buffer)) != -1) {

    System.out.println(new String(buffer, 0, len));

}

fis.close();

以上代码将打开一个名为“file.txt”的文件并读取其内容。通过使用FileInputStream的read()方法,我们可以将文件的内容读取到byte数组buffer中,并使用System.out.println()打印它们。

2.写文件

使用Java IO写入文件的方法也非常简单,只需要使用FileOutputStream类,然后通过它的方法写入数据即可。例如:

FileOutputStream fos = new FileOutputStream("file.txt");

String text = "Hello, World!";

byte[] buffer = text.getBytes();

fos.write(buffer);

fos.close();

以上代码将打开一个名为“file.txt”的文件并将文本“Hello, World!”写入该文件。通过使用FileOutputStream的write()方法,我们可以向文件中写入数据。

Java NIO

Java NIO提供了更快、更灵活、更强大的文件读写功能。NIO是一个面向缓冲区、基于通道和选择器的新IO API。使用NIO读写文件可以使用ByteBuffer类和FileChannel类。

1.读文件

使用Java NIO读取文件的方法也相当简单,只需创建一个FileInputStream对象,然后获取它的通道,并为通道创建一个ByteBuffer对象。例如:

FileInputStream fis = new FileInputStream("file.txt");

FileChannel channel = fis.getChannel();

ByteBuffer buffer = ByteBuffer.allocate(1024);

while (channel.read(buffer) != -1) {

    buffer.flip();

    while (buffer.hasRemaining()) {

        byte b = buffer.get();

        System.out.println((char) b);

    }

    buffer.clear();

}

fis.close();

以上代码将打开一个名为“file.txt”的文件并读取其内容。通过使用FileInputStream的getChannel()方法,我们可以获取该文件的通道,并通过使用ByteBuffer来读取和处理数据。

2.写文件

使用Java NIO写文件也相当直接,只需创建一个FileOutputStream对象,获取其通道,并为通道创建一个ByteBuffer对象。例如:

FileOutputStream fos = new FileOutputStream("file.txt");

FileChannel channel = fos.getChannel();

String s = "Hello, World!";

ByteBuffer buffer = ByteBuffer.allocate(s.length());

buffer.put(s.getBytes());

buffer.flip();

channel.write(buffer);

fos.close();

以上代码将打开一个名为“file.txt”的文件并以原本的文件内容写入新的字符串“Hello, World!”。通过使用ByteBuffer填充数据,并使用FileChannel类的write()方法将数据写入文件。

Java文件通道

Java NIO提供了许多新的类,其中最重要的是Java NIO2的Path和Files类。使用这些类可以更轻松、更方便地操作文件和目录。

1.读文件

使用Java文件通道读取文件也非常简单,只需获取FileInputStream的通道即可。例如:

FileInputStream fis = new FileInputStream("file.txt");

FileChannel channel = fis.getChannel();

ByteBuffer buffer = ByteBuffer.allocate(1024);

while (channel.read(buffer) != -1) {

    buffer.flip();

    while (buffer.hasRemaining()) {

        byte b = buffer.get();

        System.out.println((char) b);

    }

    buffer.clear();

}

fis.close();

2.写文件

写文件类似于使用Java IO和Java NIO。只需创建一个通道,将文本或数据写入ByteBuffer,然后写入通道即可。例如:

FileOutputStream fos = new FileOutputStream("file.txt");

FileChannel channel = fos.getChannel();

String s = "Hello, World!";

ByteBuffer buffer = ByteBuffer.allocate(s.length());

buffer.put(s.getBytes());

buffer.flip();

channel.write(buffer);

fos.close();

当然,以上代码只是文件读写的简单示例。实际应用中,我们可能需要根据具体的业务需求实现复杂的读写操作。不过,这些示例已经很详细地介绍了Java中文件读写的基本操作。