java学习第22天--IO综合练习题集锦3


 long型数据和它的原生数组相互转换
/*
 * long型数据和它的原生数组相互转换
 * 
 */
public class Demo {

public static void main(String[] args) {
long n = Long.MAX_VALUE;
System.out.println(n);

byte[] buf = long2Bytes(n);
long res = bytes2Long(buf);
System.out.println(res);

// for (byte b : buf) {
// System.out.println(b);//127
// }

}

//byte[] --> long:高位在前
public static long bytes2Long(byte[] buf){
long l0 = (buf[0] & 0xFFL)<< 56;
long l1 = (buf[1] & 0xFFL)<< 48;
long l2 = (buf[2] & 0xFFL)<< 40;
long l3 = (buf[3] & 0xFFL)<< 32;
long l4 = (buf[4] & 0xFFL)<< 24;
long l5 = (buf[5] & 0xFFL)<< 16;
long l6 = (buf[6] & 0xFFL)<< 8;
long l7 = (buf[7] & 0xFFL)<< 0;

return l0 + l1 + l2 + l3 + l4 + l5 + l6 + l7;
}

//long --> byte[]:高位在前
public static byte[] long2Bytes(long n){
byte[] buf = new byte[8];
buf[0] = (byte)(n >> 56);
buf[1] = (byte)(n >> 48);
buf[2] = (byte)(n >> 40);
buf[3] = (byte)(n >> 32);
buf[4] = (byte)(n >> 24);
buf[5] = (byte)(n >> 16);
buf[6] = (byte)(n >> 8);
buf[7] = (byte)(n >> 0);//127

return buf;
}

}

随机访问文件:RandomAccessFile
此类的实例支持对随机访问文件的读取和写入随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组存在指向该隐含数组的光标或索引,称为文件指针;输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。
构造方法:
RandomAccessFile(File file, String mode)
RandomAccessFile(String name, String mode)
读写方法:
可以读写多种常见类型数据
特殊方法:
void seek(long pos)

随机访问文件(只做简单了解)

import java.io.RandomAccessFile;
 
/*
 * RandomAccessFile(File file,String mode)
 * RandomAccessFile(String name,String mode)
 */
public class RandomAccessFileDemo {
   public static void main(String[] args ) throws Exception {
     
      RandomAccessFile raf = new RandomAccessFile( "raf.txt" , "rw" );
     
      raf .writeInt(200);
      // 移动文件指针到文件头
      raf .seek(0);
     
      int i = raf .readInt();
      System . out .println( i );
 
      raf .close();
   }
}
 

import java.io.RandomAccessFile;
/*
 * 随机访问文件父类是 Object.
 * 可以进行读和写两种操作 .
 * 在构造方法中指定对文件操作的模式 :
 * 一般使用 :" rw ", 表示既可以读 , 又可以写 .
 * RandomAccessFile(File file,String mode)
 * RandomAccessFile(String name,String mode)
 */
public class RandomAccessFileDemo2 {
   public static void main(String[] args ) throws Exception {
      // 创建对象
      RandomAccessFile raf = new RandomAccessFile( "raf.txt" , "rw" );
      // 写出数据
      raf .write(97);
      System. out .println( raf .getFilePointer());
      raf .writeInt(200);
      System. out .println( raf .getFilePointer());
      raf .writeUTF( "hello" );
      System. out .println( raf .getFilePointer());
     
      raf .writeUTF( "world" );
      System. out .println( raf .getFilePointer());
     
      // 定位到字符串偏移量
//    raf.seek(12);
//   
//    // 读一个字符串 , 读取方法要和将要读取的真实数据类型配套
//    String s = raf.readUTF();
//    System.out.println(s);
     
      // 多次读取指定的字符串
      for ( int i = 0; i <5; i ++){
         raf .seek(12);
         System. out .println( raf .readUTF());
      }
      // 释放资源
      raf .close();
   }
}



随机访问文件中有一个字符串,如何实现对字符串多次读取?


Properties
不属于 io 包下 , 但是由于某些操作和 io 流相关 , 所以放到 io 这里讲解,当成 Map 的子类使用 : 主要是添加元素及遍历
特有方法 :
setProperty //设置配置内容
getProperty //获取配置内容
stringPropertyNames

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
 
/*
 * Properties 不属于 io 流体系 ,java.util 包下 .
 * 作为 Hashtable 的子类使用
 */
public class PropertiesDemo2 {
 
   public static void main(String[] args ) throws Exception {
      // 创建属性对象
      Properties p = new Properties();
     
      // 将配置文件内容加载到内存中
      p .load( new FileInputStream( "config.properties" )); //age = 15
     
      // 获取配置文件内容
      String age = p .getProperty( "age" );
//    System.out.println(age);
     
      // 更改配置
      p .setProperty( "age" , Integer. parseInt ( age ) + 1 + "" );
     
      // 持久化到文件
//    p.store(new FileOutputStream("config.properties"), "hello world");
      p .store( new FileOutputStream( "config.properties" ), null );
//    p.store(new FileOutputStream("config.properties"), " 中文 ");// 注释中不支持中文
   }
}
 



