file read and write use java

本文介绍了使用Java进行文件读写的几种常见方法,并通过实验对比了FileInputStream、BufferedOutputStream和FileWriter在不同数据量下的执行效率。
读文件: FileInputStream 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。创建一个新 FileDescriptor 对象来表示此文件连接。 InputStreamReader InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字 符。它使用的字符集可以由名称指定或显式给定,否则可能接受平台默认的字符集。 BufferedReader 从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。 可以指定缓冲区 的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。 StringBuffer 线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。虽然在任意时间点上 它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。 public static void main(String[] args) { //读取文件内容 File a = new File("C:/add2.txt"); if(a.exists()){ FileInputStream fi = new FileInputStream(a); InputStreamReader isr = new InputStreamReader(fi, "GBk"); BufferedReader bfin = new BufferedReader(isr); String rLine = ""; while((rLine = bfin.readLine())!=null){ System.out.println(rLine); } } } 写文件: 在 java写文件中,通常会使用FileOutputStream和FileWriter,FileWriter只能写文本文件。 FileOutputStream也经常结合BufferedOutputStream。因为实际应用中写文本文件的情况占了大多数。所以下面测试用不同的方式生成一个相同行数、大小相同的文件的三种不同方式。 import java.io.File; import java.io.FileOutputStream; import java.io.*; public class FileTest { public FileTest() { } public static void main(String[] args) { FileOutputStream out = null; FileOutputStream outSTr = null; BufferedOutputStream Buff=null; FileWriter fw = null; int count=1000;//写文件行数 try { out = new FileOutputStream(new File("C:/add.txt")); long begin = System.currentTimeMillis(); for (int i = 0; i < count; i++) { out.write("测试java 文件操作/r/n".getBytes()); } out.close(); long end = System.currentTimeMillis(); System.out.println("FileOutputStream执行耗时:" + (end - begin) + " 豪秒"); outSTr = new FileOutputStream(new File("C:/add0.txt")); Buff=new BufferedOutputStream(outSTr); long begin0 = System.currentTimeMillis(); for (int i = 0; i < count; i++) { Buff.write("测试java 文件操作/r/n".getBytes()); } Buff.flush(); Buff.close(); long end0 = System.currentTimeMillis(); System.out.println("BufferedOutputStream执行耗时:" + (end0 - begin0) + " 豪秒"); fw = new FileWriter("C:/add2.txt"); long begin3 = System.currentTimeMillis(); for (int i = 0; i < count; i++) { fw.write("测试java 文件操作/r/n"); } fw.close(); long end3 = System.currentTimeMillis(); System.out.println("FileWriter执行耗时:" + (end3 - begin3) + " 豪秒"); } catch (Exception e) { e.printStackTrace(); } finally { try { fw.close(); Buff.close(); outSTr.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } } 以下结果经过多次执行,取常出现的数据,由于只是简单比较,不做详细统计。 1.当count=1000的,即写文件1000行的时候,写出的文件大小为18.5KB: FileOutputStream执行耗时:46 豪秒 BufferedOutputStream执行耗时:31 豪秒 FileWriter执行耗时:15 豪秒 2.当count=10000的,即写文件10000行的时候,写出的文件大小为185KB: FileOutputStream执行耗时:188 豪秒 BufferedOutputStream执行耗时:32 豪秒 FileWriter执行耗时:16 豪秒 3.当count=100000的,即写文件100000行的时候,写出的文件大小为1856KB: FileOutputStream执行耗时:1266 豪秒 BufferedOutputStream执行耗时:125 豪秒 FileWriter执行耗时:93 豪秒 4.当count=1000000的,即写文件1000000行的时候,写出的文件大小为18555KB: FileOutputStream执行耗时:12063 豪秒 BufferedOutputStream执行耗时:1484 豪秒 FileWriter执行耗时:969 豪秒 由以上数据可以看到,如果不用缓冲流BufferedOutputStream,FileOutputStream写文件的鲁棒性是很不好的。当写 1000000行的文件的时候,FileOutputStream比FileWriter要慢11094毫秒(11 秒),BufferedOutputStream比FileWriter慢515毫秒。 不要小看这几秒的时间。当操作的数据量很大的时候,这点性能的差距就会很大了。在通用数据迁移工具导出数据库2千万条记录生成sql脚本文件的时候,性能性能相差10分钟以上。
To implement the file read and write function of Windows computers using Java, you can use the Java File API. Here's an example code snippet to read and write a file: ```java import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.Scanner; public class FileReadWriteExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter file path: "); String filePath = scanner.nextLine(); File file = new File(filePath); try { // Read file byte[] fileContent = Files.readAllBytes(file.toPath()); System.out.println("File content:\n" + new String(fileContent)); // Write file System.out.print("Enter new content: "); String newContent = scanner.nextLine(); Files.write(file.toPath(), newContent.getBytes()); System.out.println("File written successfully."); } catch (IOException e) { // Handle file access denied error if (e.getMessage().contains("Access is denied")) { System.out.println("Access denied. Do you want to authorize? (Y/N)"); String response = scanner.nextLine(); if (response.equalsIgnoreCase("Y")) { try { Files.setAttribute(file.toPath(), "dos:hidden", false); Files.setAttribute(file.toPath(), "dos:readonly", false); Files.write(file.toPath(), newContent.getBytes()); System.out.println("File authorized and written successfully."); } catch (IOException ex) { System.out.println("Error authorizing file."); } } else { System.out.println("File access denied."); } } else { System.out.println("Error reading/writing file."); } } } } ``` In this code, we first prompt the user to enter the file path. We then read the file using the `Files.readAllBytes` method and display its content. We then prompt the user to enter new content and write it to the file using the `Files.write` method. If an `IOException` is thrown during writing, we check if it's an "Access is denied" error. If it is, we prompt the user to authorize the file by setting the "hidden" and "readonly" attributes to false using the `Files.setAttribute` method. We then attempt to write the file again and display a success or error message accordingly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值