相关文章推荐
有胆有识的槟榔  ·  如何通过Go SDK ...·  2 天前    · 
伤情的消防车  ·  定义 NMAKE 宏 | ...·  9 小时前    · 
阳刚的路灯  ·  String.raw() - ...·  9 小时前    · 
阳刚的红茶  ·  android 图片加载库 Glide ...·  4 小时前    · 
沉着的水桶  ·  HTMLElement:dragleave ...·  1 年前    · 
斯文的剪刀  ·  java - ...·  1 年前    · 
怕老婆的水龙头  ·  VRAY正交相机_unity ...·  2 年前    · 

小编上次文件丢失 电脑硬盘分区删了格式化了文件如何恢复 ,硬盘数据恢复后,找回的文件最后一行有NUL字符,想着怎么去掉,因为文件很多,就写了个java处理程序,处理掉NUL部分主要参考 java删除文本文件最后一行为NUL的字符 ,这里介绍的是删除最后一样,但是考虑到我的文件最后一样有正确文本和NUL掺杂的情况,于是对代码做了一些修改。
有这样的:
在这里插入图片描述
也有这样的
在这里插入图片描述
好在NUL字符都在文件末尾,恢复后这些文件都变成了ANSI编码,小编当时写的时候是UTF-8编码
在这里插入图片描述

