专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java文件流的输入和输出

Java文件流的输入和输出

更新时间:2020-08-20 16:18:14 来源:动力节点 浏览1687次

1. java输入流类

String strFile = "Data/DataGroup.cpp";
 输入读取文件路径
File = new File(strFile);引入File.io包,实例化文件对象
InputStream in = null; 
 定义文件输入流in = new FileInputStream(file);文件输入流读取文件
创建合适文件大小的数组,一次性把数据从文件中读出
b1 = new byte[(int)file.length()]  ;当内容为空时返回-1,可以以此作为判断文件内容是否读取完毕        
in.read(b1);       
 读取文件中的内容到b[]数组,如果read()返回读取的字节内容,in.close();关闭
textArea.append(new String(b1));

2. 文件输出流

String strOut = "*Data/DataGroup_copy.cpp";
File file = new File(strOut);
OutputStream output = null;
output = new FileOutputStream(file);
  输出流中写入文件内容
output.write(b1);
读取输入流中b1的字节数组
output.close();

3. 工作中的输入输出流

工作上的文件输入输出流都要判断流是否读取完整
while(((len0 = fis.read(buf)) != -1)){
      baos.write(buf, 0, len0);
  }  
bao代表上面的b1字节数组
System.arrayCopy
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
    代码解释:
   Object src : 原数组
   int srcPos : 从元数据的起始位置开始
  Object dest : 目标数组
   int destPos : 目标数组的开始起始位置
  int length  : 要copy的数组的长度

例如:

 //2.写入文件名本身
    System.arraycopy(fnameBytes, 0, bytes, 4, fnameBytes.length);

4. 压缩程序

public class Archiver {
        public static void main(String[] args) throws Exception {
            FileOutputStream fos = new FileOutputStream("d:/arch/x.xar",true);
            fos.write(addFile("d:/pie.png"));
            fos.close();
        }
        
        /**
         * path : d:/xxx/xxx/a.jpg
         */
        public static byte[] addFile(String path) throws Exception{
            //文件
            File f = new File(path);
            //文件名
            String fname = f.getName();
            //文件名数组
            byte[] fnameBytes = fname.getBytes() ;
            //文件内容长度
            int len = (int)f.length();
            
            //计算总长度
            int total = 4 + fnameBytes.length + 4 + len ;
            
            //初始化总数组
            byte[] bytes = new byte[total];
            
            //1.写入文件名长度
            byte[] fnameLenArr = Util.int2Bytes(fnameBytes.length);
            System.arraycopy(fnameLenArr, 0, bytes, 0, 4);
            
            //2.写入文件名本身
            System.arraycopy(fnameBytes, 0, bytes, 4, fnameBytes.length);
            
            //3.写入文件内容长度
            byte[] fcontentLenArr = Util.int2Bytes(len);
            System.arraycopy(fcontentLenArr, 0, bytes, 4 + fnameBytes.length, 4);
            
            //4.写入文件内容
            //读取文件内容到数组中
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            FileInputStream fis = new FileInputStream(f);
            byte[] buf = new byte[1024];
            int len0 = 0 ;
            while(((len0 = fis.read(buf)) != -1)){
                baos.write(buf, 0, len0);
            }
            fis.close();
            //得到文件内容
            byte[] fileContentArr = baos.toByteArray();
            System.arraycopy(fileContentArr, 0, bytes, 4 + fnameBytes.length + 4, fileContentArr.length);
            return bytes ;
        }
    }

以上就是动力节点java培训机构的小编针对“Java文件流的输入和输出”的内容进行的回答,希望对大家有所帮助,如有疑问,请在线咨询,有专业老师随时为你服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>