一 文件的操作

1.1 概况

1,切割文件的原理:一个源对应多个目的;切割文件的两种方式。

2,碎片文件的命名和编号。

3,程序代码体现。

4,如何记录源文件的类型以及碎片的个数(建立配置信息文件)(其实也可以将这些信息记录碎片文件中)

5,通过Properties属性集建立配置文件。

常见方法:load(InputStream) load(Reader)

store(OutputStream,conmments),store(Writer,conmments)

6,Properties的作为配置在应用程序很常见,主要用于将配置信息持久化。

建立的配置文件扩展名规范: .properties.

代码 SplitFileTest.java

  1. public static void main(String[] args) throws IOException {
  2.  
  3. /*
  4.  
  5. * 练习:将一个媒体文件切割成多个文件
  6.  
  7. * 思路:
  8.  
  9. * 1.读取源文件,将源文件的数据分别复制到多个文件中
  10.  
  11. * 2.切割方式有两种:按照碎片个数切.要么按照指定大小切
  12.  
  13. * 3.一个输入流对应多个输出流
  14.  
  15. * 4.每个碎片都需要编号,顺序不要错
  16.  
  17. * */
  18.  
  19. // 定义源文件
  20.  
  21. File srcFile = new File("tempFile//50.avi");
  22.  
  23. // 定义目标文件
  24.  
  25. File partDir = new File("tempFile//partFile");
  26.  
  27. // 切割方法
  28.  
  29. splitFile(srcFile,partDir);
  30.  
  31. }
  32.  
  33. public static void splitFile(File srcFile, File partDir) throws IOException {
  34.  
  35. //如果提供的源文件不存在,则异常
  36.  
  37. if(!(srcFile.exists() && srcFile.isFile())){
  38.  
  39. throw new RuntimeException("源文件不是正确文件或者不存在");
  40.  
  41. }
  42.  
  43. // 如果提供的目标文件不存在,则创建
  44.  
  45. if(!(partDir.exists())){
  46.  
  47. partDir.mkdir();
  48.  
  49. }
  50.  
  51. // 定义文件编号
  52.  
  53. int count=;
  54.  
  55. //源输入流
  56.  
  57. FileInputStream fis = new FileInputStream(srcFile);
  58.  
  59. // 输出流,由于是多个输出流.因此要定义为空
  60.  
  61. FileOutputStream fos = null;
  62.  
  63. // 缓存区,定义你要拿多少
  64.  
  65. byte [] buf = new byte [*];
  66.  
  67. int len = ;
  68.  
  69. while((len=fis.read(buf)) != -){
  70.  
  71. // 输出流对象
  72.  
  73. fos = new FileOutputStream(new File(partDir,(count++)+".part"));
  74.  
  75. //写入目标文件
  76.  
  77. fos.write(buf, , len);
  78.  
  79. }
  80.  
  81. }

1.2 加入配置文件

  1. /*
  2.  
  3. * 将源文件以及切割文件的一些信息也保存起来随着碎片文件一起发送
  4.  
  5. * 信息:
  6.  
  7. * 1.源文件的名称
  8.  
  9. * 2.切割的碎片的个数
  10.  
  11. * 将这些信息单独封装到一个文件中
  12.  
  13. * 还要一个输出流完成此动作
  14.  
  15. *
  16.  
  17. * */
  18.  
  19. String filename = srcFile.getName();
  20.  
  21. int partCount = count;
  22.  
  23. fos = new FileOutputStream(new File(partDir,count+".partconfig"));
  24.  
  25. fos.write(("filename="+filename+line_separator).getBytes() );
  26.  
  27. fos.write(("filecount="+Integer.toString(count)).getBytes());

1.3 分析配置文件

  1. /*
  2.  
  3. * 配置文件规律,只读一行文本,按照 = 对文本进行分割即可
  4.  
  5. *
  6.  
  7. * */
  8.  
  9. BufferedReader buf = new BufferedReader(new FileReader(configFile));
  10.  
  11. String line = null;
  12.  
  13. while((line = buf.readLine())!=null)
  14.  
  15. {
  16.  
  17. String [] arr = line.split("=");
  18.  
  19. System.out.println(arr[]+"======"+arr[]);
  20.  
  21. }

