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

Java函数实现文件读取和写入的方法

发布时间:2023-06-19 16:37:48

Java是一种开发跨平台应用程序的高性能编程语言,具有安全性、可扩展性、可移植性、并发性和跨平台性等特点,广泛应用于各种领域的应用程序开发。

在Java中,文件读取和写入是常见的操作,常用于读取和处理文本文件、二进制文件、配置文件、日志文件等各种类型的文件。下面将介绍Java函数实现文件读取和写入的方法。

文件读取

Java实现文件读取的方法主要有以下几种:

方法一:使用Java原生IO流读取文件

Java提供了FileInputStream和FileReader两个类来读取文件内容。其中FileInputStream可以用来读取任何类型的文件,而FileReader仅限于文本文件。

示例代码:

// 使用FileInputStream读取文件内容
String filePath = "C:/test.txt"; //文件路径
FileInputStream fis = null;
try {
  fis = new FileInputStream(filePath);
  byte[] bytes = new byte[1024];
  int hasRead = 0;
  while ((hasRead = fis.read(bytes)) > 0) {
    System.out.println(new String(bytes, 0, hasRead));
  }
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
} finally {
  if (fis != null) {
    try {
      fis.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

// 使用FileReader读取文件内容
String filePath = "C:/test.txt"; //文件路径
FileReader fr = null;
try {
  fr = new FileReader(filePath);
  char[] chars = new char[1024];
  int hasRead = 0;
  while ((hasRead = fr.read(chars)) > 0) {
    System.out.println(new String(chars, 0, hasRead));
  }
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
} finally {
  if (fr != null) {
    try {
      fr.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

方法二:使用Java NIO读取文件

NIO(New Input/Output)是Java提供的一套新的IO API,与原生IO相比,可以提高数据的读写效率,且支持多路复用等高级操作。

示例代码:

// 使用Java NIO读取文件内容
String filePath = "C:/test.txt"; //文件路径
RandomAccessFile raf = null;
try {
  raf = new RandomAccessFile(filePath, "rw");
  FileChannel channel = raf.getChannel();
  ByteBuffer buffer = ByteBuffer.allocate(1024);
  while (channel.read(buffer) > 0) {
    buffer.flip();
    System.out.println(new String(buffer.array()));
    buffer.clear();
  }
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
} finally {
  if (raf != null) {
    try {
      raf.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

方法三:使用Java 8的Stream API读取文件

Java 8新增了Stream API,可以使用Files类的静态方法lines()来按行读取文件内容,并将读取的结果转换为Stream对象。

示例代码:

// 使用Java 8的Stream API读取文件内容
String filePath = "C:/test.txt"; //文件路径
try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
  lines.forEach(System.out::println);
} catch (IOException e) {
  e.printStackTrace();
}

文件写入

Java实现文件写入的方法主要有以下几种:

方法一:使用Java原生IO流写入文件

Java提供了FileOutputStream和FileWriter两个类来写入文件内容。其中FileOutputStream可以用来写入任何类型的文件,而FileWriter仅限于文本文件。

示例代码:

// 使用FileOutputStream写入文件内容
String filePath = "C:/test.txt"; //文件路径
FileOutputStream fos = null;
try {
  fos = new FileOutputStream(filePath);
  String content = "Hello, world!";
  fos.write(content.getBytes());
  fos.flush();
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
} finally {
  if (fos != null) {
    try {
      fos.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

// 使用FileWriter写入文件内容
String filePath = "C:/test.txt"; //文件路径
FileWriter fw = null;
try {
  fw = new FileWriter(filePath);
  String content = "Hello, world!";
  fw.write(content);
  fw.flush();
} catch (IOException e) {
  e.printStackTrace();
} finally {
  if (fw != null) {
    try {
      fw.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

方法二:使用Java NIO写入文件

使用Java NIO写入文件可以提高数据的读写效率。

示例代码:

// 使用Java NIO写入文件内容
String filePath = "C:/test.txt"; //文件路径
RandomAccessFile raf = null;
try {
  raf = new RandomAccessFile(filePath, "rw");
  FileChannel channel = raf.getChannel();
  String content = "Hello, world!";
  ByteBuffer buffer = ByteBuffer.wrap(content.getBytes());
  channel.write(buffer);
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
} finally {
  if (raf != null) {
    try {
      raf.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

方法三:使用Java 8的Stream API写入文件

Java 8的Stream API也可以用来写入文件。可以将要写入的内容转换为Stream对象,并使用Files类的静态方法write()将Stream对象写入文件。

示例代码:

// 使用Java 8的Stream API写入文件内容
String filePath = "C:/test.txt"; //文件路径
List<String> lines = Arrays.asList("Hello,", "world!");
try {
  Files.write(Paths.get(filePath), lines, StandardCharsets.UTF_8);
} catch (IOException e) {
  e.printStackTrace();
}

总结

通过上述示例代码,可以看出Java函数实现文件读取和写入的方法非常多样化,可以根据具体需求选择不同的方法。在编写文件读取和写入代码时,需要注意异常处理和资源的关闭。同时,还需要注意文件的编码格式,以免出现乱码问题。