Untitled

 avatar
unknown
plain_text
6 months ago
13 kB
3
Indexable
package com.fca.rws.util;

import java.io.*;
import java.nio.file.*;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.log4j.Logger;

public class FileUtility
{

    private static final Logger LOGGER = Logger.getLogger(FileUtility.class);
    private static final String ERROR_TEXT = "Error : ";

    public FileUtility()
    {
    }

    public void appendStringToFile(String content, String filepath)
    {
        File file;
       // IOException e;
        file = new File(filepath);
        if(!file.exists())
        {
            try
            {
                if(!file.createNewFile())
                {
                    throw new IOException();
                }
            }
            // Misplaced declaration of an exception variable
            catch(IOException e)
            {
                LOGGER.error((new StringBuilder()).append("Error : ").append(e.getMessage()).toString(), e);
            }
        }
        FileWriter fw;
        Throwable throwable;
        fw = new FileWriter(file.getAbsoluteFile(), true);
        throwable = null;
        BufferedWriter bw;
        Throwable throwable3;
        Exception exception;
        bw = new BufferedWriter(fw);
        throwable3 = null;
        try
        {
            String lines[] = content.split("\n");
            String as[] = lines;
            int i = as.length;
            for(int j = 0; j < i; j++)
            {
                String line = as[j];
                bw.write(line);
                bw.newLine();
            }

        }
        catch(Throwable throwable5)
        {
            throwable3 = throwable5;
            throw throwable5;
        }
        finally
        {
            if(bw == null) goto _L0; else goto _L0
        }
        if(bw != null)
        {
            if(throwable3 != null)
            {
                try
                {
                    bw.close();
                }
                catch(Throwable throwable4)
                {
                    throwable3.addSuppressed(throwable4);
                }
            } else
            {
                bw.close();
            }
        }
        break MISSING_BLOCK_LABEL_237;
        if(throwable3 != null)
        {
            try
            {
                bw.close();
            }
            catch(Throwable throwable6)
            {
                throwable3.addSuppressed(throwable6);
            }
        } else
        {
            bw.close();
        }
        throw exception;
        if(fw != null)
        {
            if(throwable != null)
            {
                try
                {
                    fw.close();
                }
                catch(Throwable throwable1)
                {
                    throwable.addSuppressed(throwable1);
                }
            } else
            {
                fw.close();
            }
        }
        break MISSING_BLOCK_LABEL_360;
        Throwable throwable2;
        throwable2;
        throwable = throwable2;
        throw throwable2;
        Exception exception1;
        exception1;
        if(fw != null)
        {
            if(throwable != null)
            {
                try
                {
                    fw.close();
                }
                catch(Throwable throwable7)
                {
                    throwable.addSuppressed(throwable7);
                }
            } else
            {
                fw.close();
            }
        }
        throw exception1;
        fw;
        LOGGER.error((new StringBuilder()).append("Error : ").append(fw.getMessage()).toString(), fw);
    }

    public void updateStringToFile(String content, String filepath)
    {
        File file;
        IOException e;
        file = new File(filepath);
        if(!file.exists())
        {
            try
            {
                if(!file.createNewFile())
                {
                    throw new IOException();
                }
            }
            // Misplaced declaration of an exception variable
            catch(IOException e)
            {
                LOGGER.error((new StringBuilder()).append("Error : ").append(e.getMessage()).toString(), e);
            }
        }
        FileWriter fw;
        Throwable throwable;
        fw = new FileWriter(file.getAbsoluteFile());
        throwable = null;
        BufferedWriter bw;
        Throwable throwable3;
        Exception exception;
        bw = new BufferedWriter(fw);
        throwable3 = null;
        try
        {
            String lines[] = content.split("\n");
            String as[] = lines;
            int i = as.length;
            for(int j = 0; j < i; j++)
            {
                String line = as[j];
                bw.write(line);
                bw.newLine();
            }

        }
        catch(Throwable throwable5)
        {
            throwable3 = throwable5;
            throw throwable5;
        }
        finally
        {
            if(bw == null) goto _L0; else goto _L0
        }
        if(bw != null)
        {
            if(throwable3 != null)
            {
                try
                {
                    bw.close();
                }
                catch(Throwable throwable4)
                {
                    throwable3.addSuppressed(throwable4);
                }
            } else
            {
                bw.close();
            }
        }
        break MISSING_BLOCK_LABEL_236;
        if(throwable3 != null)
        {
            try
            {
                bw.close();
            }
            catch(Throwable throwable6)
            {
                throwable3.addSuppressed(throwable6);
            }
        } else
        {
            bw.close();
        }
        throw exception;
        if(fw != null)
        {
            if(throwable != null)
            {
                try
                {
                    fw.close();
                }
                catch(Throwable throwable1)
                {
                    throwable.addSuppressed(throwable1);
                }
            } else
            {
                fw.close();
            }
        }
        break MISSING_BLOCK_LABEL_359;
        Throwable throwable2;
        throwable2;
        throwable = throwable2;
        throw throwable2;
        Exception exception1;
        exception1;
        if(fw != null)
        {
            if(throwable != null)
            {
                try
                {
                    fw.close();
                }
                catch(Throwable throwable7)
                {
                    throwable.addSuppressed(throwable7);
                }
            } else
            {
                fw.close();
            }
        }
        throw exception1;
        fw;
        LOGGER.error((new StringBuilder()).append("Error : ").append(fw.getMessage()).toString(), fw);
    }

