How can I normalize the EOL character in Java?

后端 未结 8 980
醉话见心
醉话见心 2020-12-29 03:13

I have a linux server and many clients with many operating systems. The server takes an input file from clients. Linux has end of line char LF, while Mac has end of line cha

8条回答
  •  心在旅途
    2020-12-29 03:23

    solution to change the file ending with recursive search in path

    package handleFileLineEnd;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.FileSystems;
    import java.nio.file.Files;
    import java.nio.file.OpenOption;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.List;
    
    import sun.awt.image.BytePackedRaster;
    
    public class handleFileEndingMain {
    
        static int carriageReturnTotal;
        static int newLineTotal;
    
        public static void main(String[] args)  throws IOException
        {       
            processPath("c:/temp/directories");
    
            System.out.println("carriageReturnTotal  (files have issue): " + carriageReturnTotal);
    
            System.out.println("newLineTotal: " + newLineTotal);
        }
    
        private static void processPath(String path) throws IOException
        {
            File dir = new File(path);
            File[] directoryListing = dir.listFiles();
    
            if (directoryListing != null) {
                for (File child : directoryListing) {
                    if (child.isDirectory())                
                        processPath(child.toString());              
                    else
                        checkFile(child.toString());
                }
            } 
    
    
        }
    
        private static void checkFile(String fileName) throws IOException
        {
            Path path = FileSystems.getDefault().getPath(fileName);
    
            byte[] bytes= Files.readAllBytes(path);
    
            for (int counter=0; counter

提交回复
热议问题