问题
I am attempting to create a simple text file that I will be writing to.
I receive the following error:
/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)
Code:
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;
}
If someone could please guide me through what I'm doing wrong, I would appreciate it. I got this working on my Windows laptop, but on my Mac, it doesn't seem to be working.
回答1:
public static String filePath = "~//Desktop//";
This will not work. In fact I am surprised that you say that it works on Windows.
You probably meant for the '~' to mean your home directory...
Except that it means this for the shell. Java has no idea what that is. What it will effectively try to do here is find a directory named '~' and an entry named Desktop
in it.
Use System.getProperty("user.home")
to know what your home diretory it.
And this is 2015, so don't use File. use java.nio.file instead:
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
}
回答2:
You can't use a tilde ~ in your path. If you want the user's home directory, you can get it from System.getProperty("user.home")
来源:https://stackoverflow.com/questions/29199257/unexpected-filenotfoundexception-on-filewriter