特征提取算法提取二进制后面的特征数据,使用Java流实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28844767/article/details/84329069

帮别人解决一个问题:在搞图片特征相似度检索的东西,特征提取算法提取出来的二进制特征数据,想要读取二进制文件的每一行固定位之后的数据有啥好的办法没?

首先读取二进制文件,考虑到使用字节流,但是不能解决如何判断换行的问题,综上,本人是先使用BufferedReader流读取一行的数据,然后将拿到的tempString转化为字节数组,使用System.arraycopy方法去截取字节数组,得到想要的数据,然后通过FileOutputStream流将数据写入到对应的文件中。

先查询一些资料:JAVA中读取文件内容的方法有很多,比如按字节读取文件内容,按字符读取文件内容,按行读取文件内容,随机读取文件内容等方法。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;

public class ReadFromFile {
 /**
  * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
  */
 public static void readFileByBytes(String fileName) {
  File file = new File(fileName);
  InputStream in = null;
  try {
   System.out.println("以字节为单位读取文件内容,一次读一个字节:");
   // 一次读一个字节
   in = new FileInputStream(file);
   int tempbyte;
   while ((tempbyte = in.read()) != -1) {
    System.out.write(tempbyte);
   }
   in.close();
  } catch (IOException e) {
   e.printStackTrace();
   return;
  }
  try {
   System.out.println("以字节为单位读取文件内容,一次读多个字节:");
   // 一次读多个字节
   byte[] tempbytes = new byte[100];
   int byteread = 0;
   in = new FileInputStream(fileName);
   ReadFromFile.showAvailableBytes(in);
   // 读入多个字节到字节数组中,byteread为一次读入的字节数
   while ((byteread = in.read(tempbytes)) != -1) {
    System.out.write(tempbytes, 0, byteread);
   }
  } catch (Exception e1) {
   e1.printStackTrace();
  } finally {
   if (in != null) {
    try {
     in.close();
    } catch (IOException e1) {
    }
   }
  }
 }
 
