文件输出流,用于向文件中写入数据。
import java.io.*; public class FileOutputStreamExample { // 堆代码 duidaima.com public static void main(String[] args) { // 要写入的文件路径和名称 String filePath = "C:/example/output.txt"; // 要写入文件的内容 String content = "Hello, World!"; // 创建输出流对象 FileOutputStream fos = null; try { fos = new FileOutputStream(filePath); // 将字符串转换为字节数组,并将其写入文件 fos.write(content.getBytes("UTF-8")); // 刷新输出流 fos.flush(); // 输出提示信息 System.out.println("Content has been written to " + filePath); } catch (FileNotFoundException e) { System.out.println("File not found: " + filePath); } catch (IOException e) { System.out.println("Error writing file: " + e.getMessage()); } finally { // 关闭输出流 try { if (fos != null) { fos.close(); } } catch (IOException e) { System.out.println("Error closing file: " + e.getMessage()); } } } }示例代码说明:
注意:在使用FileOutputStream类时,我们需要确保文件存在,并且我们有写入文件的权限。此外,在实际应用中,可能需要使用更高效的方法来写入大型文件,以避免IO开销的问题。
import java.io.*; public class ByteArrayOutputStreamExample { public static void main(String[] args) { // 创建字节数组输出流对象 ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { // 将字符串转换为字节数组,并写入到字节数组输出流中 baos.write("Hello, World!".getBytes("UTF-8")); // 将字节数组输出流中的数据转换为字节数组 byte[] bytes = baos.toByteArray(); // 将字节数组转换为字符串,并输出到控制台 String content = new String(bytes, "UTF-8"); System.out.println(content); } catch (IOException e) { System.out.println("Error writing to byte array: " + e.getMessage()); } finally { // 关闭字节数组输出流 try { if (baos != null) { baos.close(); } } catch (IOException e) { System.out.println("Error closing byte array output stream: " + e.getMessage()); } } } }示例代码说明:
注意:在使用ByteArrayOutputStream类时,需要注意内存占用问题。BytesArrayOutputStream类主要用于在内存中临时存储数据。对于大数据,可能需要使用其他方式存储。
import java.io.*; public class PipedOutputStreamExample { public static void main(String[] args) { // 创建一对PipedInputStream和PipedOutputStream PipedInputStream input = new PipedInputStream(); PipedOutputStream output = new PipedOutputStream(); try { // 将输入流和输出流连接起来 input.connect(output); // 创建一个写线程 Thread writerThread = new Thread(new Runnable() { @Override public void run() { try { // 写入一些数据到PipedOutputStream output.write("Hello, World!".getBytes("UTF-8")); // 刷新PipedOutputStream output.flush(); // 关闭PipedOutputStream output.close(); } catch (IOException e) { System.out.println("Error writing to pipe: " + e.getMessage()); } } }); // 创建一个读线程 Thread readerThread = new Thread(new Runnable() { @Override public void run() { try { // 读取PipedInputStream中的数据 byte[] buffer = new byte[1024]; int len = input.read(buffer); // 将读取的字节转换为字符串,并输出到控制台 String content = new String(buffer, 0, len, "UTF-8"); System.out.println(content); // 关闭PipedInputStream input.close(); } catch (IOException e) { System.out.println("Error reading from pipe: " + e.getMessage()); } } }); // 启动写线程和读线程 writerThread.start(); readerThread.start(); // 等待写线程和读线程完成 writerThread.join(); readerThread.join(); } catch (IOException | InterruptedException e) { System.out.println("Error communicating between threads: " + e.getMessage()); } } }示例代码说明:
最后,我们启动写线程和读线程,并等待它们完成。