Java中对二进制以及文本文件的读写操作

日期:2011-10-27    阅读:38   分类:Java资料

Java中对二进制以及文本文件的读写操作,对原生的类库做下简单包装:

public static byte[] readBinFile(String path) throws Exception
    {
        FileInputStream stream = new FileInputStream(path);
        
        int len = stream.available();
        byte[] buffer = new byte[len];
        stream.read(buffer);
        stream.close();
        
        return buffer;
    }
    
    public static String readAscFile(String path) throws Exception
    {
        FileInputStream stream = new FileInputStream(path);
        
        int len = stream.available();
        byte[] buffer = new byte[len];
        stream.read(buffer);
        stream.close();
        
        return new String(buffer);
    }
    
    public synchronized static void writeBinFile(String path, byte[] buffer) throws Exception
    {
        FileOutputStream output = new FileOutputStream(path, true);        
        output.write(buffer);
        output.flush();
        output.close();
    }
    
    public synchronized static void writeAscFile(String path, String content) throws Exception
    {
        FileOutputStream output = new FileOutputStream(path, true);
        OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8");
        
        writer.append(content);
        writer.flush();
        writer.close();
    }

本页链接: http://www.scriptlover.com/static/1041-java-文件操作

标签:

相关文章

网友评论

Leave a comment

 required

 required (Not published)

 required