package com.unimlink.demo.qartz.utils; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.List; /** * @Description * @Date XXX * @Created by XXX */ public class FileTransUtil { public static void textToBinary(String binaryFilePath, String textFilePath) throws IOException { //String binaryFilePath="D://test//222//test.xlsx"; // String textFilePath="D://test//222//test.txt"; Path pathIn = Paths.get(textFilePath); File textFile = new File(binaryFilePath); if (!textFile.exists()) { textFile.createNewFile(); } Path pathOut = Paths.get(binaryFilePath); FileChannel inputChannel = FileChannel.open(pathIn, StandardOpenOption.READ); FileChannel outputChannel =FileChannel.open(pathOut,StandardOpenOption.WRITE); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); List<Byte> byteList = new ArrayList<>(); while (true){ byteBuffer.clear(); int read = inputChannel.read(byteBuffer); if(-1 == read){//此处用-1判断是否读完需要依赖上面的 byteBuffer.clear(); //文件读完了 break; } //读到文件中内容了,翻转缓冲区,准备get其中数据 byteBuffer.flip();//本例中从ByteBuffer获取数据没有显示的get,而是将其作为参数传给另一个Channel的write方法,但是也是读其中的数据,所以也要进行翻转 while(byteBuffer.hasRemaining()){ byteList.add(byteBuffer.get()); } } byte[] bytes = new byte[byteList.size()]; for (int ii=0;ii<bytes.length;ii++){ bytes[ii] = byteList.get(ii); } //1、这里的data,是A,B,C...的字符串形式,也就是原始二进制文件的byte数组的字符串逗号分割形式 String data = new String(bytes, Charset.defaultCharset());//这一步是通过文本的字节数组形式,获取文本内容//2、将文本文件中字节还原成A,B,C,....形式,这里相当于decode,解码字符集要与编码保持一致 //3、下面是还原原二进制文件的字节数组:byte2s String[] dataspliet = data.split(","); byte[] byte2s = new byte[dataspliet.length]; for (int ii=0;ii<byte2s.length;ii++){ byte2s[ii] = Byte.valueOf(dataspliet[ii]); } //4、下面是根据原二进制文件字节数组封装ByteBuffer ByteBuffer byteBufferWrite = ByteBuffer.allocate(byte2s.length); byteBufferWrite.put(byte2s); byteBufferWrite.flip(); //5、将字节数组写入outputChannel outputChannel.write(byteBufferWrite);//将byteBuffer中内容写入到Channel中 // outputChannel.write(byteTemp);//将byteBuffer中内容写入到Channel中 inputChannel.close(); outputChannel.close(); } public static void binaryToText(String binaryFilePath, String textFilePath) throws IOException { //String binaryFilePath="D://test//111//test.xlsx"; //String textFilePath="D://test//111//test.txt"; Path pathIn = Paths.get(binaryFilePath); File textFile = new File(textFilePath); if (!textFile.exists()) { textFile.createNewFile(); } Path pathOut = Paths.get(textFilePath); FileChannel inputChannel = FileChannel.open(pathIn, StandardOpenOption.READ); FileChannel outputChannel =FileChannel.open(pathOut,StandardOpenOption.WRITE); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); while (true){ byteBuffer.clear(); int read = inputChannel.read(byteBuffer); if(-1 == read){//此处用-1判断是否读完需要依赖上面的 byteBuffer.clear(); //文件读完了 break; } //读到文件中内容了,翻转缓冲区,准备get其中数据 byteBuffer.flip();//本例中从ByteBuffer获取数据没有显示的get,而是将其作为参数传给另一个Channel的write方法,但是也是读其中的数据,所以也要进行翻转 StringBuilder builder = new StringBuilder(); while (byteBuffer.hasRemaining()){ builder.append(byteBuffer.get()+",");//1、将bytebuffer中数据,以字节形式取出,每个字节后追加逗号,形成字符串,以便解析字符串时时按,分割还原字节 } CharBuffer charBuffer = CharBuffer.allocate(builder.length());//2、定义一个上面生成字符串长度的ChatBuffer charBuffer.put(builder.toString());//3、将字符串写入CharBuffer(A,B,C,D....) charBuffer.flip(); Charset charset= Charset.defaultCharset();//4、将CharBuffer数据准备写入outputChannel,写入之前,需要转换成ByteBuffer,转换时需要按照特定字符集编码形式进行转换 ByteBuffer byteBuffe1r=charset.encode(charBuffer); // byteBuffe1r.flip(); outputChannel.write(byteBuffe1r);//将byteBuffer中内容写入到Channel中//5、将CharBuffer的ByteBuffer数组写入outputChannel // outputChannel.write(byteTemp);//将byteBuffer中内容写入到Channel中 } inputChannel.close(); outputChannel.close(); } }
【无标题】记录将二进制与文本文件互转
于 2023-03-13 15:31:38 首次发布