public class InputStreamTest { public static void main(String[] args) { InputStreamTest inputstream = new InputStreamTest(); FileInputStreamExample example = inputstream.new FileInputStreamExample("D:\\istream\\rechargeLarge.txt", "D:\\istream\\rechargeCopy.txt"); long start = System.currentTimeMillis(); // example.copy(); example.copyReader(); // example.copyNio(); System.out.println(System.currentTimeMillis() - start); } public class FileInputStreamExample { String inputPath; String outputPath; public FileInputStreamExample(String inputPath, String outputPath) { this.inputPath = inputPath; this.outputPath = outputPath; } public FileInputStreamExample(String inputPath) { this(inputPath, null); } public void copy() { FileInputStream inputStream = null; BufferedInputStream bufferInputStream = null; FileOutputStream outputStream = null; BufferedOutputStream bufferOutputStream = null; try { inputStream = new FileInputStream(inputPath); bufferInputStream = new BufferedInputStream(inputStream); outputStream = new FileOutputStream(outputPath); bufferOutputStream = new BufferedOutputStream(outputStream); int n = 0; byte[] but = new byte[1024]; while((n = bufferInputStream.read(but)) != -1) { //这里注意读到多少写多少 bufferOutputStream.write(but, 0, n); } bufferOutputStream.flush(); // System.out.println("再一次读" + inputStream.read()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { bufferInputStream.close(); inputStream.close(); bufferOutputStream.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } public void copyReader() { FileReader fileReader = null; BufferedReader bufferedReader = null; FileWriter fileWriter = null; BufferedWriter bufferedWriter = null; try { fileReader = new FileReader(inputPath); bufferedReader = new BufferedReader(fileReader); fileWriter = new FileWriter(outputPath); bufferedWriter = new BufferedWriter(fileWriter); int n = 0; char[] buf = new char[1024]; while ((n = bufferedReader.read(buf)) != -1) { bufferedWriter.write(buf, 0, n); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); fileReader.close(); bufferedWriter.close(); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } public void copyNio() { FileInputStream inputStream = null; FileOutputStream outputStream = null; FileChannel inputChannel = null; FileChannel outputChannel = null; try { inputStream = new FileInputStream(inputPath); outputStream = new FileOutputStream(outputPath); inputChannel = inputStream.getChannel(); outputChannel = outputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while(inputChannel.read(buffer) != -1) { buffer.flip(); outputChannel.write(buffer); buffer.clear(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); outputStream.close(); inputChannel.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
在windows7平台下,做了三种IO例子的简单性能比较。通过对比发现,对于文件IO,NIO并不比流快,速度受缓冲区大小的影响,在同样缓冲区大小的条件下,按字节读写流的速度反而比NIO的速度更快。对于字符流,由于其构建在字节流的基础上,需要处理编码信息,其读写速度要比字节流慢。
相关推荐
- 提供了方便的文本扫描功能,可以逐行或逐词读取文件,适合简单文本输入。 6. **RandomAccessFile** - 支持随机访问文件,适用于大数据文件的分块读写。 7. **NIO(非阻塞I/O)** - Java 1.4引入的NIO库,提供...
Java 7引入的`java.nio.file.Files`类提供了一些方便的静态方法,可以直接读取文件内容: ```java String content = Files.readString(Paths.get("example.txt")); ``` 5. **流的关闭**: 完成文件操作后,...
- `FileInputStream`:这个类用于读取文件中的原始字节。例如,我们可以通过创建一个`FileInputStream`对象并传入文件路径来打开文件,然后使用`read()`方法读取字节。 - `FileOutputStream`:与`FileInputStream`...
Java文件读写IO/NIO及性能比较详细代码及总结 Java文件读写是Java编程中一个非常重要的方面,在各种项目中都需要对文件进行读写操作。Java提供了多种方式来实现文件读写,包括字节读写、字符读取、行读取等。以下是...
3. 读取文件:使用`read()`系统调用,传入文件描述符、缓冲区地址和要读取的字节数。 4. 写入文件:使用`write()`系统调用,传入文件描述符、缓冲区地址和要写入的字节数。 5. 关闭文件:完成操作后,使用`close()`...
Java IO流是Java平台中用于处理输入和输出的重要机制,它允许程序与各种数据源(如文件、网络连接、内存缓冲区等)进行交互。在Java中,IO流分为两大类:字节流(Byte Stream)和字符流(Character Stream)。字节流...
4. **适用场景**:标准IO适合文本处理和简单文件操作,而文件IO更适合底层系统编程或高效IO操作。 为了更好地理解和对比这两种IO方式,可以参考以下资源: - **标准IO跟文件IO区别.pdf**:这个文档可能详细分析了...
本文将深入探讨三种不同的读取方式,并进行性能比较:二进制数据读取、字符数据读取以及压缩文件的读取。这三种方式在处理不同类型的数据时各有优劣,了解它们的特性可以帮助我们更有效地优化代码。 1. 二进制数据...
本压缩包提供的"WinCE磁盘IO读写性能测试工具"旨在帮助开发者评估系统在读取大文件时的性能表现。工具的核心功能包括从SD卡选择一个大文件,然后通过启动一个独立的线程来执行读取操作。在读取过程中,工具会实时...
- **字节流**:使用`FileInputStream`读取文件。以下是一个简单的示例: ```java FileInputStream fis = new FileInputStream("source.txt"); byte[] buffer = new byte[1024]; int bytesRead; while (...
在读取文件时,可以使用FileInputStream的read()方法逐个读取字节,写入文件时,使用FileOutputStream的write()方法将字节写入。 2. 字符流:Reader和Writer是所有字符输入流和输出流的基类。对于文本文件操作,...
本话题主要探讨在Linux环境下,针对文件逆序操作的不同I/O策略的性能比较。我们将分析四种不同的实现方式:`creat_text_file.c`、`mmap.c`、`lio.c`和`cio.c`。 首先,`creat_text_file.c`可能是通过标准的文件打开...
例如,使用字符流读取文件内容: ```java File inputFile = new File("input.txt"); try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) { String line; while ((line = br.readLine...
- 读取文件: ```java try { FileInputStream fis = new FileInputStream("path_to_file"); BufferedInputStream bis = new BufferedInputStream(fis); // 读取数据 byte[] buffer = new byte[1024]; int ...
### Java IO学习基础之读写文本文件 #### 一、Java IO概述 Java IO(Input/Output)是Java中处理输入输出操作的核心包,它主要提供了文件读写、网络通信等基本功能。Java IO操作主要基于流的概念进行,分为字节流...
在Java中,`java.io`包提供了许多用于读写文件的类,例如`FileInputStream`和`FileOutputStream`分别用于读取和写入文件,而`BufferedInputStream`和`BufferedOutputStream`则可以提高读写效率。在文件上传的场景下...
`StreamReader`的`ReadLine`方法会逐行读取文件,直到文件末尾。如果文件内容需要存储在其他数据结构中,例如List,你可以轻松替换ArrayList以提高代码的类型安全性。此外,使用`using`语句可确保`StreamReader`在...
3. 读写操作:使用循环读取源文件的字节,并写入到目标文件中。 4. 关闭流:操作完成后,记得关闭所有打开的流,释放系统资源。 三、字节流复制文件 以下是一个使用字节流进行文件复制的简单示例: ```java import...
例如,在需要高性能的IO操作时,可以选择使用NIO来实现高性能的IO操作,而在需要简单的IO操作时,可以选择使用基本的InputStream和OutputStream来实现文件复制。 Java中的IO API和编程方式非常多样化,需要根据实际...
### 探寻C++最快的读取文件的方案:C++ IO优化 在计算机编程领域,尤其是在需要处理大量数据的应用场景中,文件的读取速度往往成为制约程序性能的关键因素之一。本文将通过一系列实验对比不同读取方法的速度,旨在...