How to change a file reader to a directory scanner?

386 views Asked by At

Answer

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(); }

Problem

System.out.print("Enter the name of file: ");
fn = (new BufferedReader(new InputStreamReader(System.in)))
        .readLine();

I have tried

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 }}`

but receive an error of The constructor File(InputStream) is undefined

I want to change fn to a directory. How do I iterate through the directory keeping the Buffered Reader and InputStreamReader?

1

There are 1 answers

0
The-null-Pointer- On BEST ANSWER

I can see you want to read the content into another file...

the following classes are used for writing/reading data to files. FileOutputStream,FileInputStream;

as you can see, FileOuputStream is used in writing data to a file and FileInputStream is used in reading data from a file.

the data can be text,image,video, etc.

you can a create an instance of FileInputStream like this

//replace the string with actual file name    
File fileToReadFrom = new File("path/to/file");
FileInputStream = new FileInputStream(fileToReadFrom);

and similarly you create an instance of FileOutputStream like this:

//replace the string with actual file name    
File fileToWriteTo= new File("path/to/file");
FileInputStream = new FileInputStream(fileToWriteTo);

with instances of these classes you can read and write from/to any file. you can read more about them here