今天遇到了一个极其郁闷的问题,想写一段代码,可以给windows自动安装一种字体。
原理就是将4个ttf字体文件复制到C:\\WINDOWS\\Fonts\\目录下。本来以为很简单,但用java I/O复制过去的字体不能使用(将记事本的字体改成DejaVuSansMono,如果有效果变化,就是正常的),直接手动复制同样的文件过去,就可以使用。不知道问题出在哪里?
哪位朋友帮忙看看,万分感谢,字体文件在附件中,代码如下:
用另一种写法试了下,也是不行,复制过去的文件大小是相同的,用比较工具比较也没问题。
原理就是将4个ttf字体文件复制到C:\\WINDOWS\\Fonts\\目录下。本来以为很简单,但用java I/O复制过去的字体不能使用(将记事本的字体改成DejaVuSansMono,如果有效果变化,就是正常的),直接手动复制同样的文件过去,就可以使用。不知道问题出在哪里?
哪位朋友帮忙看看,万分感谢,字体文件在附件中,代码如下:
package com.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class FontInstall {
public static void main(String[] args) {
try {
String[] fonts = { "DejaVuSansMono-Oblique.ttf",
"DejaVuSansMono-BoldOblique.ttf", "DejaVuSansMono.ttf",
"DejaVuSansMono-Bold.ttf" };
System.out.println();
for (int i = 0; i < fonts.length; i++) {
// Create channel on the source
FileChannel srcChannel = new FileInputStream(System
.getProperty("user.dir")
+ "\\" + fonts[i]).getChannel();
// Create channel on the destination
FileChannel dstChannel = new FileOutputStream(
"C:\\WINDOWS\\Fonts\\" + fonts[i]).getChannel();
// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
// Close the channels
srcChannel.close();
dstChannel.close();
}
} catch (IOException e) {
}
}
}
用另一种写法试了下,也是不行,复制过去的文件大小是相同的,用比较工具比较也没问题。
package com.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FontInstall2 {
public static void main(String[] args) {
try {
String[] fonts = { "DejaVuSansMono-Oblique.ttf",
"DejaVuSansMono-BoldOblique.ttf", "DejaVuSansMono.ttf",
"DejaVuSansMono-Bold.ttf" };
System.out.println();
for (int i = 0; i < fonts.length; i++) {
InputStream in = new FileInputStream(System
.getProperty("user.dir")
+ "\\" + fonts[i]);
OutputStream out = new FileOutputStream("C:\\WINDOWS\\Fonts\\"
+ fonts[i]);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
} catch (IOException e) {
}
}
}