如何从 java 中的任何目录和对话框中读取文本文件?
How to read a text file from any directory and dialog in java?
我正在创建一个游戏和关卡编辑器,其中关卡存储在 .txt 文件中。打开时,应用程序会打开一个对话框来选择文件。除了无法从类路径外部读取文件外,这非常有效。让它能够从类路径外部读取文件的简单方法是什么?
获取我使用的文件:
public void getLevelPath() {
FileDialog dialog = new FileDialog((Frame) null, "Select File to Open");
dialog.setDirectory("C://");
dialog.setMode(FileDialog.LOAD);
dialog.setVisible(true);
String file = dialog.getFile();
System.out.println(file + " chosen.");
levelPath = file;
}
阅读时我使用:
InputStream is = this.getClass().getResourceAsStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
感谢您的帮助!
File inputFile = new File(path);
InputStream is = new FileInputStream(inputFile );
BufferedReader br = new BufferedReader(new InputStreamReader(is));
我正在创建一个游戏和关卡编辑器,其中关卡存储在 .txt 文件中。打开时,应用程序会打开一个对话框来选择文件。除了无法从类路径外部读取文件外,这非常有效。让它能够从类路径外部读取文件的简单方法是什么?
获取我使用的文件:
public void getLevelPath() {
FileDialog dialog = new FileDialog((Frame) null, "Select File to Open");
dialog.setDirectory("C://");
dialog.setMode(FileDialog.LOAD);
dialog.setVisible(true);
String file = dialog.getFile();
System.out.println(file + " chosen.");
levelPath = file;
}
阅读时我使用:
InputStream is = this.getClass().getResourceAsStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
感谢您的帮助!
File inputFile = new File(path);
InputStream is = new FileInputStream(inputFile );
BufferedReader br = new BufferedReader(new InputStreamReader(is));