背景
从我到目前为止看到的示例中,当您在java中使用字节流读取文件时,必须在命令提示符中指定文件名。我编写了一个Java程序,我需要在源代码中指定文件名。例如:
/* Display a text file.
To use this program, specify the name of the file that you want to see.
For example, to see a file called TEST.TXT, use the following command line.
java ShowFile TEST.TXT
*/
import java.io.*;
class ShowFile {
public static void main(String args[])
throws IOException
{
int i;
FileInputStream fin;
try {
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException e) {
System.out.println("File Not Found");
return;
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: ShowFile File");
return;
}
// read characters until EOF is encountered
do {
i = fin.read();
if(i != -1) System.out.print((char) i);
} while(i != -1);
fin.close();
}
}
问题
有没有办法在源代码中使用字节流指定要读取的文件名?
答案 0 :(得分:0)
第一个google结果" java打开文件字节流"告诉你如何做到这一点。它是主要的java documentation。
编辑: 基于您的代码示例
fin = new FileInputStream("myFilenameExample");