我目前正在制作一个应用程序,用户可以在其中添加多个帐户。登录信息存储在文本文件中。我需要一种方法让用户添加新帐户,而 BufferedWriter 不会覆盖其他帐户的信息。有什么建议么?
package mainFiles.fileManagers;
import java.io.*;
public class NewTXT {
public static void nFile( String a , String b ) throws IOException {
File f = new File("mainFiles\\storage\\accountinfo.txt");
FileWriter writer = new FileWriter("mainFiles\\storage\\accountinfo.txt");
BufferedWriter bWriter = new BufferedWriter(writer);
f.createNewFile();
f.canWrite();
bWriter.newLine();
bWriter.write(a + "?" + b );
bWriter.newLine();
bWriter.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将文件写入Java中的特定目录.我已经在Stack Overflow论坛中查看了许多与此相关的问题.然而,我尝试的每种方法最终都会出现此错误:
java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1012)
at mainFiles.fileManagers.NewCSV.nJSON(NewCSV.java:40)
at mainFiles.appWindows.UserDashboard$1.run(UserDashboard.java:30)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Run Code Online (Sandbox Code Playgroud)
码:
public static void nJSON(String username) {
File file = new File("src/jsonFiles/" + username + ".json");
try {
if (file.exists() && file.isDirectory()) {
System.out.println(username + ".json exist.");
} else {
json.put("Username", username);
json.put("Password", mainFiles.windowsSrc.UserInfo.password); …Run Code Online (Sandbox Code Playgroud)