unicode 访问问题

发布于 2024-10-04 17:24:46 字数 169 浏览 3 评论 0原文

我是java世界中的一只新蜜蜂,我需要一个简单的java程序,它能够从一个文件夹访问一个pdf文件并将其存储在另一个文件夹中,我面临着如何读取具有非英文文件名的pdf文件的问题意味着如果文件名是这样的,我如何读取这个文件并将其存储在另一个同名的文件夹中,这对我来说是非常迫切的要求,如果有人知道这个请给我发一个代码,预先感谢

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 store 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随遇而安 2024-10-11 17:24:46

首先,如果唯一的任务是将文件复制到其他文件夹,则文件是 PDF 还是其他文件并不重要。打开文件,读取它并写入其他文件。如果您使用 jakarta 库,任务非常简单:

OutputStream out = new FileOutputStream("yourfile.pdf");
IOUtils.copy(new FileInputStream("myfile.pdf"), out);
out.flush();
out.close();

由于 Java 使用 Unicode 进行字符串的内部表示,任何文件名都应该在这里工作,包括包含非英语字符的文件名。
但是,如果您对解析内容感兴趣,请使用开源库之一进行 PDF 解析(例如 http://java-source.net/open-source/pdf-libraries)。

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).

别念他 2024-10-11 17:24:46

您从哪里检索文件名?我尝试了以下代码,它将所有文件从一个目录复制到另一个目录,并保留中文字符。

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();
    }
}

也许

您正在以删除中文字符的方式处理文件名?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文