经验值设置问题:
新手第一次登陆经验值设置200
老手每次登陆经验值+10
package com.czz.test01;
 
/*
 * 经验值设置问题 :
新手第一次登陆经验值设置 200
老手每次登陆经验值 +10
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
 
public class PropertiesDemo { 
   public static void main(String[] args ) throws Exception {
      Properties p = new Properties();
     
      p .load( new FileInputStream( "config.Properties" ));
     
      String exp = p .getProperty( "exp" );
     
      if ( exp .equals( "0" )) {
         p .setProperty( "exp" , "200" );
      } else {
         p .setProperty( "exp" , Integer. parseInt ( exp )+10 + "" );
      }
     
      p .store( new FileOutputStream( "config.Properties" ), null );
   } 
}
 
游戏试玩次数限制问题
package com.czz.test01;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
 
public class PropertiesDemo2 {
 
   public static void main(String[] args ) throws Exception {
      Properties p = new Properties();
      p .load( new FileInputStream( "czz.properties" ));
 
      String time = p .getProperty( "time" );
      int i = Integer. parseInt ( time );
     
      if ( i <=0) {
         System. out .println( " 当前试用结束 " );
      } else {
         System. out .println( " 当前试用第 : " + (10- i +1) + " 次使用 , 还剩 " + ( i -1) + " 次使用 " );
         play ();
         p .setProperty( "time" , i -1+ "" );
         p .store( new FileOutputStream( "czz.properties" ), null );
      }
   }
 
   private static void play() {
      System. out .println( " 正在游戏中 ......." );
   }
}
 


********************************
切割大文件 *****
/*
 * 切割大文件
 * 1.按固定大小切 (常用)
 * 2.按个数切
 */
package com.czz.test02;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
 
public class SplitFileDemo {
 
   public static void main(String[] args ) throws Exception {
      File srcFile = new File( "E:\\czz\\VMware workstation  12.rar" );
      File destFolder = new File( "E:\\czz\\VMware" );
  
      if (! destFolder .exists()) {
         destFolder .mkdirs();
      }
     
      splitFile ( srcFile , destFolder );
   }
 
   // 切割文件
   public static void splitFile(File srcFile , File destFolder ) throws Exception {
      // 计算切割后的小文件个数
      // 先求出源文件的长度
      int length = ( int ) srcFile .length();
      // 再设置切割文件的大小
      int size = 1024 * 1024 * 50;
 
      int count = 0;
      if ( length % size == 0) {
         count = length / size ;
      } else {
         count = length / size + 1;
      }
 
      // 遍历生成小文件
      for ( int i = 0; i < count ; i ++) {
         String name = srcFile .getName() + "_split_" + ( i + 1);
         File file = new File( destFolder , name );
         file .createNewFile();
 
         // 计算当前文件的长度
         int len = 0;
         if ( i == count - 1 && length % size != 0) {
            len = length % size ;
         } else {
            len = size ;
         }
        
         // 流的对考 , 生成小文件
         copy ( srcFile , file , i * size , len );
      }
 
   }
 
   /**
    *
    * @param srcFile 要切割的文件
    * @param file 生成的小文件
    * @param i 起始位置
    * @param len 拷贝的数据长度
    * @throws Exception
    */
   private static void copy(File srcFile , File file , int i , int len ) throws Exception {
      // 创建随机访问文件
      RandomAccessFile raf = new RandomAccessFile( srcFile , "rw" );
      // 定位起始位置
      raf .seek( i );
      FileOutputStream fos = new FileOutputStream( file );
     
      // 创建字节数组
      byte [] buf = new byte [ len ];
     
      // 拷贝数据
      raf .read( buf );
      fos .write( buf );
     
      // 释放资源
      raf .close();
      fos .close();
   }
}
 

合并文件
package com.czz.test02;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
public class MergeFileDemo {
 
   public static void main(String[] args ) throws Exception {
      File srcFolder = new File( "E:\\czz\\VMware" );
      File destFolder = new File( "E:\\czz\\merger" );
     
      mergeFile ( srcFolder , destFolder );
      // 这个 srcFolder 表示切割后的用来存放切割文件的文件夹
      // 这个 destFolder 表示合并后要存放的文件夹
   }
 
   // 合并文件
   private static void mergeFile(File srcFolder , File destFolder ) throws Exception {
      // 获取源文件名
      File[] files = srcFolder .listFiles();
      String name = files [0].getName(); // 随便找个可以获取文件名的索引
      name = name .substring(0, name .indexOf( "_" ));   //indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置
     
      // 创建目标文件
      File mergeFile = new File( destFolder , name );
      mergeFile .createNewFile();
  
      // 在循环外创建输出流
      FileOutputStream fos = new FileOutputStream( mergeFile );
     
      for (File file : files ) {
         // 复制文件
         copy ( file , fos );
      }
   }
 
