java java.io.FileNotFoundException: ‪D:\qr.jpg (The filename, directory name, or volume label syntax is incorrect)

时间:2018-02-03 08:52:07

标签: java ioexception

when i run this code it gets me " java.io.FileNotFoundException: ‪‪D:\qr.jpg (The filename, directory name, or volume label syntax is incorrect)" error

here is my code

 try {
        String kk = "fsdfsfs";
        ByteArrayOutputStream out = QRCode.from(kk).to(ImageType.JPG).stream();
        File f = new File("‪‪D:\\qr.jpg");
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(out.toByteArray());
        fos.flush();
    } catch (Exception e) {
        System.err.println(e);
    }

1 个答案:

答案 0 :(得分:1)

It seems that something funny is going on there. My advice will be to check if your program thinks the file is there and that it can write to it. Try the following and let me know what comes up:

try {
            String kk = "fsdfsfs";
            ByteArrayOutputStream out = QRCode.from(kk).to(ImageType.JPG).stream();
            File f = new File("‪‪D:\\qr.jpg");
            if(f.exists()) {
                System.out.println("File exists");
            }else {
                f.createNewFile(); // if the file does not exist, create it
                System.out.println("Created non-existing file");
            }
            if(f.canWrite()) {
                System.out.println("File can be written to");
            }

            FileOutputStream fos = new FileOutputStream(f);
            fos.write(out.toByteArray());
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }