FileWriter 上出现意外的 FileNotFoundException
Unexpected FileNotFoundException on FileWriter
我正在尝试创建一个我将写入的简单文本文件。
我收到以下错误:
/Library/Java/Home/bin/java -Didea.launcher.port=7542 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 14 CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/Home/lib/deploy.jar:/Library/Java/Home/lib/dt.jar:/Library/Java/Home/lib/javaws.jar:/Library/Java/Home/lib/jce.jar:/Library/Java/Home/lib/jconsole.jar:/Library/Java/Home/lib/management-agent.jar:/Library/Java/Home/lib/plugin.jar:/Library/Java/Home/lib/sa-jdi.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/charsets.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsse.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/ui.jar:/Library/Java/Home/lib/ext/apple_provider.jar:/Library/Java/Home/lib/ext/dnsns.jar:/Library/Java/Home/lib/ext/localedata.jar:/Library/Java/Home/lib/ext/sunjce_provider.jar:/Library/Java/Home/lib/ext/sunpkcs11.jar:/Users/Adam/IdeaProjects/Data Scraper/out/production/Data Scraper:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain DataScraper
Exception in thread "main" java.io.FileNotFoundException: ~/Desktop/usernames.txt (No such file or directory)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:192)
at java.io.FileWriter.<init>(FileWriter.java:90)
at DataScraper.main(DataScraper.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
代码:
import Resources.Constants;
import java.awt.*;
import java.io.*;
public class DataScraper {
public static void main(String[] args) throws Exception {
File file = new File(Constants.filePath, Constants.fileName);
Desktop desktop = Desktop.getDesktop();
BufferedWriter Entry = new BufferedWriter(new FileWriter(file, true));
}
}
package Resources;
public class Constants {
public static String baseURL = "www.lolking.net/summoner/na/";
public static String filePath = "~//Desktop//";
public static String fileName = "usernames.txt";
public static int limit = 100;
}
如果有人可以指导我解决我做错的地方,我将不胜感激。我在我的 Windows 笔记本电脑上使用它,但在我的 Mac 上它似乎不起作用。
您不能在路径中使用波浪号 ~。如果你想要用户的主目录,你可以从 System.getProperty("user.home")
public static String filePath = "~//Desktop//";
这行不通。事实上,我很惊讶你说它适用于 Windows。
您可能想让“~”表示您的主目录...
除此之外表示shell。 Java 不知道那是什么。它将在这里有效地尝试做的是找到一个名为“~”的目录和一个名为 Desktop
的条目。
用System.getProperty("user.home")
就知道你家的目录是什么了。
现在是 2015 年,所以不要使用文件。使用 java.nio.file 代替:
final Path path = Paths.get(System.getProperty("user.home"),
"Desktop", "yourFileName");
try (
final BufferedWriter writer = Files.newBufferedWriter(path,
StandardCharsets.UTF_8, StandardOpenOption.APPEND);
) {
// use the writer here
}
我正在尝试创建一个我将写入的简单文本文件。
我收到以下错误:
/Library/Java/Home/bin/java -Didea.launcher.port=7542 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 14 CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/Home/lib/deploy.jar:/Library/Java/Home/lib/dt.jar:/Library/Java/Home/lib/javaws.jar:/Library/Java/Home/lib/jce.jar:/Library/Java/Home/lib/jconsole.jar:/Library/Java/Home/lib/management-agent.jar:/Library/Java/Home/lib/plugin.jar:/Library/Java/Home/lib/sa-jdi.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/charsets.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsse.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/ui.jar:/Library/Java/Home/lib/ext/apple_provider.jar:/Library/Java/Home/lib/ext/dnsns.jar:/Library/Java/Home/lib/ext/localedata.jar:/Library/Java/Home/lib/ext/sunjce_provider.jar:/Library/Java/Home/lib/ext/sunpkcs11.jar:/Users/Adam/IdeaProjects/Data Scraper/out/production/Data Scraper:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain DataScraper
Exception in thread "main" java.io.FileNotFoundException: ~/Desktop/usernames.txt (No such file or directory)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:192)
at java.io.FileWriter.<init>(FileWriter.java:90)
at DataScraper.main(DataScraper.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
代码:
import Resources.Constants;
import java.awt.*;
import java.io.*;
public class DataScraper {
public static void main(String[] args) throws Exception {
File file = new File(Constants.filePath, Constants.fileName);
Desktop desktop = Desktop.getDesktop();
BufferedWriter Entry = new BufferedWriter(new FileWriter(file, true));
}
}
package Resources;
public class Constants {
public static String baseURL = "www.lolking.net/summoner/na/";
public static String filePath = "~//Desktop//";
public static String fileName = "usernames.txt";
public static int limit = 100;
}
如果有人可以指导我解决我做错的地方,我将不胜感激。我在我的 Windows 笔记本电脑上使用它,但在我的 Mac 上它似乎不起作用。
您不能在路径中使用波浪号 ~。如果你想要用户的主目录,你可以从 System.getProperty("user.home")
public static String filePath = "~//Desktop//";
这行不通。事实上,我很惊讶你说它适用于 Windows。
您可能想让“~”表示您的主目录...
除此之外表示shell。 Java 不知道那是什么。它将在这里有效地尝试做的是找到一个名为“~”的目录和一个名为 Desktop
的条目。
用System.getProperty("user.home")
就知道你家的目录是什么了。
现在是 2015 年,所以不要使用文件。使用 java.nio.file 代替:
final Path path = Paths.get(System.getProperty("user.home"),
"Desktop", "yourFileName");
try (
final BufferedWriter writer = Files.newBufferedWriter(path,
StandardCharsets.UTF_8, StandardOpenOption.APPEND);
) {
// use the writer here
}