| •1 |
|
| 2 |
|
| 3 |
|
| •4 |
|
| 5 |
|
| •6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| •11 |
|
| 12 |
|
| 13 |
|
| •14 |
|
| 15 |
|
| •16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package org.stathissideris.ascii2image.core; |
| 21 |
|
| 22 |
import java.io.File; |
| 23 |
import java.io.FileInputStream; |
| •24 |
import java.io.FileNotFoundException; |
| 25 |
import java.io.IOException; |
| 26 |
import java.io.InputStream; |
| •27 |
import java.io.InputStreamReader; |
| 28 |
import java.io.LineNumberReader; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class FileUtils { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public static String makeTargetPathname(String sourcePathname, String extension, boolean overwrite){ |
| 39 |
return makeTargetPathname(sourcePathname, extension, "", overwrite); |
| 40 |
} |
| 41 |
|
| 42 |
public static String makeTargetPathname(String sourcePathname, String extension, String postfix, boolean overwrite){ |
| 43 |
File sourceFile = |
| 44 |
new File(sourcePathname); |
| 45 |
|
| 46 |
String path = ""; |
| 47 |
if(sourceFile.getParentFile() != null){ |
| 48 |
path = sourceFile.getParentFile().getAbsolutePath(); |
| 49 |
if(!path.endsWith(File.separator)) path += File.separator; |
| 50 |
} |
| 51 |
String baseName = getBaseName(sourceFile.getName()); |
| 52 |
|
| 53 |
String targetName = path + baseName + postfix + "." + extension; |
| 54 |
if(new File(targetName).exists() && !overwrite) |
| 55 |
targetName = makeAlternativePathname(targetName); |
| 56 |
return targetName; |
| 57 |
} |
| 58 |
|
| 59 |
public static String makeAlternativePathname(String pathName){ |
| 60 |
int limit = 100; |
| 61 |
|
| 62 |
for(int i = 2; i <= limit; i++){ |
| 63 |
String alternative = getBaseName(pathName)+"_"+i; |
| 64 |
String extension = getExtension(pathName); |
| 65 |
if(extension != null) alternative += "."+extension; |
| 66 |
if(!(new File(alternative).exists())) return alternative; |
| 67 |
} |
| 68 |
return null; |
| 69 |
} |
| 70 |
|
| 71 |
public static String getExtension(String pathName){ |
| 72 |
if(pathName.lastIndexOf('.') == -1) return null; |
| 73 |
return pathName.substring(pathName.lastIndexOf('.') + 1); |
| 74 |
} |
| 75 |
|
| 76 |
public static String getBaseName(String pathName){ |
| 77 |
if(pathName.lastIndexOf('.') == -1) return pathName; |
| 78 |
return pathName.substring(0, pathName.lastIndexOf('.')); |
| 79 |
} |
| 80 |
|
| 81 |
public static String readFile(File file) throws IOException { |
| 82 |
return readFile(file, null); |
| 83 |
} |
| 84 |
|
| 85 |
public static String readFile(File file, String encoding) throws IOException { |
| 86 |
long length = file.length(); |
| 87 |
|
| 88 |
if (length > Integer.MAX_VALUE) { |
| 89 |
|
| 90 |
|
| 91 |
} |
| •92 |
|
| 93 |
return readFile(new FileInputStream(file), file.getName(), encoding, length); |
| 94 |
} |
| 95 |
|
| 96 |
public static String readFile(InputStream is, String name, String encoding) throws IOException { |
| 97 |
return readFile(is, name, encoding, -1); |
| 98 |
} |
| 99 |
|
| 100 |
public static String readFile(InputStream is, String name, String encoding, long length) throws IOException { |
| 101 |
|
| 102 |
if (length < 0) { |
| 103 |
LineNumberReader reader = new LineNumberReader(new InputStreamReader(is)); |
| 104 |
StringBuilder builder = new StringBuilder(); |
| 105 |
while (true) { |
| 106 |
String line = reader.readLine(); |
| 107 |
if (line == null) break; |
| 108 |
else builder.append(line).append("\n"); |
| 109 |
} |
| 110 |
return builder.toString(); |
| 111 |
} |
| 112 |
|
| 113 |
else { |
| 114 |
|
| 115 |
byte[] bytes = new byte[(int)length]; |
| 116 |
|
| •117 |
|
| 118 |
int offset = 0; |
| 119 |
int numRead = 0; |
| 120 |
while (offset < bytes.length |
| 121 |
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { |
| 122 |
offset += numRead; |
| 123 |
} |
| 124 |
|
| •125 |
|
| 126 |
if (offset < bytes.length) { |
| 127 |
throw new IOException("Could not completely read file "+name); |
| 128 |
} |
| 129 |
|
| •130 |
|
| 131 |
is.close(); |
| 132 |
if(encoding == null){ |
| 133 |
return new String(bytes); |
| •134 |
} else { |
| 135 |
return new String(bytes, encoding); |
| •136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
public static void main(String[] args){ |
| 141 |
System.out.println(makeTargetPathname("C:\\Files\\papar.txt", "jpg", false)); |
| 142 |
System.out.println(makeTargetPathname("C:\\Files\\papar", "jpg", false)); |
| 143 |
System.out.println(makeTargetPathname("papar.txt", "jpg", false)); |
| 144 |
System.out.println(makeTargetPathname("/home/sideris/tsourekia/papar.txt", "jpg", false)); |
| 145 |
System.out.println(makeTargetPathname("D:\\diagram.max", "jpg", false)); |
| 146 |
System.out.println(makeAlternativePathname("C:\\Files\\papar.txt")); |
| 147 |
System.out.println(makeAlternativePathname("C:\\Files\\papar")); |
| 148 |
System.out.println(getExtension("pipi.jpeg")); |
| 149 |
System.out.println(getExtension("pipi")); |
| 150 |
} |
| 151 |
} |