    public boolean backupFiles(String source, String target, String filename)
    {
        boolean result = false;
        try
        {
            String fileName = (new SimpleDateFormat((new StringBuilder()).append("yyyyMMddhhmm'").append(filename).append("'").toString())).format(new Date());
            java.nio.file.Path from = Paths.get((new StringBuilder()).append(source).append(filename).toString(), new String[0]);
            java.nio.file.Path to = Paths.get((new StringBuilder()).append(target).append(fileName).toString(), new String[0]);
            CopyOption options[] = {
                StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES
            };
            Files.copy(from, to, options);
            result = true;
            LOGGER.info((new StringBuilder()).append("File : ").append(filename).append(", backed up succesfully.").toString());
        }
        catch(IOException ex)
        {
            result = false;
            LOGGER.error(ex.getMessage(), ex);
        }
        return result;
    }

    public boolean backupFilesWithMatchingPattern(String source, String target, String fileNamePattern)
    {
        boolean result = false;
        try
        {
            File file = new File(source);
            File files[] = file.listFiles();
            List lstSelectedFiles = new ArrayList();
            File afile[] = files;
            int i = afile.length;
            for(int j = 0; j < i; j++)
            {
                File f = afile[j];
                if(f.getName().contains(fileNamePattern) && f.getName().contains(".processed"))
                {
                    lstSelectedFiles.add(f);
                }
            }

            File file2;
            for(Iterator iterator = lstSelectedFiles.iterator(); iterator.hasNext(); LOGGER.info((new StringBuilder()).append("File : ").append(file2.getName()).append(", backed up succesfully.").toString()))
            {
                file2 = (File)iterator.next();
                String fileName = (new SimpleDateFormat((new StringBuilder()).append("yyyyMMddhhmm'").append(file2.getName()).append("'").toString())).format(new Date());
                java.nio.file.Path from = Paths.get((new StringBuilder()).append(source).append(file2.getName()).toString(), new String[0]);
                java.nio.file.Path to = Paths.get((new StringBuilder()).append(target).append(fileName).toString(), new String[0]);
                CopyOption options[] = {
                    StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE
                };
                Files.move(from, to, options);
                result = true;
            }

        }
        catch(IOException ex)
        {
            result = false;
            LOGGER.error(ex.getMessage(), ex);
        }
        return result;
    }

    public String readFileContent(String filepath)
    {
        String content;
        IOException e;
        content = null;
        File file = new File(filepath);
        if(!file.exists())
        {
            try
            {
                if(!file.createNewFile())
                {
                    throw new IOException();
                }
            }
            // Misplaced declaration of an exception variable
            catch(IOException e)
            {
                LOGGER.error((new StringBuilder()).append("Error : ").append(e.getMessage()).toString(), e);
            }
        }
        BufferedReader br;
        Throwable throwable;
        Exception exception;
        br = new BufferedReader(new FileReader(filepath));
        throwable = null;
        String sCurrentLine;
        try
        {
            while((sCurrentLine = br.readLine()) != null)
            {
                content = sCurrentLine;
            }
        }
        catch(Throwable throwable2)
        {
            throwable = throwable2;
            throw throwable2;
        }
        finally
        {
            if(br == null) goto _L0; else goto _L0
        }
        if(br != null)
        {
            if(throwable != null)
            {
                try
                {
                    br.close();
                }
                catch(Throwable throwable1)
                {
                    throwable.addSuppressed(throwable1);
                }
            } else
            {
                br.close();
            }
        }
        break MISSING_BLOCK_LABEL_230;
        if(throwable != null)
        {
            try
            {
                br.close();
            }
            catch(Throwable throwable3)
            {
                throwable.addSuppressed(throwable3);
            }
        } else
        {
            br.close();
        }
        throw exception;
        br;
        LOGGER.error((new StringBuilder()).append("Error : ").append(br.getMessage()).toString(), br);
        return content;
    }

    public void deleteFile(String filepath)
    {
        try
        {
            File file = new File(filepath);
            if(file.exists())
            {
                if(!file.delete())
                {
                    throw new IOException();
                }
                LOGGER.info((new StringBuilder()).append("File deleted successfully, filename : ").append(filepath).toString());
            }
        }
        catch(IOException e)
        {
            LOGGER.error((new StringBuilder()).append("Error while deleting unprocessed record, filename : ").append(filepath).toString(), e);
        }
    }

}//remove errors
Editor is loading...
Leave a Comment