download::GO development engineer: welcome the rising tuyere, step into the blue ocean industry!

To change this license header, choose License Headers in Project Properties.

To change this template file, choose Tools | Templates

and open the template in the editor.

*/

package datei.steuern;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.RandomAccessFile;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import java.util.logging.Level;

import java.util.logging.Logger;

/ * * *

@author s.watson

*/

public class FileTools {

public FileTools() {

}

/ * *

Public static String formatPath(String path) {

return path.replaceAll(“\\”, “/”);

}

/ * *

Private static String combainPath(String zwei, String zwei) {private static String combainPath(String zwei, String zwei) {

String dori = “”; eins = null == eins ? “” : formatPath(eins); zwei = null == zwei ? “” : formatPath(zwei); if (! eins.endsWith(“/”) && zwei.indexOf(“/”) ! = 0) { dori = eins + “/” + zwei; } else { dori = (eins + zwei).replaceAll(“//”, “/”); } return dori; }

/ * *

Private static String[] list2Array(list list) {@param list @return */ private static String[] list2Array(list list) {

String array[] = (String[]) list.toArray(new String[list.size()]);

return array;

}

/ * *

Public static List

cp(String source, String source, String source, String source, String source, String source, String source) String destination, boolean loop) {

List<File> list = new ArrayList();

try {

File srcFile = new File(source);

File desFile = new File(destination);

list.addAll(cp(srcFile, desFile, loop));

} catch (Exception ex) {

j2log(null, null, ex);

}

return list;

}

/ * *

Public static List

cp(File source, File source, File source) File destination, boolean loop) {

List

list = new ArrayList(); try { if (! source.exists() || source.isDirectory()) {

throw new FileNotFoundException();

}

list.add(cp(source, destination));

if (loop) {

String[] subFile = source.list(); for (String subPath : subFile) { String src = combainPath(source.getPath(), subPath); String des = combainPath(Destination.getPath (), subPath); // File subfile = new File(SRC); if (subfile.isFile()) { list.add(cp(src, des)); } else if (subfile.isDirectory() && loop) { list.addAll(cp(src, des, loop)); }}

}

} catch (Exception ex) {

j2log(null, null, ex);

}

return list;

}

/ * *

* @param source @param destination @return */ public static File cp(String source, String destination) {

File desFile = null;

try {

File srcFile = new File(source);

desFile = new File(destination);

desFile = cp(srcFile, desFile);

} catch (Exception ex) {

j2log(null, null, ex);

}

return desFile;

}

/ * *

* @param source @param destination @return */ public static File cp(File source, File destination) {

FileInputStream in = null; FileOutputStream out = null; FileChannel inc = null; FileChannel outc = null; try { if (! source.exists() || source.isDirectory()) {

throw new FileNotFoundException();

}

if (source.getPath().equals(destination.getPath())) {

return source;

} long allbytes = du(source, false); if (! destination.exists()) {

destination.createNewFile();

} in = new FileInputStream(source.getPath()); out = new FileOutputStream(destination); inc = in.getChannel(); outc = out.getChannel(); ByteBuffer byteBuffer = null; long length = 2097152; // Default 2M long _2M = 2097152; while (inc.position() < inc.size()) {

If (allBytes > (64 * length)) {// If the file size is larger than 128M, change the cache to 64M length = 32 * _2M; } else if (allBytes > (32 * length)) {// If the file size is larger than 64M, change the cache to 32M length = 16 * _2M; } else if (allBytes > (16 * length)) {// If the file size is larger than 32M, change the cache to 16M length = 8 * _2M; } else if (allBytes > (8 * length)) {// If the file size is larger than 16M, change the cache to 8M length = 4 * _2M; } else if (allBytes > (4 * length)) {// If the file size is larger than 8M, change the cache to 4M length = 2 * _2M; } else if (allBytes > (2 * length)) {// If the file size is larger than 4M, change the cache to 2M length = _2M; } else if (allBytes > (length)) {// If the file size is larger than 2M, change the cache to 1M length = _2M / 2; } else if (allBytes < Length) {// If the file is smaller than the base size, print length = allBytes; } allbytes = inc.size() - inc.position(); byteBuffer = ByteBuffer.allocateDirect((int) length); inc.read(byteBuffer); byteBuffer.flip(); outc.write(byteBuffer); outc.force(false);

}

} catch (Exception ex) {

j2log(null, null, ex);

} finally {

try {

if (null ! = inc) { inc.close(); } if (null ! = outc) { outc.close(); } if (null ! = in) { in.close(); } if (null ! = out) { out.close(); }

} catch (Exception ex) {

j2log(null, null, ex);

}

}

return destination;

}

/ * *

@param from @param to @return */ public static File rename(String filePath, String from, String from, String to) {

File newFile = null;

try {

File oldFile = new File(combainPath(filePath, from));

newFile = new File(combainPath(filePath, to));

rename(newFile, oldFile);

} catch (Exception ex) {

j2log(null, null, ex);

}

return newFile;

}

/ * *

Public static File rename(File from, File to) {@param to @param from @return */ public static File rename(File from, File to) {

try { String newPath = to.getPath(); String oldPath = from.getPath(); if (! oldPath.equals(newPath)) {

if (! to.exists()) { from.renameTo(to); }

}

} catch (Exception ex) {

j2log(null, null, ex);

}

return to;

}

/ * *

* @Param fileName @Param source @Param destination @Param cover */ public static void mV (String fileName, String fileName, String fileName, String fileName, String fileName) String source, String destination, boolean cover) {

try { if (! source.equals(destination)) {

File oldFile = new File(combainPath(source, fileName));
File newFile = new File(combainPath(destination, fileName));
mv(oldFile, newFile, cover);

} } catch (Exception ex) { j2log(null, null, ex); }}

/ * *

@param destination @param cover */ public static void mV (String source, String destination, String destination, boolean cover) {

try { if (! source.equals(destination)) {

File oldFile = new File(source);
File newFile = new File(destination);
mv(oldFile, newFile, cover);

} } catch (Exception ex) { j2log(null, null, ex); }}

/ * *

@param destination @param cover */ public static void mv(File source, File destination, boolean cover) {

try { if (! source.exists()) {

throw new FileNotFoundException();

} StringBuilder fileName = new StringBuilder(source.getName()); if (! cover && source.getPath().equals(destination.getPath())) {

If (filename.indexOf (".") > 0) {filename.insert (filename.lastIndexOf ("."), "_ copy "); } else {filename.append ("_ copy "); } cp(source, new File(combainPath(source.getParent(), fileName.toString())));

} else {

source.renameTo(destination);

} } catch (Exception ex) { j2log(null, null, ex); }}

/ * *

@Param FilePath @Param FileName @Return */ private static String[] String FilePath, String fileName) {

String[] extension = {};

try {

String fullPath = combainPath(filePath, fileName);

File file = new File(fullPath);

extensions(file);

} catch (Exception ex) {

j2log(null, null, ex);

}

return extension;

}

/ * *

* @param fullPath @return */ private static String[] Extensions (String fullPath) {

String[] extension = {};

try {

File file = new File(fullPath);

extensions(file);

} catch (Exception ex) {

j2log(null, null, ex);

}

return extension;

}

/ * *

* @Param File @Return */ private static String[] Extensions (File File) {

String[] extension = {};

try {

if (file.isFile()) {

String fileName = file.getName(); if (fileName.lastIndexOf(".") >= 0) { int lastIndex = fileName.lastIndexOf("."); extension[0] = String.valueOf(lastIndex); // The ". "of the extension Extension [1] = filename. substring(lastIndex + 1); // extension[2] = filename. substring(0, lastIndex); // filename}

}

} catch (Exception ex) {

j2log(null, null, ex);

}

return extension;

}

/ * *

Public static List

List<File> list = new ArrayList();

try {

File file = new File(filePath);

list.addAll(ls(file, loop));

} catch (Exception ex) {

j2log(null, null, ex);

}

return list;

}

/ * *

* @param file @param loop @return */ public static List< file bb0 ls(file file, Boolean loop) {

List

list = new ArrayList(); try { list.add(file); if (! file.isDirectory()) {

list.add(file);

} else if (file.isDirectory()) {

File[] subList = file.listFiles(); subList = filesSort(subList, true); for (File subFile : subList) { if (subFile.isDirectory() && loop) { list.addAll(ls(subFile.getPath(), loop)); } else { list.add(subFile); }}

}

} catch (Exception ex) {

j2log(null, null, ex);

}

return list;

}

/ * *

* @Param parentPath @Param subList @return */ private static File[] filesSort(File[] inFiles, boolean asc) {

List<String> files = new ArrayList();

List<String> dirs = new ArrayList();

for (File subFile : inFiles) {

if (subFile.isDirectory()) {

dirs.add(subFile.getPath());

} else if (subFile.isFile()) {

files.add(subFile.getPath());

} } String[] fileArray = {}; if (files.size() > 0) { fileArray = list2Array(files); Arrays.sort(fileArray); if (! asc) {

Arrays.sort(fileArray, Collections.reverseOrder());

} } String[] dirArray = {}; if (dirs.size() > 0) { dirArray = list2Array(dirs); Arrays.sort(dirArray); if (! asc) {

Arrays.sort(dirArray, Collections.reverseOrder());

}

}

return concat2FileArray(fileArray, dirArray);

}

/ * *

Concat2FileArray merge File * @ param array old1 @ param old2 @ return * / private static File [] concat2FileArray (String [] old1, String[] old2) {

File[] newArray = new File[old1.length + old2.length];

for (int i = 0, n = old1.length; i < n; i++) {

newArray[i] = new File(old1[i]);

}

for (int i = 0, j = old1.length, n = (old1.length + old2.length); j < n; i++, j++) {

newArray[j] = new File(old2[i]);

}

return newArray;

}

/ * *

Public static StringBuilder read(String) public static StringBuilder read(String) public static StringBuilder read(String) public static StringBuilder read(String) public static StringBuilder read(String filePath, String fileName, String charset) {

StringBuilder sb = new StringBuilder();

try {

String fullPath = combainPath(filePath, fileName);

File file = new File(fullPath);

sb.append(FileTools.tail(file, false, 0, charset));

} catch (Exception ex) {

j2log(null, null, ex);

}

return sb;

}

/ * *

Public static StringBuilder read(String fullPath, String fullPath, String fullPath) public static StringBuilder read(String fullPath, String fullPath, String fullPath) String charset) {

StringBuilder sb = new StringBuilder();

try {

File file = new File(fullPath);

sb.append(FileTools.tail(file, false, 0, charset));

} catch (Exception ex) {

j2log(null, null, ex);

}

return sb;

}

/ * *

Public static StringBuilder read(file file, String charset) {public static StringBuilder read(file file, String charset) {public static StringBuilder read(file file, String charset) {public static StringBuilder read(file file, String charset) {

StringBuilder sb = new StringBuilder();

try {

sb.append(FileTools.tail(file, false, 0, charset));

} catch (Exception ex) {

j2log(null, null, ex);

}

return sb;

}

/ * *

Public static StringBuilder public static StringBuilder public static StringBuilder public static StringBuilder public static StringBuilder public static StringBuilder find(String filePath, String fileName, int line, String charset) {