1.4 配置文件存储

  1. Properties的使用 还有其他代码
  2.  
  3. /*
  4.  
  5. * 将源文件以及切割文件的一些信息也保存起来随着碎片文件一起发送
  6.  
  7. * 信息:
  8.  
  9. * 1.源文件的名称
  10.  
  11. * 2.切割的碎片的个数
  12.  
  13. * 将这些信息单独封装到一个文件中
  14.  
  15. * 还要一个输出流完成此动作
  16.  
  17. *
  18.  
  19. * */
  20.  
  21. String filename = srcFile.getName();
  22.  
  23. int partCount = count;
  24.  
  25. fos = new FileOutputStream(new File(partDir,count+".properties"));
  26.  
  27. Properties prop = new Properties();
  28.  
  29. prop.setProperty("filename", srcFile.getName());
  30.  
  31. prop.setProperty("patcount", Integer.toString(partCount));
  32.  
  33. prop.store(fos, "part file info");

1.5 合并

恐怖方式

  1. public static void main(String[] args) throws IOException {
  2.  
  3. //合并碎片文件
  4.  
  5. // 多个源去的一个目的地
  6.  
  7. FileInputStream fis1 = new FileInputStream("tempFile//partFile//1.part");
  8.  
  9. FileInputStream fis2 = new FileInputStream("tempFile//partFile//2.part");
  10.  
  11. FileInputStream fis3 = new FileInputStream("tempFile//partFile//3.part");
  12.  
  13. FileInputStream fis4 = new FileInputStream("tempFile//partFile//4.part");
  14.  
  15. FileInputStream fis5 = new FileInputStream("tempFile//partFile//5.part");
  16.  
  17. FileOutputStream fos = new FileOutputStream("5.avi");
  18.  
  19. byte [] buf = new byte[*];
  20.  
  21. int len1 = fis1.read(buf);
  22.  
  23. fos.write(buf, , len1);
  24.  
  25. int len2 = fis2.read(buf);
  26.  
  27. fos.write(buf, , len2);
  28.  
  29. int len3 = fis3.read(buf);
  30.  
  31. fos.write(buf, , len3);
  32.  
  33. int len4 = fis4.read(buf);
  34.  
  35. fos.write(buf, , len4);
  36.  
  37. int len5 = fis5.read(buf);
  38.  
  39. fos.write(buf, , len5);
  40.  
  41. fos.close();
  42.  
  43. fis1.close();
  44.  
  45. fis2.close();
  46.  
  47. fis3.close();
  48.  
  49. fis4.close();
  50.  
  51. fis5.close();
  52.  
  53. }

序列流

  1. /*
  2.  
  3. * 如上代码的问题,
  4.  
  5. * 当流对象多时,应该先存储起来,面的流对象的容器操作更容易
  6.  
  7. * 1.需要容器,
  8.  
  9. * 2.将流对象和文件关联后存储到容器中
  10.  
  11. * 3.遍历容器获取其中的流对象,在进行频繁的读写操作
  12.  
  13. * 4.即使关流也是遍历容器对每个流对象进行close的调用
  14.  
  15. *
  16.  
  17. */
  18.  
  19. List<FileInputStream> list = new ArrayList<FileInputStream>();
  20.  
  21. // 读
  22.  
  23. for (int i = ; i < ; i++) {
  24.  
  25. list.add(new FileInputStream("tempFile//partFile//" + i + ".part"));
  26.  
  27. }
  28.  
  29. FileOutputStream fos = new FileOutputStream("5.avi");
  30.  
  31. // 缓存区
  32.  
  33. byte[] buf = new byte[ * ];
  34.  
  35. // 写入
  36.  
  37. for (FileInputStream fis : list) {
  38.  
  39. int len = fis.read(buf);
  40.  
  41. fos.write(buf,,len);
  42.  
  43. }
  44.  
  45. // 关闭流
  46.  
  47. for (FileInputStream fis : list) {
  48.  
  49. fis.close();
  50.  
  51. }
  52.  
  53. 对上面代码进行封装,序列流
  54. public static void mergerFile(File partDir) throws IOException {
  55.  
  56. List<FileInputStream> list = new ArrayList<FileInputStream>();
  57.  
  58. // 读
  59.  
  60. for (int i = ; i < ; i++) {
  61.  
  62. list.add(new FileInputStream("tempFile//partFile//" + i + ".part"));
  63.  
  64. }
  65.  
  66. // 怎么获取枚举对象呢?考虑到collection里面去找找
  67.  
  68. Enumeration<FileInputStream> en = Collections.enumeration(list);
  69.  
  70. // 序列流
  71.  
  72. SequenceInputStream sis = new SequenceInputStream(en);
  73.  
  74. // 输出流
  75.  
  76. FileOutputStream fos = new FileOutputStream("5.avi");
  77.  
  78. // 缓存区
  79.  
  80. byte[] buf = new byte[];
  81.  
  82. int len= ;
  83.  
  84. while((len = sis.read(buf))!=-){
  85.  
  86. fos.write(buf,,len);
  87.  
  88. System.out.println();
  89.  
  90. }
  91.  
  92. fos.close();
  93.  
  94. sis.close();
  95.  
  96. }

