尝试创建一个文件来保存图像Java

发布于 2024-11-18 04:37:01 字数 1910 浏览 4 评论 0原文

尝试使用 Java 在 Windows 7 上创建文件时出现以下异常。路径示例为“C:/g-ecx/images-amazon/com/images/G/01/gno/images/orangeBlue/navPackedSprites-US-22.V183711641.png”。如果我在路径中进行硬编码,它确实可以工作。我的头已经敲了两个小时了,谁能帮帮我?

mkdir 失败但没有抛出异常,create file 会抛出异常。

java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at org.willmanning.mtt.html.processingbehavior.ImageProcessingBehavior.processImage(ImageProcessingBehavior.java:125)
at org.willmanning.mtt.html.processingbehavior.ImageProcessingBehavior.loadImages(ImageProcessingBehavior.java:99)
at org.willmanning.mtt.html.processingbehavior.ImageProcessingBehavior.processNodes(ImageProcessingBehavior.java:66)
at org.willmanning.mtt.html.processingbehavior.ImageProcessingBehavior.processRootNode(ImageProcessingBehavior.java:34)
at org.willmanning.mtt.html.ParsingFacade.processURL(ParsingFacade.java:38)
at org.willmanning.mtt.App.main(App.java:45)



 /**
 * 
 * @param image
 * @param url
 */
public void processImage(BufferedImage image, URL url) {

    StringBuilder path = new StringBuilder();
    path.append("C:/Users/will/Documents/");
    path.append(url.getHost().replace('.', '/'));
    path.append(url.getFile());
    path.replace(path.lastIndexOf("."), path.length(), ".txt");

    File file = new File(path.toString());

    boolean mkdir = file.mkdir();

    boolean isNew = false;
    try {
        isNew = file.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    /*
     * only create the file if it doesn't exist
     */
    if (isNew) {
        try {
            ImageIO.write(image, "jpg", file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I get the following exception when trying to create a file on windows 7 using Java. An example of a path is "C:/g-ecx/images-amazon/com/images/G/01/gno/images/orangeBlue/navPackedSprites-US-22.V183711641.png". If I hard code in a path it does work however. I've been banging my head for two hours, can anyone help.

mkdir fails but doesn't through an exception, create file throws the exception.

java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at org.willmanning.mtt.html.processingbehavior.ImageProcessingBehavior.processImage(ImageProcessingBehavior.java:125)
at org.willmanning.mtt.html.processingbehavior.ImageProcessingBehavior.loadImages(ImageProcessingBehavior.java:99)
at org.willmanning.mtt.html.processingbehavior.ImageProcessingBehavior.processNodes(ImageProcessingBehavior.java:66)
at org.willmanning.mtt.html.processingbehavior.ImageProcessingBehavior.processRootNode(ImageProcessingBehavior.java:34)
at org.willmanning.mtt.html.ParsingFacade.processURL(ParsingFacade.java:38)
at org.willmanning.mtt.App.main(App.java:45)



 /**
 * 
 * @param image
 * @param url
 */
public void processImage(BufferedImage image, URL url) {

    StringBuilder path = new StringBuilder();
    path.append("C:/Users/will/Documents/");
    path.append(url.getHost().replace('.', '/'));
    path.append(url.getFile());
    path.replace(path.lastIndexOf("."), path.length(), ".txt");

    File file = new File(path.toString());

    boolean mkdir = file.mkdir();

    boolean isNew = false;
    try {
        isNew = file.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    /*
     * only create the file if it doesn't exist
     */
    if (isNew) {
        try {
            ImageIO.write(image, "jpg", file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

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

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

发布评论

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

评论(1

菩提树下叶撕阳。 2024-11-25 04:37:01

尝试使用

boolean mkdir = file.mkdirs();

boolean mkdir = file.mkdir();

mkdirs如果需要, () 创建整个父路径/目录:

Try using

boolean mkdir = file.mkdirs();

instead of

boolean mkdir = file.mkdir();

mkdirs() creates the whole parent path/directories if needed:

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