import java . io . * ; import java . nio . file . Files ; import java . nio . file . Paths ; import java . util . ArrayList ; import java . util . List ; import java . util . concurrent . Executors ; import java . util . stream . Stream ; public class DelNul { // static String src = "E:\\abc\\"; static String src = "E:\\abc\\" ; // 路径最后必须带/ static long maxDateTime = dateToStamp ( "2022-02-21 00:00:00" ) ; public static void main ( String args [ ] ) throws Exception { processDirMain ( ) ; //processFile(); public static void processFile ( ) { File strFile = new File ( "E:\\abc\\consumer-rules.pro" ) ; // DateUtils.stampToDate(strFile.lastModified()); if ( strFile . exists ( ) && strFile . lastModified ( ) < maxDateTime delNulEndFile ( strFile ) ; } else { System . out . println ( "不符合" ) ; public static boolean isNeedProcessDir ( File strFile ) { if ( strFile == null || ! strFile . exists ( ) || ! strFile . isDirectory ( ) ) { return false ; String fileNme = strFile . getName ( ) ; if ( strFile . isHidden ( ) //||strFile.equals("out") //||strFile.equals("build") //||strFile.startsWith(".") || fileNme . equals ( ".gradle" ) || fileNme . equals ( ".idea" ) || fileNme . equals ( "build" ) //||strFile.equals(".settings") || fileNme . equals ( ".metadata" ) System . err . println ( strFile . getAbsolutePath ( ) + "不处理" ) ; return false ; } else { return true ; public static boolean isNeedProcessFile ( File strFile ) { if ( strFile == null || ! strFile . exists ( ) || ! strFile . isFile ( ) ) { return false ; String fileNme = strFile . getName ( ) ; if ( strFile . lastModified ( ) >= maxDateTime || strFile . isHidden ( ) || fileNme . endsWith ( ".rar" ) || fileNme . endsWith ( ".zip" ) || fileNme . endsWith ( ".7z" ) || fileNme . endsWith ( ".gz" ) || fileNme . endsWith ( ".rpm" ) || fileNme . endsWith ( ".exe" ) || fileNme . endsWith ( ".doc" ) || fileNme . endsWith ( ".jar" ) || fileNme . endsWith ( ".aar" ) || fileNme . endsWith ( ".so" ) ) { System . err . println ( strFile . getAbsolutePath ( ) + "不处理" ) ; return false ; } else { return true ; public static void processDirMain ( ) throws IOException { File filesrc = new File ( src ) ; if ( ! filesrc . exists ( ) ) { System . out . println ( "目标文件不存在!" ) ; return ; File [ ] file = filesrc . listFiles ( ) ; if ( file == null ) { System . out . println ( "目录为空" ) ; return ; System . out . println ( "******开始处理******" ) ; java . util . Date now = new java . util . Date ( ) ; java . text . SimpleDateFormat dateFormat = new java . text . SimpleDateFormat ( "yyyy/MM/dd HH:mm:ss" ) ; // 可以方便地修改日期格式 String hehe = dateFormat . format ( now ) ; System . out . println ( hehe ) ; for ( int i = 0 ; i < file . length ; i ++ ) { System . out . println ( file [ i ] . getName ( ) + " 开始处理!" ) ; if ( isNeedProcessFile ( file [ i ] ) ) { delNulEndFile ( file [ i ] ) ; System . out . println ( file [ i ] . getName ( ) + " 文件处理完成!" + "这是第" + i + "个,共" + file . length ) ; } // file[i].getPath().indexOf("/.") == -1 else if ( isNeedProcessDir ( file [ i ] ) ) { System . out . println ( file [ i ] . getName ( ) + " 目录开始处理!" ) ; String sourceDir = src + File . separator + file [ i ] . getName ( ) ; processDirectiory ( sourceDir ) ; System . out . println ( file [ i ] . getName ( ) + " 目录处理完成!" + "这是第" + i + "个,共" + file . length ) ; System . out . println ( "############全部处理结束!############" ) ; java . util . Date now2 = new java . util . Date ( ) ; java . text . SimpleDateFormat dateFormat2 = new java . text . SimpleDateFormat ( "yyyy/MM/dd HH:mm:ss" ) ; // 可以方便地修改日期格式 String hehe2 = dateFormat2 . format ( now2 ) ; System . out . println ( hehe2 ) ; public static void processDirectiory ( String sourceDir ) throws IOException { File [ ] file = ( new File ( sourceDir ) ) . listFiles ( ) ; for ( int i = 0 ; i < file . length ; i ++ ) { if ( isNeedProcessFile ( file [ i ] ) ) { File sourceFile = file [ i ] ; delNulEndFile ( sourceFile ) ; } else if ( isNeedProcessDir ( file [ i ] ) ) { String dir1 = sourceDir + File . separator + file [ i ] . getName ( ) ; System . out . println ( dir1 + "开始处理" ) ; processDirectiory ( dir1 ) ; * https://www.cnblogs.com/baby123/p/12706280.html * @param fileName * @throws Exception public static void delNulEndFile ( File srcfile ) { Executors . newCachedThreadPool ( ) . execute ( new Runnable ( ) { @Override public void run ( ) { // TODO Auto-generated method stub if ( srcfile == null || ! srcfile . exists ( ) ) { System . err . println ( "delNulEndFile error" ) ; return ; System . out . println ( srcfile . getAbsolutePath ( ) ) ; try { RandomAccessFile file = new RandomAccessFile ( srcfile , "rw" ) ; long len = file . length ( ) ; if ( len == 0 ) { System . err . println ( "len is 0,not process" ) ; file . close ( ) ; return ; long start = file . getFilePointer ( ) ; long nextend = start + len - 1 ; int i = - 1 ; // 移动指针 file . seek ( nextend ) ; byte [ ] buf = new byte [ 1 ] ; boolean isDelete = false ; while ( nextend > start ) { i = file . read ( buf , 0 , 1 ) ; // System.out.println(buf[0]); if ( buf [ 0 ] != 0 ) { isDelete = true ; // 删除最后的NUL字符 if ( isDelete ) { file . setLength ( nextend + 1 ) ; System . out . println ( srcfile . getAbsolutePath ( ) + "..." ) ; break ; nextend -- ; //System.out.println(nextend); file . seek ( nextend ) ; file . close ( ) ; } catch ( Exception e ) { // TODO Auto-generated catch block System . err . println ( e ) ; e . printStackTrace ( ) ; } ) ; * https://www.cnblogs.com/baby123/p/12706280.html * @param fileName * @throws Exception public static void delNulAtLastLine ( String fileName ) throws Exception { RandomAccessFile file = new RandomAccessFile ( fileName , "rw" ) ; long len = file . length ( ) ; long start = file . getFilePointer ( ) ; long nextend = start + len - 1 ; int i = - 1 ; // 移动指针 file . seek ( nextend ) ; byte [ ] buf = new byte [ 1 ] ; boolean isDelete = true ; while ( nextend > start ) { i = file . read ( buf , 0 , 1 ) ; System . out . println ( buf [ 0 ] ) ; if ( buf [ 0 ] == 0 ) { isDelete = true ; if ( buf [ 0 ] == '\r' ) { // 删除最后一行 if ( isDelete ) { file . setLength ( nextend - start ) ; break ; nextend -- ; file . seek ( nextend ) ; file . close ( ) ; // 将时间转换为时间戳 public static long dateToStamp ( String s ) { String res = "" ; // 设置时间格式,将该时间格式的时间转换为时间戳 SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ; Date date ; try { date = simpleDateFormat . parse ( s ) ; long time = date . getTime ( ) ; //res = String.valueOf(time); System . out . println ( time ) ; return time ; } catch ( ParseException e ) { // TODO Auto-generated catch block e . printStackTrace ( ) ; return System . currentTimeMillis ( ) ;

对于大量文件的处理还是很耗时的,博主只是在处理的时候用了多线程,文件夹遍历的时候并没有使用多线程,后续有时间看看能不能优化,尽量缩短处理时间
参考
java删除文本文件最后一行为NUL的字符
Notepad++中如何替换掉NUL不可见字符【原创】

小编上次文件丢失,硬盘数据恢复后,找回的文件最后一行有NUL字符,想着怎么去掉,因为文件很多,就写了个java处理程序,主要参考java删除文本文件最后一行为NUL的字符,这里介绍的是删除最后一样,但是考虑到我的文件最后一样有正确文本和NUL掺杂的情况,于是对代码做了一些修改。有这样的:也有这样的好在NUL字符都在文件末尾,恢复后这些文件都变成了ANSI编码,小编当时写的时候是UTF-8编码import java.io.*;import java.nio.file.Files;impor
nul l 空 就好像一个杯子里面式真空的."" 字符 串空 就像杯子在那里 但是里面有空气]而 0 是指一个数值类型的变量 在初始化后 并没有赋值 则这个数值型变量就的默认值是 00或者 是一个被赋值的变量 转载于:https://www.cnblogs.com/thomasbc/p/6687425.html...
在做加解密的时候,遇到了一个问题:解密出来的 字符 间,不可预期地出现了一些 NUL L域,如图 输出: 图 红框 NUL 输出就是这些 NUL L域,这些 NUL L域影响了业务的后续处理,所以必须 去除 这些 NUL 。 研究了一下这个输出,不难发现这些 NUL L输出,在 字符 串的字节数组里很容易发现,这些 NUL L也是占“地儿”的。所以只需要将这些"地儿" 去除 :去掉ascii码值为0的比特。 * gz 文件 是linux下常见的压缩格式。使用 java .util.zip.GZIPInputStream即可,压缩是 java .util.zip.GZIPOutputStream 压缩为.gz 文件 java 代码如下,经本地测试未发现问题。 public static void compressFile(String inFileName) {
nul 文件 删不掉, 文件 夹无法删除出现无法删除 nul 参数不正确,MS-DOS命令无效,等各种难以删除的 文件 或者 文件 夹。 1、出现上面的情况,主要是创建了特殊的 文件 文件 夹,使用了windows系统 关键字,导致常规操作已经无法删除,即使使用右击管理员权限删除也不行。 2、解决方法: 1)搜索 CMD 命令窗口,右击以管理员身份运行; 2)输入:rd /s \.\要删除的 文件 路径 3)按回车键,提示是否删除,输入 y 并回车,即可进行删除。 官网首页:http://www.52014991.xyz/hom