Java 的IO操作(文件的读,写操作)

本文通过对比两种不同文件编辑方式(使用与未使用缓冲技术)的性能差异,特别是在处理大文件时的表现。实验结果显示,在小文件处理上两者差异不大,但在处理超过一定大小的文件时出现了内存溢出等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * FileEditor.java
 *
 * Created on 2006年9月13日, 下午2:22
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
/**
*这是第一个类,没有利用缓冲技术
*/
package fileeditor;
import java.io.*;
/**
 *
 * @author Administrator
 */
public class FileEditor {
   
    /** Creates a new instance of FileEditor */
    public FileEditor() {
    }
     /*用于创建文件;
     */
    public static void createFile(String fileName) throws IOException{
            String file=fileName;
            FileWriter f=new FileWriter(file);
            f.close();
     }
    /*用于更新文件内容;
     */
    public static void updateFileContent(String fileName, byte[] content) throws IOException{
        ByteArrayOutputStream f=new ByteArrayOutputStream();
        byte[] buf=content;
        f.write(buf);
        OutputStream out=new FileOutputStream(fileName);
        f.writeTo(out);
        f.close();
        out.close();
    }
    /*用于获取文件内容(返回比特流(字节)数组)
     */
    public static byte[] getFileContent(String fileName) throws IOException {
        int size;
        File f=new File(fileName);
        FileInputStream in = null;
        in = new FileInputStream(f);
        size=in.available();
        byte[] content =new byte[size];
        in.read(content,0,size);
        in.close();
        return content;
    }
    /*用于删除文件;
     */
    public static void deleteFile(String fileName) throws  IOException{
        String file=fileName;
        File f=new File(fileName);
        f.delete();
    }
}
/**
*这里是第二个类,用了缓冲技术
*/
/*用于更新文件内容;
     */
    public static void updateFileContent(String fileName, byte[] content) throws IOException{
        byte[] buf=content;
        BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(fileName));
        out.write(content);
        out.close();
    }
    /*用于创建文件;
     */
    public static void createFile(String fileName)throws IOException{
       
            String file=fileName;
            FileWriter f=new FileWriter(file);
            f.close();
     
    }
    /*用于获取文件内容
     */
    public static byte[] getFileContent(String fileName) throws IOException {
        File f=new File(fileName);
        BufferedInputStream in =  new BufferedInputStream(new FileInputStream(f));
        byte[] content =null;
        in.read(content);        
        in.close();
        return content;
    }
    /*用于删除文件;
     */
    public static void deleteFile(String fileName) throws  IOException{
        String file=fileName;
        File f=new File(fileName);
        f.delete();
    }
/**
*这是主函数
*/
public static void main(String[] args) {
        byte[] content = null;
       // BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String fromFile = null;
        String toFile=null;
        try {
         fromFile="g://asd.jpg";
         content=FileEditor.getFileContent(fromFile);
         for (int i=0;i<1000;i++){
             toFile="g://test//";
             toFile+=i;
             toFile=".jpg";
             FileEditor.createFile(toFile);//创建目标文件;
             FileEditor.updateFileContent(toFile,content);     
         }
        
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
 在这次试验中,我想对比用缓冲流和不用缓冲流读写文件的效率,同一个文件(150k)复制1000份,在我机器上,不用缓冲的是4秒,用了也是4秒,所以没比较出来。后来当我试图复制更大的文件(30M)时出现了错误。
提示 :Exception in thread "main" java.lang.OutOfMemoryError: Java heap space :
我调试了半天,也没弄懂,希望高手指教。其他的问题没什么,这个类完全可以适用与小于30M的任何情况下使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值