我有点奇怪的情况。
问题在于import java.nio.*;
和import java.nio.file.Paths;
。
首先,import java.nio.*;
无论如何都应该导入第二个类,但我遇到了一些奇怪的事情。这可能是我所犯的一个已知错误,但我是Java的新手。
如果我同时导入两者,或者我只导入java.nio.*
,则error: cannot find symbol
会获得Files.write(...)
。但是,如果我仅导入java.nio.file.Paths
,我会收到相同的错误,但Paths.get(...)
。
知道为什么会这样吗?我开始探索除这两个类之外的其他选项,我想要做的就是附加一个文本文件。
public static void addAction(String cmd) {
String text = "Text to save to file";
Files.write(Paths.get("/Users/Andrew/Desktop/testfile.txt"), text.getBytes());
}
问题出在哪里,但如果我写的其他内容出现问题,我已经附上了以下整个文件:
import java.io.File; // Find Files
import java.awt.FileDialog; // File Picker
import javax.swing.JFrame; // JFrame
import java.nio.*;
// import java.nio.file.Paths;
class Files {
public static void getApps(final File folder) {
for (final File file : folder.listFiles()) {
String fileName = file.getName();
// System.out.println(fileName);
// System.out.println(file.getPath());
final File subfolder = new File(file.getPath()+"/Contents/Resources");
if (subfolder.exists()) {
String icon = pickFile(subfolder.getPath());
if (icon == null) return;
System.out.println("App Name: " + fileName);
System.out.println("Icon Name: " + icon);
} else {
System.out.println("Directory not found...");
}
}
}
public static String pickFile(String path) {
FileDialog dialog = new FileDialog(new JFrame(), "Select File to Open");
dialog.setMode(FileDialog.LOAD);
dialog.setFilenameFilter((File dir, String name) -> name.endsWith(".icns"));
dialog.setDirectory(path);
dialog.setVisible(true);
String fileChosen = dialog.getFile();
return fileChosen;
}
public static void addAction(String cmd) {
String text = "Text to save to file";
Files.write(Paths.get("/Users/Andrew/Desktop/testfile.txt"), text.getBytes());
}
public static void main (String[] args) {
String action = "{\n\"BTTTouchBarButtonName\":\"Messages\",\n\"BTTTriggerType\":629,\n\"BTTTriggerClass\":\"BTTTriggerTypeTouchBar\",\n\"BTTPredefinedActionType\":49,\n\"BTTPredefinedActionName\":\"Launch Application \\/ Open File \\/ Start Apple Script …\",\n\"BTTLaunchPath\":\"file:\\/\\/\\/Applications\\/Messages.app\",\n\"BTTEnabled2\":1,\n\"BTTUUID\":\"F3089B96-FDEF-4D54-9DA8-9CCD3C7AF8A9\",\n\"BTTEnabled\":1,\n\"BTTRequiredModifierKeys\":1048576,\n\"BTTOrder\":1,\n \"BTTIconData\":\"icondata\",\n\"BTTTriggerConfig\":{\n\"BTTTouchBarItemIconHeight\":22,\n\"BTTTouchBarItemIconWidth\":22,\n\"BTTTouchBarItemPadding\":0,\n\"BTTTouchBarFreeSpaceAfterButton\":\"5.000000\",\n\"BTTTouchBarButtonColor\":\"75.323769, 75.323769, 75.323769, 255.000000\",\n\"BTTTouchBarAlwaysShowButton\":\"1\",\n\"BTTTouchBarAlternateBackgroundColor\":\"0.000000, 0.000000, 0.000000, 0.000000\"\n}\n}";
File f = new File("/Users/Andrew/Desktop/testfile.txt");
try {f.createNewFile();}catch(Exception e){}
addAction("open 'btt://add_new_trigger/?json="+action+"'");
}
}
编辑:我正在使用JDK版本8-171 for Mac
答案 0 :(得分:0)
Java编译器变得混乱,因为Files是您的类的名称,因此它正在您的类中查找write方法。尝试只留下这个导入:
import java.nio.file.Paths;
然后调用java.nio.file.Files的完整类名:
java.nio.file.Files.write(Paths.get("/Users/Andrew/Desktop/testfile.txt"), text.getBytes());
但是你应该处理你的IOException。添加导入和try / catch。这是你的类没有编译错误:
import java.io.File; // Find Files
import java.io.IOException;
import java.awt.FileDialog; // File Picker
import javax.swing.JFrame; // JFrame
import java.nio.file.Paths;
class Files {
public static void getApps(final File folder) {
for (final File file : folder.listFiles()) {
String fileName = file.getName();
// System.out.println(fileName);
// System.out.println(file.getPath());
final File subfolder = new File(file.getPath()+"/Contents/Resources");
if (subfolder.exists()) {
String icon = pickFile(subfolder.getPath());
if (icon == null) return;
System.out.println("App Name: " + fileName);
System.out.println("Icon Name: " + icon);
} else {
System.out.println("Directory not found...");
}
}
}
public static String pickFile(String path) {
FileDialog dialog = new FileDialog(new JFrame(), "Select File to Open");
dialog.setMode(FileDialog.LOAD);
dialog.setFilenameFilter((File dir, String name) -> name.endsWith(".icns"));
dialog.setDirectory(path);
dialog.setVisible(true);
String fileChosen = dialog.getFile();
return fileChosen;
}
public static void addAction(String cmd) {
String text = "Text to save to file";
try{
java.nio.file.Files.write( Paths.get("/Users/Andrew/Desktop/testfile.txt"), text.getBytes());
}catch(IOException e){
System.err.print("Handle your error!");
}
}
public static void main (String[] args) {
String action = "{\n\"BTTTouchBarButtonName\":\"Messages\",\n\"BTTTriggerType\":629,\n\"BTTTriggerClass\":\"BTTTriggerTypeTouchBar\",\n\"BTTPredefinedActionType\":49,\n\"BTTPredefinedActionName\":\"Launch Application \\/ Open File \\/ Start Apple Script …\",\n\"BTTLaunchPath\":\"file:\\/\\/\\/Applications\\/Messages.app\",\n\"BTTEnabled2\":1,\n\"BTTUUID\":\"F3089B96-FDEF-4D54-9DA8-9CCD3C7AF8A9\",\n\"BTTEnabled\":1,\n\"BTTRequiredModifierKeys\":1048576,\n\"BTTOrder\":1,\n \"BTTIconData\":\"icondata\",\n\"BTTTriggerConfig\":{\n\"BTTTouchBarItemIconHeight\":22,\n\"BTTTouchBarItemIconWidth\":22,\n\"BTTTouchBarItemPadding\":0,\n\"BTTTouchBarFreeSpaceAfterButton\":\"5.000000\",\n\"BTTTouchBarButtonColor\":\"75.323769, 75.323769, 75.323769, 255.000000\",\n\"BTTTouchBarAlwaysShowButton\":\"1\",\n\"BTTTouchBarAlternateBackgroundColor\":\"0.000000, 0.000000, 0.000000, 0.000000\"\n}\n}";
File f = new File("/Users/Andrew/Desktop/testfile.txt");
try {f.createNewFile();}catch(Exception e){}
addAction("open 'btt://add_new_trigger/?json="+action+"'");
}
}
答案 1 :(得分:0)
就我而言,它是一个 JAVA 项目,类是用 Kotlin 编写的(复制粘贴),所以我添加了 Kotlin 依赖项,问题解决了。