尝试将文件创建到(可能)正确的目录时发生java.io.FileNotFoundException
嗨,我的问题是,当我使用这个:尝试将文件创建到(可能)正确的目录时发生java.io.FileNotFoundException,java,path,relative-path,filepath,fileoutputstream,Java,Path,Relative Path,Filepath,Fileoutputstream,嗨,我的问题是,当我使用这个: final File f = newFile(ButtonOneEvent.class.getProtectionDomain().getCodeSource().getLocation().getPath()); filePath = f.toString() + "/PlayerList.txt"; //Playerlist.txt being the file I want to create, filePath being an empty string.
final File f = newFile(ButtonOneEvent.class.getProtectionDomain().getCodeSource().getLocation().getPath());
filePath = f.toString() + "/PlayerList.txt"; //Playerlist.txt being the file I want to create, filePath being an empty string.
System.out.println(f);
要为此定义类的本地目录,请执行以下操作:
FileOutputStream fout = new FileOutputStream(Start.filePath); //**Reference Point**
if(inputData != null) { //Ignore the if else statement
new PrintStream(fout).println("Text Stuff")
} else {
new PrintStream(fout).println("Text Stuff")
}
fout.close();
当我想要创建这个文件时,我在“reference point”行上得到了一个java.io.FileNotFoundException
,当然它还不在那里。
此代码由JButton
激活顺便说一句,
system.out.println(f)
输出:
*/Users/HillFamily/Library/Application%20Support/Youth%20Digital/Software/Forge/mcp/eclipse/Football%20Card%20Sorter/bin* <- Directory
*/Users/HillFamily/Library/Application%20Support/Youth%20Digital/Software/Forge/mcp/eclipse/Football%20Card%20Sorter/bin*这很可能是因为您的Start.filePath
要么是目录(不是文件),要么无法创建,要么根本不存在。另外,Start.filePath
依赖于系统,必须与您的平台兼容。您需要确保这是正确的,并且您所指的位置具有足够的写入操作访问权限。另一个问题是,您的完整路径名如何知道您正在查看/写入正确的位置?路径上的某些目录确实存在吗
要开始修复操作,您可能需要检查System.out.println(start.filePath)
是否打印任何有意义的内容。如果您对空格和URL编码有问题(例如,在ASCII中%20是一个空格),您需要开始在您的问题上下文中考虑它们
作为示例(从Windows的角度),请参见以下内容:
public static void main(String[] args) {
try{
FileOutputStream fos = new FileOutputStream("C:/myjavaprojects/folder%20with%20a%20space/haha.txt");
// This is unchecked exception - we have to rethrow/catchprocess it
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("Writing to file".getBytes());
bos.flush();
bos.close();
} catch (FileNotFoundException fnfe){
try{
FileOutputStream fos = new FileOutputStream("C:/myjavaprojects/folder with a space/haha.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("Writing to file".getBytes());
bos.flush();
bos.close();
} catch(FileNotFoundException e){
e.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
}
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
第一:这个方法“newFile”的外观,可能需要更改为正确的文件调用:
文件f=新文件(mypath+“PlayerList.txt”)
其次,尝试查看您的类成员“filePath”是否未更改(起始类的成员)。如果要创建要写入文件的输出流,请使用:
如果文件不存在,也会创建该文件
try {
Path p = Paths.get("demo", "test.txt");
Files.createDirectories(p.getParent());
DataOutputStream out = new DataOutputStream(Files.newOutputStream(p, StandardOpenOption.CREATE, StandardOpenOption.WRITE));
out.writeBytes("Hello-World!!");
out.flush();
out.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
向我们展示堆栈跟踪!这是在Windows还是Linux上?有没有办法向文件中添加“强制转换”?或者,我如何在Start.filePath目录下编写一个名为“PlayerList.txt”的文件?您有什么建议?@TurtulWarrior您对文件路径中的%20个符号有问题,并且FileOutputStream
构造函数可能不接受它们。我应该执行filepath=filepath.replace(“%20”,File.separator)@但是,你确实需要确保你的路径是有效的——如果你把路径写错了——它根本不起作用。另外,我在Windows上尝试了包含%20的路径,它抛出FileNotFoundException,但我不知道MAC是否也有同样的问题。只需隔离您的文件位置,然后执行一个简单的FileOutputStream fos=new FileOutputStream(path)
,看看这是否会在运行时引发任何异常。我认为实际路径可能不起作用,因为我移动了文件夹库,在应用程序支持中找不到Youth Digital。我不知道为什么路径仍然显示文件的原始位置。我将尝试手动找到正确的路径。
try {
Path p = Paths.get("demo", "test.txt");
Files.createDirectories(p.getParent());
DataOutputStream out = new DataOutputStream(Files.newOutputStream(p, StandardOpenOption.CREATE, StandardOpenOption.WRITE));
out.writeBytes("Hello-World!!");
out.flush();
out.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}