如何将文件 reader 更改为目录扫描程序?
How to change a file reader to a directory scanner?
回答
System.out.print("Enter the name of the directory:");
File a= new File((new BufferedReader(new
InputStreamReader(System.in))) .readLine());
File[] b=a.listFiles();
for(int i=1;i < b.length;i++){
fn=b[i].getPath(); }
问题
System.out.print("Enter the name of file: ");
fn = (new BufferedReader(new InputStreamReader(System.in)))
.readLine();
我试过了
File[] file Array=new File(System.in).listFiles();
for(File f: fileArray) // loop through all files {
if(f.getName().endsWith(".fasta")){
fn = (new BufferedReader(new InputStreamReader(fileArray)).readLine()); // to read the files }}`
但收到构造函数 File(InputStream) 未定义的错误
我想将 fn 更改为目录。如何遍历保留 Buffered Reader 和 InputStreamReader 的目录?
我看得出您想将内容读入另一个文件...
以下 类 用于 writing/reading 数据到文件。
FileOutputStream,FileInputStream;
如您所见,FileOuputStream用于将数据写入文件,而FileInputStream用于从文件读取数据。
数据可以是文字、图片、视频等
您可以像这样创建一个 FileInputStream 的实例
//replace the string with actual file name
File fileToReadFrom = new File("path/to/file");
FileInputStream = new FileInputStream(fileToReadFrom);
并且类似地创建一个 FileOutputStream 的实例,如下所示:
//replace the string with actual file name
File fileToWriteTo= new File("path/to/file");
FileInputStream = new FileInputStream(fileToWriteTo);
使用这些 类 的实例,您可以读取和写入 from/to 任何文件。
你可以阅读更多关于它们的信息 here
回答
System.out.print("Enter the name of the directory:");
File a= new File((new BufferedReader(new
InputStreamReader(System.in))) .readLine());
File[] b=a.listFiles();
for(int i=1;i < b.length;i++){
fn=b[i].getPath(); }
问题
System.out.print("Enter the name of file: ");
fn = (new BufferedReader(new InputStreamReader(System.in)))
.readLine();
我试过了
File[] file Array=new File(System.in).listFiles();
for(File f: fileArray) // loop through all files {
if(f.getName().endsWith(".fasta")){
fn = (new BufferedReader(new InputStreamReader(fileArray)).readLine()); // to read the files }}`
但收到构造函数 File(InputStream) 未定义的错误
我想将 fn 更改为目录。如何遍历保留 Buffered Reader 和 InputStreamReader 的目录?
我看得出您想将内容读入另一个文件...
以下 类 用于 writing/reading 数据到文件。 FileOutputStream,FileInputStream;
如您所见,FileOuputStream用于将数据写入文件,而FileInputStream用于从文件读取数据。
数据可以是文字、图片、视频等
您可以像这样创建一个 FileInputStream 的实例
//replace the string with actual file name
File fileToReadFrom = new File("path/to/file");
FileInputStream = new FileInputStream(fileToReadFrom);
并且类似地创建一个 FileOutputStream 的实例,如下所示:
//replace the string with actual file name
File fileToWriteTo= new File("path/to/file");
FileInputStream = new FileInputStream(fileToWriteTo);
使用这些 类 的实例,您可以读取和写入 from/to 任何文件。 你可以阅读更多关于它们的信息 here