配置文件的读取

虽然合并成功.但是也有一些问题

  1. 如何明确碎片的个数,来确定循环的次数,以明确多少个输入流对象
  2. 如何知道合并的文件的类型,

解决的方案是:应该先读取配置文件.

  1. public static void main(String[] args) throws IOException {
  2.  
  3. File partDir = new File("tempFile//partFile");
  4.  
  5. // 1.获取配置文件
  6.  
  7. File configFile = getConfigFile(partDir);
  8.  
  9. System.out.println(configFile);
  10.  
  11. //2.先获取配置文件信息容器,获取配置文件信息的属性集
  12.  
  13. Properties prop = getProperties(configFile);
  14.  
  15. System.out.println(prop);
  16.  
  17. // 获取属性信息,并传到合并方法里面去
  18.  
  19. mergerFile(partDir, prop);
  20.  
  21. }
  22.  
  23. // 根据配置文件获取配置文件信息的属性集
  24.  
  25. private static Properties getProperties(File configFile) throws IOException {
  26.  
  27. FileInputStream fis = null;
  28.  
  29. Properties prop = new Properties();
  30.  
  31. try {
  32.  
  33. // 读取流和配置文件相关联
  34.  
  35. fis = new FileInputStream(configFile);
  36.  
  37. // 将流的数据加载到集合中去
  38.  
  39. prop.load(fis);
  40.  
  41. } finally {
  42.  
  43. if (fis != null)
  44.  
  45. fis.close();
  46.  
  47. }
  48.  
  49. // 将集合返回
  50.  
  51. return prop;
  52.  
  53. }
  54.  
  55. // 根据碎片目录获取配置文件对象
  56.  
  57. private static File getConfigFile(File partDir) {
  58.  
  59. if (!(partDir.isDirectory() && partDir.exists())) {
  60.  
  61. throw new RuntimeException("不是有效的目录");
  62.  
  63. }
  64.  
  65. // 判断碎片文件目录中是否存在properties文件
  66.  
  67. // 过滤器
  68.  
  69. File[] files = partDir.listFiles(new FileFilter() {
  70.  
  71. @Override
  72.  
  73. public boolean accept(File pathname) {
  74.  
  75. return pathname.getName().endsWith(".properties");
  76.  
  77. }
  78.  
  79. });
  80.  
  81. if (files.length != ) {
  82.  
  83. throw new RuntimeException("properties扩展名的文件不存在,或不唯一");
  84.  
  85. }
  86.  
  87. File configFile = files[];
  88.  
  89. return configFile;
  90.  
  91. }
  92.  
  93. // 合并
  94.  
  95. public static void mergerFile(File partDir , Properties prop) throws IOException {
  96.  
  97. // 获取属性集中的信息
  98.  
  99. String filename = prop.getProperty("filename");
  100.  
  101. int partCount = Integer.parseInt(prop.getProperty("patcount"));
  102.  
  103. // 使用io包中的SeqenceInputStream 对碎片文件进行合并,将多个文件合并一个读取流
  104.  
  105. List<FileInputStream> list = new ArrayList<FileInputStream>();
  106.  
  107. // 读
  108.  
  109. for (int i = ; i < partCount; i++) {
  110.  
  111. list.add(new FileInputStream("tempFile//partFile//" + i + ".part"));
  112.  
  113. }
  114.  
  115. // 怎么获取枚举对象呢?考虑到collection里面去找找
  116.  
  117. Enumeration<FileInputStream> en = Collections.enumeration(list);
  118.  
  119. // 序列流
  120.  
  121. SequenceInputStream sis = new SequenceInputStream(en);
  122.  
  123. // 输出流
  124.  
  125. FileOutputStream fos = new FileOutputStream(filename);
  126.  
  127. // 缓存区
  128.  
  129. byte[] buf = new byte[];
  130.  
  131. int len = ;
  132.  
  133. while ((len = sis.read(buf)) != -) {
  134.  
  135. fos.write(buf, , len);
  136.  
  137. }
  138.  
  139. fos.close();
  140.  
  141. sis.close();
  142.  
  143. }