   // 拷贝文件
   private static void copy(File file , FileOutputStream fos ) throws Exception {
      // 创建输入流
      BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ));
      BufferedOutputStream bos = new BufferedOutputStream( fos );
     
      // 流拷贝
      byte [] buf = new byte [1024];
      int len = 0;
      while (( len = bis .read( buf )) != -1){
         bos .write( buf , 0, len );
         bos .flush();
      }
      bis .close();
     
      // 输出流不用关
   }
}
**************************************************** 

归档:
******************************************************************
package dad.czz;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
/*
 * 归档过程 :
 *  头文件信息 :
 *
 *  1 个字节的文件名长度
 *  n 个字节的文件名内容
 *  4 个字节文件内容的长度
 *  m 个字节文件真实内容
 */
public class Demo {
 
   public static void main(String[] args ) throws Exception {
      File srcFolder = new File( "c:/arch" );
      File destFolder = new File( "c:/arch/arched" );
 
      archFile ( srcFolder , destFolder );
   }
 
   // 归档
   public static void archFile(File srcFolder , File destFolder ) throws Exception {
      // 列出所有要归档的文件对象
      File[] files = srcFolder .listFiles();
      // 归档文件输出流
      File archFile = new File( destFolder , "arch.dat" );
      FileOutputStream fos = new FileOutputStream( archFile );
 
      for (File file : files ) {
         if ( file .isFile()) {
            String name = file .getName();
            // 文件名数组
            byte [] bys = name .getBytes();
            // 文件名数组长度
            int len = bys . length ;
            // 文件内容的长度
            int length = ( int ) file .length();
 
            // 写文件名长度
            fos .write( len );
            // 写文件名的内容
            fos .write( bys );
            // 写文件长度
            fos .write( int2Bytes ( length ));
            // 写真实的数据
            copyData ( file , fos );
         }
      }
      fos .close();
   }
 
   // 归档
   private static void copyData(File file , FileOutputStream fos ) throws Exception {
      // 封装输入流
      BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ));
      BufferedOutputStream bos = new BufferedOutputStream( fos );
 
      byte [] buf = new byte [1024];
      int len = 0;
      while (( len = bis .read( buf )) != -1) {
         bos .write( buf , 0, len );
         bos .flush();
      }
      bis .close();
   }
 
   // int --> byte[] : 高位在前
   public static byte [] int2Bytes( int n ) {
      byte [] buf = new byte [4];
      buf [0] = ( byte ) ( n >> 24);
      buf [1] = ( byte ) ( n >> 16);
      buf [2] = ( byte ) ( n >> 8);
      buf [3] = ( byte ) ( n >> 0);
 
      return buf ;
   }
}
 
 

解档:
********************************************
package com.oldboyedu.test05;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
/*
 * 归档过程 :
 * 头文件信息 :
 * 1 个字节的文件名长度
 * n 个字节的文件名内容
 * 4 个字节文件内容的长度
 * m 个字节文件真实内容
 */
public class ArcherDemo {
 
   public static void main(String[] args ) throws Exception {
//    File srcFolder = new File("c:/arch");
//    File destFolder = new File("c:/arch/arched");
//   
//    archFile(srcFolder,destFolder);
     
      File srcFile = new File( "c:/arch/arched/arch.dat" );
      File destFolder = new File( "c:/arch/unarched" );
      unarchFile ( srcFile , destFolder );
   }
  
   // 解档
   public static void unarchFile(File srcFile ,File destFolder ) throws Exception{
      FileInputStream fis = new FileInputStream( srcFile );
      int b = 0;
        
         byte [] nameArr = new byte [ b ];
 
         fis .read( nameArr );
         // 还原文件名
         String name = new String( nameArr );
         File file = new File( destFolder , name );
         // 还原文件长度
         byte [] contentLen = new byte [4];
         fis .read( contentLen );
         int contentSize = bytes2Int ( contentLen );
        
         byte [] content = new byte [ contentSize ];
         // 读写数据
         fis .read( content );
         FileOutputStream fos = new FileOutputStream( file );
         fos .write( content );
         fos .close();
      }
      // 释放资源
      fis .close();
   }
  
   //byte[] --> int : 高位在前
   private static int bytes2Int( byte [] b ) {
      int i0 = ( b [0] & 0xFF) << 24;
      int i1 = ( b [1] & 0xFF) << 16;
      int i2 = ( b [2] & 0xFF) << 8;
      int i3 = ( b [3] & 0xFF) << 0;
      return i0 + i1 + i2 + i3 ;
   }
}
 
 

猜你喜欢

转载自blog.csdn.net/czz1141979570/article/details/80157460