您好,有什么方法可以让 FileInputStream 读取hello.txt
在同一目录中而不指定路径?
package hello/
helloreader.java
hello.txt
我的错误信息:Error: .\hello.txt (The system cannot find the file specified)
您可以使用相对路径读取文件,例如。
File file = new File("./hello.txt");
-
你的项目
->bin
-> 你好.txt
->.classpath
->.项目
这是作品
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class fileInputStream {
public static void main(String[] args) {
File file = new File("./hello.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
System.out.println("Total file size to read (in bytes) : "
+ fis.available());
int content;
while ((content = fis.read()) != -1) {
// convert to char and display it
System.out.print((char) content);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)