1.6 记录程序运行次数

  1. public static void main(String[] args) throws IOException {
  2.  
  3. /*
  4.  
  5. * 练习:定义一个功能程序运行次数,满足试用次数后给出提示,试用次数到了
  6.  
  7. * 思路:
  8.  
  9. * 1.需要计数器,
  10.  
  11. * 2.需要一个拷久的计数器,
  12.  
  13. * 3.并且给这个计数器取个名字
  14.  
  15. * 4.properties集合下好满足这个要求
  16.  
  17. *
  18.  
  19. * */
  20.  
  21. if (isStop()){
  22.  
  23. System.out.println("试用次数到了.不能在用了");
  24.  
  25. return;
  26.  
  27. }
  28.  
  29. runcode();
  30.  
  31. }
  32.  
  33. private static boolean isStop() throws IOException {
  34.  
  35. // 配置文件
  36.  
  37. File configFile = new File("tempFile\\app.properties");
  38.  
  39. if (!(configFile.exists())){
  40.  
  41. configFile.createNewFile();
  42.  
  43. }
  44.  
  45. int count = ;
  46.  
  47. // 创建属性集
  48.  
  49. Properties prop = new Properties();
  50.  
  51. // 定义读取流和配置文件关联
  52.  
  53. FileInputStream fis = new FileInputStream(configFile);
  54.  
  55. prop.load(fis);
  56.  
  57. String value = prop.getProperty("count");
  58.  
  59. // 对value进行判断,如果存在就对其自增
  60.  
  61. if(value != null ){
  62.  
  63. count=Integer.parseInt(value);
  64.  
  65. if(count>){
  66.  
  67. return true;
  68.  
  69. }
  70.  
  71. }
  72.  
  73. count++;// 对其值进行自增
  74.  
  75. // 将处增后的值和指定的键重新存储到属性集中,键相同,值覆盖
  76.  
  77. prop.setProperty("count", Integer.toString(count));
  78.  
  79. FileOutputStream fos = new FileOutputStream(configFile);
  80.  
  81. prop.store(fos, "app run count");
  82.  
  83. fos.close();
  84.  
  85. fis.close();
  86.  
  87. return false;
  88.  
  89. }
  90.  
  91. // 程序文体
  92.  
  93. public static void runcode() {
  94.  
  95. System.out.println("程序运行了::::::play ");
  96.  
  97. }

二 对象

序列化

ObjectOutputStream

  1. // 应该先有对象,Person
  2.  
  3. Person p1 = new Person("LISHI",);
  4.  
  5. //
  6.  
  7. FileOutputStream fos = new FileOutputStream("tempFile\\obj.object");
  8.  
  9. ObjectOutputStream obs = new ObjectOutputStream(fos);
  10.  
  11. obs.writeObject(p1);
  12.  
  13. obs.close();

反序列化

ObjectInputStream

  1. //需求:读取已有的对象文件,并获取对象中的数据。
  2.  
  3. //通过阅读ObjectOutputStream对象的文档,发现有一个对应的对象ObjectInputStream可以用于读区存储对象的文件
  4.  
  5. //对象的反序列化。
  6.  
  7. FileInputStream fis = new FileInputStream("tempfile\\obj.object");
  8.  
  9. ObjectInputStream ois = new ObjectInputStream(fis);
  10.  
  11. Object obj = ois.readObject();
  12.  
  13. System.out.println(obj.toString());

注意:

1.一定要在你的类里面加这个private static final long serialVersionUID = 1L

2.对于一个非静态的数据也不想序列化怎么办?需要一个关键字来修饰 transient

