Java How to Read Multiple Files From Folder
#i
Read multiple files from a binder in java- mistake
Posted 23 September 2012 - 10:31 PM
I am trying to read the contents of multiple files stored at the given binder. Just I seem to exist getting this fault at Lines 44 and 47: Exception in thread "main" java.lang.Error: Unresolved compilation bug:
br cannot be resolved to a variable
br cannot be resolved
I just can't figure whats the error. This error is non even letting me proceed so that I can check if my logic is correct. I have attached my code if needed.
Please allow me know whats my mistake and thank you for your help!!
parcel FirstTry; import java.io.*; import java.lang.*; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import coffee.io.FileReader; import java.io.IOException; public grade FileRead { public static void main(String[] args) { endeavor { File dir = new File( "C:\\Users\\Fu\\Desktop\\Downloads\\cranfield\\cranfieldDocs"); for (String fn : dir.list()) { FileInputStream fstream = new FileInputStream(fn); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; while ((strLine = br.readLine()) != null) { System.out.println(strLine); } in.close(); } } catch (FileNotFoundException east) { e.printStackTrace(); } grab (IOException e) { e.printStackTrace(); } finally { if (br != zip) endeavor { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } #two
Re: Read multiple files from a folder in java- error
Posted 23 September 2012 - 10:43 PM
You are trying to use br out of the scope it is declared in.
The variables you declare inside your try block cannot be referenced in your catch or finally blocks.
It would exist enough if you lot close your reader in your attempt block.
package FirstTry; import java.io.*; import coffee.lang.*; import java.io.BufferedReader; import coffee.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class FileRead { public static void main(String[] args) { endeavor { File dir = new File( "C:\\Users\\Fu\\Desktop\\Downloads\\cranfield\\cranfieldDocs"); for (Cord fn : dir.list()) { BufferedReader br = new BufferedReader(new FileReader(in)); Cord strLine; while ((strLine = br.readLine()) != nada) { System.out.println(strLine); } br.shut(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { east.printStackTrace(); } } } This mail has been edited by CasiOo: 23 September 2012 - 10:44 PM
#iii
Re: Read multiple files from a folder in java- fault
Posted 23 September 2012 - x:46 PM
Well, if your program fails at the proclamation and initialization of the dir variable, your BufferedReader won't get initialized. I suggest declaring your br and input streams outside your endeavor - catch block prepare as empty or null. Endeavour it.
#4
Re: Read multiple files from a folder in java- mistake
Posted 23 September 2012 - xi:08 PM
Thank you CasiOo and fromTheSprawl!
I completely disregarded such a simple mistake.
That got resolved, but at present I'thousand getting a new set up of errors:
coffee.io.FileNotFoundException: cranfield0001 (The system cannot detect the file specified)
Cheers CasiOo and fromTheSprawl!
I completely overlooked such a simple mistake.
That got resolved, but now I'm getting a new set of errors:
java.io.FileNotFoundException: cranfield0001 (The organization cannot find the file specified)
#5
Re: Read multiple files from a binder in java- error
Posted 23 September 2012 - 11:41 PM
Is cranfield0001 a directory?
list() returns both directories and files, so exist sure to check if information technology is a file.
package FirstTry; import java.io.*; import coffee.lang.*; import coffee.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import coffee.io.FileReader; import coffee.io.IOException; public class FileRead { public static void main(String[] args) { try { File dir = new File( "C:\\Users\\Fu\\Desktop\\Downloads\\cranfield\\cranfieldDocs"); for (Cord fn : dir.list()) { File file = new File(fn); if (file.isFile()) { BufferedReader br = new BufferedReader(new FileReader(in)); String strLine; while ((strLine = br.readLine()) != null) { System.out.println(strLine); } br.close(); } } } take hold of (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { due east.printStackTrace(); } } } #half dozen
Re: Read multiple files from a folder in java- fault
Posted 23 September 2012 - 11:52 PM
Information technology is a file. cranfieldDocs is the folder which contains files Cranfield0001,Cranfield0002...Cranfield1500.
This is simply a base program. I actually have to retrieve each discussion from these files and store them.
Simply to go ane step at a time, I'm trying to only read the files(in the above program, then later on on I'll try and code the tokenization).
#seven
Re: Read multiple files from a binder in java- error
Posted 23 September 2012 - eleven:57 PM
Does list() return the accented path of the file or just the name of it?
A quick look in the File class shows that it has a listFiles() method, mayhap you volition desire to utilize that method instead
#8
Re: Read multiple files from a binder in java- mistake
Posted 24 September 2012 - 08:20 AM
Give thanks yous!! I made the changes to: for (File fn : dir.listFiles())
Its giving me the corret output, that is, displays the contents of all the files.
At present I'll move on to the side by side part of extracting each word from every file and storing in an array. Any pointers to aid me in that direction?
Java How to Read Multiple Files From Folder
Source: https://www.dreamincode.net/forums/topic/292998-read-multiple-files-from-a-folder-in-java-error/
0 Response to "Java How to Read Multiple Files From Folder"
Post a Comment