 /**
  * 以字符为单位读取文件,常用于读文本,数字等类型的文件
  */
 public static void readFileByChars(String fileName) {
  File file = new File(fileName);
  Reader reader = null;
  try {
   System.out.println("以字符为单位读取文件内容,一次读一个字节:");
   // 一次读一个字符
   reader = new InputStreamReader(new FileInputStream(file));
   int tempchar;
   while ((tempchar = reader.read()) != -1) {
    // 对于windows下,\r\n这两个字符在一起时,表示一个换行。
    // 但如果这两个字符分开显示时,会换两次行。
    // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
    if (((char) tempchar) != '\r') {
     System.out.print((char) tempchar);
    }
   }
   reader.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  try {
   System.out.println("以字符为单位读取文件内容,一次读多个字节:");
   // 一次读多个字符
   char[] tempchars = new char[30];
   int charread = 0;
   reader = new InputStreamReader(new FileInputStream(fileName));
   // 读入多个字符到字符数组中,charread为一次读取字符数
   while ((charread = reader.read(tempchars)) != -1) {
    // 同样屏蔽掉\r不显示
    if ((charread == tempchars.length)
      && (tempchars[tempchars.length - 1] != '\r')) {
     System.out.print(tempchars);
    } else {
     for (int i = 0; i < charread; i++) {
      if (tempchars[i] == '\r') {
       continue;
      } else {
       System.out.print(tempchars[i]);
      }
     }
    }
   }
 
  } catch (Exception e1) {
   e1.printStackTrace();
  } finally {
   if (reader != null) {
    try {
     reader.close();
    } catch (IOException e1) {
    }
   }
  }
 }
 
 /**
  * 以行为单位读取文件,常用于读面向行的格式化文件
 * @throws Exception 
  */
 public static void readFileByLines(String fileName) {
  File file = new File(fileName);
  BufferedReader reader = null;
  File fileout = new File("C:\\newword.txt");
  FileOutputStream fos = null;
  try {
   System.out.println("以行为单位读取文件内容,一次读一整行:");
   reader = new BufferedReader(new FileReader(file));
   fos= new FileOutputStream(fileout);
   String tempString = null;
   int line = 1;
   // 一次读入一行,直到读入null为文件结束
   while ((tempString = reader.readLine()) != null) {
    // 显示行号
	byte[] bytes = tempString.getBytes();
	int begin = 3;
	byte[] subbytes = new byte[bytes.length-1];
	System.arraycopy(bytes,begin , subbytes, 0, bytes.length-begin);
	 
	System.out.println("line " + line + ": " + subbytes.toString());
	fos.write(subbytes);
	fos.flush();
	
//    System.out.println("line " + line + ": " + tempString);
	
    line++;
   }
   reader.close();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (reader != null) {
    try {
     reader.close();
     fos.close();
    } catch (IOException e1) {
    }
   }
  }
 }
 
 /**
  * 随机读取文件内容
  */
 public static void readFileByRandomAccess(String fileName) {
  RandomAccessFile randomFile = null;
  try {
   System.out.println("随机读取一段文件内容:");
   // 打开一个随机访问文件流,按只读方式
   randomFile = new RandomAccessFile(fileName, "r");
   // 文件长度,字节数
   long fileLength = randomFile.length();
   // 读文件的起始位置
   int beginIndex = (fileLength > 4) ? 4 : 0;
   // 将读文件的开始位置移到beginIndex位置。
   randomFile.seek(beginIndex);
   byte[] bytes = new byte[10];
   int byteread = 0;
   // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
   // 将一次读取的字节数赋给byteread
   while ((byteread = randomFile.read(bytes)) != -1) {
    System.out.write(bytes, 0, byteread);
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (randomFile != null) {
    try {
     randomFile.close();
    } catch (IOException e1) {
    }
   }
  }
 }
 
 /**
  * 显示输入流中还剩的字节数
  */
 private static void showAvailableBytes(InputStream in) {
  try {
   System.out.println("当前字节输入流中的字节数为:" + in.available());
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 public static void main(String[] args) throws Exception {
  String fileName = "C:/word.txt";
  ReadFromFile.readFileByBytes(fileName);
  ReadFromFile.readFileByChars(fileName);
  ReadFromFile.readFileByLines(fileName);
  ReadFromFile.readFileByRandomAccess(fileName);
 }
}

参考以上资料,写出:


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
 * 
 * @ClassName IOTest
 * @Description	
 * @author rk	
 * @Date	2018年11月21日下午7:03:19
 */

public class IOTest {
	public static void main(String[] args) throws Exception {
		
		File file = new File("C:\\word.txt");
		FileInputStream fis = new FileInputStream(file);
		File fileout = new File("C:\\newword.txt");
		FileOutputStream fos = null;
		fos= new FileOutputStream(fileout);
		byte[] d = new byte[3]; 
		fis.read(d, 0, 3);
		byte[] b = new byte[(int) (file.length()-3)];
		fis.read(b);
		fos.write(b);
		fos.flush();
		fis.close();
		fos.close();
	
	}

}

最终版:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

/**
 * 
 * @ClassName ReadFileByLines
 * @Description
 * @author rk
 * @Date 2018年11月21日下午8:20:23
 */

public class ReadFileByLines {
	public static void main(String[] args) throws Exception {
		String fileName = "C:/word.txt";
		File outFileName = new File("C:\\newword.txt");
		ReadFromFile.readFileByLines(fileName);

	}

	public static void readFileByLines(String fileName, String outFileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		File outFile = new File(outFileName);
		FileOutputStream fos = null;
		try {
			System.out.println("以行为单位读取文件内容,一次读一整行:");
			reader = new BufferedReader(new FileReader(file));
			fos = new FileOutputStream(outFile);
			String tempString = null;
			int line = 1;
			// 一次读入一行,直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				byte[] bytes = (tempString+"\r\n").getBytes();
				int begin = 3;
				byte[] subbytes = new byte[bytes.length - begin];
				System.arraycopy(bytes, begin, subbytes, 0, bytes.length - begin);

				System.out.println("line " + line + ": " + Arrays.toString(subbytes));
				fos.write(subbytes);
				fos.flush();

				// System.out.println("line " + line + ": " + tempString);

				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
					fos.close();
				} catch (IOException e1) {
				}
			}
		}
	}

}

最终实现:

问题:生成了一个NUL的不知道的字符,换行符没有了

解决:发现是最后读取为一行后要加"\r\n",截取字节数组的长度是总长度-开始截取的位置长度

问题是解决了,有待优化!(在查找资料中发现有CharArrayReader类,应该也可以实现,不过要考虑换行位置的问题 

以上解决方法是基于Java编写的,个人感觉有Python和C应该可以更好的实现,Python中应该有一些更方便的方法,C里面使用指针,应该可以轻松实现。不过整体思路应该是一样的。

扫描二维码关注公众号,回复: 4419576 查看本文章

版本一:读取一个目录下的所有的文件,然后截取每一行字段后面的字节,最后写入到一个指定文件

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 递归查找一个文件夹
 * @ClassName Recursion
 * @Description	
 * @author rk	
 * @Date	2018年11月26日上午10:56:31
 */
public class RecursionTest {
	
	public static FileOutputStream  fos = null;
	
	public static void main(String[] args) {
		
		fos = connection("C:\\out\\result\\result.txt");
		
		List<File> files =  recursion("C:\\out\\face_data");
		for(File file : files) {
			readFileByLines(file.toString(),114);
		}
		
		close(fos);

	}
	
//	存储指定文件夹的所有的文件路径
	public static List<File> listFiles = new ArrayList<>();	
	/**
	 * 递归查询出该路径下所有文件的路径
	 * @param strPath
	 * @return
	 */
	public static List<File> recursion(String strPath) {
		
		File dir = new File(strPath);
		File[] files = dir.listFiles();
		if(files != null) {
			for(int i = 0; i < files.length; i++) {
				if(files[i].isDirectory()) {
					recursion(files[i].getAbsolutePath());
				}else {
//					System.out.println(files[i].toString());
					listFiles.add(files[i]);
				}
				
			}
			
		}
		return listFiles;	
	}
	
	
	
	
	
	/**
	 * 将指定的文件先按照开始index截取,后输出到fos文件中
	 * @param fileName	输入文件名
	 * @param index	开始截取的位置
	 */
	public static void readFileByLines(String fileName,int index) {
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			System.out.println("以行为单位读取文件内容,一次读一整行:");
			reader = new BufferedReader(new FileReader(file));
			
			String tempString = null;
			int line = 1;
			// 一次读入一行,直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
//				显示行号
				line++;
				
//				添加\r\n进行换行
				byte[] bytes = (tempString+"\r\n").getBytes();
				
				byte[] subbytes = null;
				/**
				 * 判断截取长度是否小于字节数组长度,如果小于,正常处理
				 * 如果大于,跳出循环,进行下一行读取
				 */
				if(bytes.length >= index) {
					subbytes = new byte[bytes.length - index];
//					数组的copy
					System.arraycopy(bytes, index, subbytes, 0, bytes.length - index);
				}else {
					System.out.println("在"+fileName+"中的第"+line+"行,长度小于截取的长度");
					continue;
				}	
				
//				打印到控制台测试
				System.out.println("line " + line + ": " + Arrays.toString(subbytes));
				
//				写入指定位置开始的文件内容
			    fos.write(subbytes);
				
//				写入整个文件内容
//				fos.write(bytes);
			    
//				刷新文件内容
				fos.flush();
			
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} 
		
	}

	
	/**
	 * 获取一个输出流的连接
	 * @return
	 */
	public static FileOutputStream connection(String outFileName) {
		 File outFile = new File(outFileName);
		 FileOutputStream fos = null;
		 try {
			fos = new FileOutputStream(outFile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return fos;
		
	}
	
	
	/**
	 * 关闭一个输出流 fos
	 * @param fos
	 */
	public static void close(FileOutputStream fos) {
		try {
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
}

版本二:文件是二进制文件,没有换行,所以要求是每616byte一条数据,截取104byte后面的512byte数据。

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 递归查找一个文件夹,文件是二进制文件,没有换行,
 * 所以要求是每626byte一条数据,截取114byte后面的512byte数据。
 * @ClassName Recursion
 * @Description	
 * @author rk	
 * @Date	2018年11月26日上午10:56:31
 */
public class RecursionByteTest {
	
	public static FileOutputStream  fos = null;
	
	public static void main(String[] args) {
		//获取输出流
		fos = connection("C:\\out\\result\\human");
		//获取一个目录下的所有文件
		List<File> files =  recursion("C:\\out\\human_data");
		for(File file : files) {
			//每个文件取每一条数据切分,写入到fos
			readFileByBuffer(file.toString(),104,616);
		}
		//关闭输出流
		close(fos);

	}
	
//	存储指定文件夹的所有的文件路径
	public static List<File> listFiles = new ArrayList<>();	
	/**
	 * 递归查询出该路径下所有文件的路径
	 * @param strPath
	 * @return
	 */
	public static List<File> recursion(String strPath) {
		
		File dir = new File(strPath);
		File[] files = dir.listFiles();
		if(files != null) {
			for(int i = 0; i < files.length; i++) {
				if(files[i].isDirectory()) {
					recursion(files[i].getAbsolutePath());
				}else {
//					System.out.println(files[i].toString());
					listFiles.add(files[i]);
				}
				
			}
			
		}
		return listFiles;	
	}

//	统计文件的个数
	public static int file = 1;
//	记录数据条数
	public static int line = 1;
	
	/**
	 * 将指定的文件选取size的长度为一条数据,再按照开始index截取,最后输出到fos文件中
	 * @param fileName	输入文件名
	 * @param index	开始截取的位置
	 * @param size	一条数据的长度
	 */
	public static void readFileByBuffer(String fileName,int index,int size) {
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		try {
//			显示第几个文件
			System.out.println("第"+(file++)+"个文件-------");

			fis = new FileInputStream(fileName);
			bis = new BufferedInputStream(fis,size);
		
			byte[] bytes = new byte[size];	
			byte[] subbytes = new byte[bytes.length - index];
			while(bis.read(bytes) != -1) {
//				显示条数
				System.out.println("正在写入--第"+(line++)+"条数据");
						
//				判断最后一条数据是否是完整的一条数据
				if(isNull(bytes)) {
					System.out.println("写入--第"+(line-1)+"条数据不完整");
				}else {
					
//					数组的copy
					System.arraycopy(bytes, index, subbytes, 0, bytes.length - index);
//					写入指定位置开始的文件内容
					fos.write(subbytes); 
//					刷新文件内容
					fos.flush();
				}
//				重置,更新下一条数据
				bytes = new byte[size];
			}
			
/** 		//直接使用流去访问
			byte[] bytes1 = new byte[size];
			byte[] subbytes1 = new byte[bytes1.length - index];
			while(fis.read(bytes1) != -1) {
				System.arraycopy(bytes1, index, subbytes1, 0, bytes1.length - index);
				fos.write(subbytes1);
				fos.flush();
			}*/
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				bis.close();
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} 
		
	}
	
	/**
	 * 判断最后一个字节数组中是否有空,即不完整的数据
	 * @param bytes
	 * @return
	 */
	public static boolean isNull(byte[] bytes) {
		Boolean bool = null;
		for(byte b : bytes) {
			if(b == 0) {
				bool = true;
			}else {
				bool = false;
			}
		}
		return bool;		
	}
	
	
	
	/**
	 * 获取一个输出流的连接
	 * @return
	 */
	public static FileOutputStream connection(String outFileName) {
		 File outFile = new File(outFileName);
		 FileOutputStream fos = null;
		 try {
			fos = new FileOutputStream(outFile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return fos;
		
	}
	
	
	/**
	 * 关闭一个输出流 fos
	 * @param fos
	 */
	public static void close(FileOutputStream fos) {
		try {
			fos.close();
			System.out.println("写入完成");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	
}

需要注意的是:如果文件最后一条数据是不完整的,将不会写入。此代码要求是严格的数据结构,有不完整的将有错误提示。

猜你喜欢

转载自blog.csdn.net/qq_28844767/article/details/84329069