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

使用Java文件IO函数读写数据

发布时间:2023-06-23 23:05:54

Java文件IO函数是处理文件和IO操作的基本工具,Java提供了许多类和函数来处理文件和IO操作。使用Java文件IO函数进行数据的读写操作是很简单的。本文将简单介绍Java文件IO函数常用的读写操作。

Java文件IO函数常用的读写数据操作有:读取字符和字符串、读取二进制数据、写入字符和字符串、写入二进制数据等。

一、读取字符和字符串

Java提供了 Reader 类来读取字符和字符串,其主要方法有:read(),read(char[] cbuf) 和 read(char[] cbuf, int off, int len) 方法。read() 方法每次读取一个字符,返回一个 char 类型的值,如果已经读取到文件末尾,则返回-1。read(char[] cbuf) 方法可以一次读取多个字符,返回读取到的字符个数。read(char[] cbuf, int off, int len) 方法可以从偏移量 off 处读取 len 个字符。

代码示例:

File file = new File("test.txt");
Reader reader = null;

try {
    reader = new FileReader(file);
    char[] cbuf = new char[1024];
    int length = 0;
    while ((length = reader.read(cbuf)) != -1) {
        System.out.println(new String(cbuf, 0, length));
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (reader != null) {
            reader.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

二、读取二进制数据

Java提供了 InputStream 类来读取二进制数据,其主要方法有:read(),read(byte[] b) 和 read(byte[] b, int off, int len) 方法。read() 方法每次读取一个 byte 类型的数据,返回读取到的数据,如果已经读取到文件末尾,则返回-1。read(byte[] b) 方法可以一次读取多个字节,返回读取到的字节个数。read(byte[] b, int off, int len) 方法可以从偏移量 off 处读取 len 个字节。

代码示例:

File file = new File("test.bin");
InputStream inputStream = null;

try {
    inputStream = new FileInputStream(file);
    byte[] buffer = new byte[1024];
    int length = 0;
    while ((length = inputStream.read(buffer)) != -1) {
        for (int i = 0; i < length; i++) {
            System.out.print(buffer[i] + " ");
        }
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (inputStream != null) {
            inputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

三、写入字符和字符串

Java提供了 Writer 类来写入字符和字符串,其主要方法有:write(int c), write(char[] cbuf) 和 write(char[] cbuf, int off, int len) 方法。write(int c) 方法会将一个字符写入到文件中。write(char[] cbuf) 方法可以一次写入多个字符。write(char[] cbuf, int off, int len) 方法可以从偏移量 off 处写入 len 个字符。

代码示例:

File file = new File("test.txt");
Writer writer = null;

try {
    writer = new FileWriter(file);
    String content = "Hello, World";
    writer.write(content);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (writer != null) {
            writer.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

四、写入二进制数据

Java提供了 OutputStream 类来写入二进制数据,其主要方法有:write(int b), write(byte[] b) 和 write(byte[] b, int off, int len) 方法。write(int b) 方法会将一个字节写入到文件中。write(byte[] b) 方法可以一次写入多个字节。write(byte[] b, int off, int len) 方法可以从偏移量 off 处写入 len 个字节。

代码示例:

File file = new File("test.bin");
OutputStream outputStream = null;

try {
    outputStream = new FileOutputStream(file);
    byte[] buffer = {1, 2, 3, 4, 5};
    outputStream.write(buffer);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (outputStream != null) {
            outputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

总结:

Java文件IO函数是处理文件和IO操作的基本工具,Java提供了许多类和函数来处理文件和IO操作。使用Java文件IO函数进行数据的读写操作是很简单的。通过上述的示例代码可以看出,Java文件IO函数的使用方式十分简单,开发者可以根据不同的需求来选择不同的文件IO函数进行读写操作。