java-文件切割合并_对象的序列化的更多相关文章

  1. JAVA之旅(三十)——打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码

    JAVA之旅(三十)--打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码 三十篇了,又是一个 ...

  2. java下io文件切割合并功能加配置文件

    package cn.stat.p1.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...

  3. java下io文件切割合并功能

    package cn.stat.p1.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...

  4. Java 文件切割工具类

    Story: 发送MongoDB 管理软件到公司邮箱,工作使用. 1.由于公司邮箱限制附件大小,大文件无法发送,故做此程序用于切割大文件成多个小文件,然后逐个发送. 2.收到小文件之后,再重新组合成原 ...

  5. jni 文件切割合并

    最近学习c++,看到很多常用的例子,比如文件切割,切割后后缀可以自定义,别人就无法从表面的一个文件看出是什么,也无法查看到原文件信息,只有合并后才能识别这庐山真面目 实现也比较粗暴,首先在应用层定义好 ...

  6. 四、java基础-面向过程_对象_类中可出现的因素

    1.面向过程和面向对象区别: 1)面向过程:开发一个应用程序.一个项目,必须先了解整个过程,了解各个步骤.模块间的因果关系,使的面向过程方式去开发程序时,代码和代码之间的关联程度是非常强.所以其中任何 ...

  7. IO流_文件切割与合并(带配置信息)

    在切割文件的时候应该生成一个记录文件信息的文件,以便在以后合并文件的时候知道这个文件原来的文件名和记录文件切割完后生成了多少个切割文件 import java.io.File; import java ...

  8. Java对象序列化文件追加对象的问题,以及Java的读取多个对象的问题解决方法。

    这几天做一个小的聊天项目用到对象序列化的知识,发现对象序列化不能像普通文件一样直接追加对象.每次写入对象都会被覆盖.弄了2个多小时终于解决了.Java默认的对象序列化是每次写入对象都会写入一点头ace ...

  9. Java---练习:文件切割与合并(1)

    实现对大文件的切割与合并. 按指定个数切(如把一个文件切成10份)或按指定大小切(如每份最大不超过10M),这两种方式都可以. 示例程序说明: 文件切割:把一个文件切割成多个碎片,每个碎片的大小不超过 ...

随机推荐

  1. 箭头函数报错:Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.

    解决:根目录新建babel.config.js加入如下内容 module.exports = { presets: [ "@babel/preset-env", "@ba ...

  2. PHPstorm同步服务器代码的缺点---命名空间undefined

      在把一个服务器的代码同步到phpstorm下开发的时候,发现新建的命名空间代码都失效了,然而换到 https://blog.csdn.net/afraid_to_have/article/deta ...

  3. JEECMS 自定义标签

    CMS 是”Content Management System” 的缩写,意为” 内容管理系统”. 内容管理系统是企业信息化建设和电子政务的新宠,也是一个相对较新的市场.对于内容管理,业界还没有一个统 ...

  4. quatz调度-手动终止线程(1) 创建触发器,线程执行完毕后添加到cleaner list

    import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException;impor ...

  5. Django项目: 4.用户登录登出功能

    用户登录登出功能 一.功能需求分析 1. 登录退出功能分析 流程图 功能 登录页面 登录功能 退出功能 二.登录页面 1. 接口设计 接口说明 类目 说明 请求方法 GET url定义 /user/l ...

  6. Spring Cloud各组件

    讲的不错:http://www.ityouknow.com/springcloud/2017/05/16/springcloud-hystrix.html Spring Cloud技术应用从场景上可以 ...

  7. mysql导出某张表的部分数据

    .使用into outfile '保存到操作系统的外部文件路径' mysql -uroot -p123456 -hhostname -P3306 select column_name_list fro ...

  8. Tuxera ntfs软件如何删除干净

    sudo /Library/Filesystems/fusefs_txantfs.fs/Contents/Resources/Support/uninstall-package.sh

  9. leyou_06——FastDFS在Nginx下的安装与测试

    1.FastDFS FastDFS是由淘宝的余庆先生所开发的一个轻量级.高性能的开源分布式文件系统.用纯C语言开发,功能丰富: 文件存储 文件同步 文件访问(上传.下载) 存取负载均衡 在线扩容 2. ...

  10. bootstrap字体图标在IE上不显示

    最简单的办法就是直接下载最新的bootstrap.css替换掉旧的. 但是由于我做的项目直接替换会出现样式冲突问题,因此只好慢慢找是什么属性导致图标不显示,最后找到了解决办法: 1.首先保字体文件的位 ...