2017-10-04 108 views
0

所以我得到一個錯誤,指出:麻煩與文件輸出流

java.io.FileNotFoundException: drscqei<@.txt (The filename, directory name, 
or volume label syntax is incorrect) 

at java.io.FileOutputStream.open0(Native Method) 
at java.io.FileOutputStream.open(Unknown Source) 
at java.io.FileOutputStream.<init>(Unknown Source) 
at java.io.FileOutputStream.<init>(Unknown Source) 
at java.io.FileWriter.<init>(Unknown Source) 
at ch1.User.makeUser(User.java:205) 
at ch1.helloworld.main(helloworld.java:94) 

makeUser功能包括以下內容:

public static User makeUser(String fn, String ln, String un, String pw, String cpw, String dob) { 
    if (pw.equals(cpw)) { 
     System.out.println("pass is same as confirm."); 
     if (containsNumber(pw) && charLength(pw) && charLength(un) && !(containsInvalidSymbol(un))) { 
      System.out.println(
        "pw has an uppercase and lower and doesnt have any symbols and length of un and pw are greater than 5 and less than 50."); 
      encrypt(fn, ln, un, pw, dob); 
      File f = new File(encryptedUser + ".txt"); 
      try { 
       FileWriter fw = new FileWriter(f); 
       fw.write(encryptedFn + "," + encryptedLn + "," + encryptedUser + "," + encryptedPass + "," 
         + encryptedDob); 
       fw.flush(); 
       fw.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return new User(fn, encryptedLn, encryptedUser, encryptedPass, encryptedDob); 
     } else { 
      return null; 
     } 
    } else { 
     return null; 
    } 

} 

加密方法只使用一個加密字串凱撒密碼

我知道凱撒密碼不是最好的加密類型m雖然,目前我只是想讓它變成虛擬證明。 加密方法如下:

public static void encrypt(String fn, String ln, String un, String pw, String dob) { 

    int shift = 10; 
    for (int x = 0; x < un.length(); x++) { 
     char c = (char) (un.charAt(x) + shift); 
     if (c > 'z') { 
      encryptedUser += (char) ((un.charAt(x) - 26) + shift); 
     } else { 
      encryptedUser += (char) (un.charAt(x) + shift); 
     } 
    } 
} 

的HelloWorld類那裏我得到的錯誤有以下代碼:

  boolean signUpScreen = true; 
      do { 
       int s = JOptionPane.showOptionDialog(null, signUp, "Sign Up", JOptionPane.OK_CANCEL_OPTION, 
         JOptionPane.PLAIN_MESSAGE, null, Enter_Cancel, Enter_Cancel[0]); 
       if (s == JOptionPane.OK_OPTION) { 

        User.eraseEncryptions(); 

        User sUser = User.makeUser(signUpfn.getText(), signUpln.getText(), signUpUser.getText(), 
          signUpPass.getText(), signUpCPass.getText(), signUpDob.getText()); 
        if (sUser == null) { 
         JOptionPane.showMessageDialog(null, 
           "Sorry, something went wrong. please check that you have filled in all the text fields and that your password is the same as your confirm password.", 
           "Error", JOptionPane.PLAIN_MESSAGE); 
        } else { 
         JOptionPane.showMessageDialog(null, 
           "Success! You have created a new account! welcome " + User.fn, "Account Created", 
           JOptionPane.PLAIN_MESSAGE); 
         signUpScreen = false; 
        } 
       } else { 
        signUpScreen = false; 
       } 
      } while (signUpScreen); 

註冊只是一個JPanel與JTextField的在裏面。我的問題是,爲什麼我得到這個錯誤,我想推廣一下我必須做的事情,以便我可以自己弄清楚,但如果不可能,那麼解決方案就沒有問題。感謝您的時間。

+0

在將文件傳遞給FileWriter之前,您是否嘗試過在文件上調用'File.createNewFile()'?可能首先也要檢查'File.exists()'。 –

+1

改變你的加密方法,只包含允許的字符作爲文件名 –

+0

@AhmadAlsanie謝謝你,這種類型的錯誤是困擾我很多的類型哈哈。 – dirtydan

回答

1

您正在運行的操作系統不允許使用像drscqei<@.txt這樣的名稱。 Windows例如doesn't allow <

+0

感謝人哈哈不久之後就意識到了這一點,並因爲這樣一個愚蠢的錯誤而毆打自己。 – dirtydan