problem with unicode access
am a new bee in java world i need a simple java program which can able to access one pdf file from one folder and stor开发者_如何学Pythone it in another folder, am facing problem how to read pdf file with non-english file name mean to say if the file name is like this how can i read this file and store it in another folder with same name, this is very urjent requirement for me plz if any one know about this plz send me a code, thanks in advance
First if the only task is to copy file to other folder it does not matter whether the file if PDF or whatever. Open file, read it and writer into other file. If you are using jakarta libraries the task is very simple:
OutputStream out = new FileOutputStream("yourfile.pdf");
IOUtils.copy(new FileInputStream("myfile.pdf"), out);
out.flush();
out.close();
Due to Java uses Unicode for internal representation of string any file name should work here including file name that contains non English characters. But if you are interesting in parsing of the content use one of the open source libraries for PDF parsing (e.g. http://java-source.net/open-source/pdf-libraries).
Where do you retrieve the file name from? I tried the following code, which copies all the files from one directory to another, and does keep the chinese characters.
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
String sourceDirectory = "temp/d1";
String targetDirectory = "temp/d2";
for (File fIn : new File(sourceDirectory).listFiles()) {
File fOut = new File(targetDirectory, fIn.getName());
copy(fIn, fOut);
}
}
private static void copy(File fIn, File fOut) throws FileNotFoundException, IOException {
InputStream in = new BufferedInputStream(new FileInputStream(fIn));
OutputStream out = new BufferedOutputStream(new FileOutputStream(fOut));
try {
byte[] buf = new byte[1024];
int read;
while (-1 != (read = in.read(buf))) {
out.write(buf, 0, read);
}
} finally {
out.flush();
out.close();
in.close();
}
}
}
Maybe you are handling the file name in a way the chinese characters are dropped?
精彩评论