以下は、Blob2Stringで取得したテキストファイルをPDF化にする処理です。
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Convert2Pdf {
public static void main(String[] args) {
final File sqlPdfFile = new File(args[0]);
if (!sqlPdfFile.isFile()) {
System.out.printf("ファイルがありません:%s%n", args[0]);
System.exit(-1);
}
if (sqlPdfFile.length() > 0 ) {
convert2Pdf_fmfcm(sqlPdfFile);
}
else {
System.out.printf("0 Bytesファイル:%s%n", args[0]);
System.exit(-1);
}
}
static String getFileBaseName(String fileName) {
String baseName = "";
int i = fileName.lastIndexOf('.');
if (i > 0) {
baseName = fileName.substring(0, i);
}
return baseName;
}
static String getFileExtName(String fileName) {
String extension = "";
int i = fileName.lastIndexOf('.');
if (i > 0) {
extension = fileName.substring(i + 1);
}
return extension;
}
static final String __header__pdf__ = "25504446";
static final String __header_fmfcm_ = "46434D00";
static String getFileHeader(File sqlTextFile) {
String ret = "";
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(sqlTextFile));
String line = br.readLine();
if (line != null) {
if (line.substring(0, __header__pdf__.length()).equals(__header__pdf__))
ret = "pdf";
else if (line.substring(0, __header_fmfcm_.length()).equals(__header_fmfcm_))
ret = "fmfcm";
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
}
}
}
return ret;
}
static void convert2Pdf_fmfcm(File sqlTextFile) {
BufferedReader br = null;
BufferedOutputStream wr = null;
String path = sqlTextFile.getParent();
String name = sqlTextFile.getName();
if (path == null) path = ".";
String fileExtName = getFileHeader(sqlTextFile);
String newFileName = String.format("%s/%s.%s", path, getFileBaseName(name), fileExtName);
try {
File pdfFile = new File(newFileName);
System.out.printf("Convert to: %s%n", pdfFile.getCanonicalFile().getAbsolutePath());
wr = new BufferedOutputStream(new FileOutputStream(pdfFile));
br = new BufferedReader(new FileReader(sqlTextFile));
String line = "";
while ((line = br.readLine()) != null) {
byte[] bs = str2hex(line);
// System.out.println(int2hex(bs));
wr.write(bs);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (wr != null) {
wr.close();
}
} catch (IOException e) {
}
}
}
static byte[] str2hex(String strInt) {
String safeStr = strInt;
if (strInt.length() % 2 == 1) {
StringBuffer w = new StringBuffer();
w.append(strInt.substring(0, strInt.length()-1));
w.append("0");
w.append(strInt.substring(strInt.length()-1));
safeStr = w.toString();
}
List<Byte> w = new ArrayList<Byte>();
int i = 0;
do {
String s = safeStr.substring(i, i + 2);
Integer n = 0;
try {
n = Integer.parseInt(s, 16);
} catch(NumberFormatException e) {}
byte b = (byte) (0x000000ff & (n));
w.add( b );
i += 2;
} while(i < strInt.length());
Byte[] oa = w.toArray(new Byte[w.size()]);
byte[] bs = new byte[oa.length];
for(int k = 0; k < oa.length; k ++){
bs[k] = oa[k];
}
return bs;
}
static String int2hex(int i) {
String s = Integer.toHexString(i & 0xff);
return (s.length() == 1) ? ("0" + s) : s;
}
static String int2hex(byte[] data) {
StringBuffer buf = new StringBuffer();
int c = 0;
for (byte b : data) {
buf.append(int2hex(b));
c++;
buf.append((c == 8) ? " -" : " ");
buf.append((c >=16) ? "\n" : " ");
c = (c >= 16) ? 0 : c;
}
return buf.toString();
}
}