Hello,
I'm writing a program. It must read many files and for each he must split their content in tokens ( the blank space is the delimiter). Now I think to use scanner Object to work on this; and I'm wondering about the file reading: what better? I suppose doens't make sense to read every file into a block and then Scanner it !? Or it's better open a file, read a file, scanner a single line and go on till the end of the file (this for all file);
Then I must work on the tokens of all files: so I think to put every tokens in a Vector.
Furthermore, How do you implement it ? I mean:I suppose to have something like:
class FileToText {
static public String readString file){
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader(new File(file) ));
try {
String s = null;
while (( s = in.readLine()) != null) {
sb.append(s);
}
} finally { in.close(); }
} catch (IOException e) {
System.out.println("file not found");
System.exit(-1);
}
return sb.toString();
}
}
//main
Scanner scanner = new Scanner ( TextFile.read( "file.txt") );
class FileToText {
static public String readString file){
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader(new File(file) ));
try {
String s = null;
while (( s = in.readLine()) != null) {
sb.append(s);
}
} finally { in.close(); }
} catch (IOException e) {
System.out.println("file not found");
System.exit(-1);
}
return sb.toString();
}
}
//main
Scanner scanner = new Scanner ( TextFile.read( "file.txt") );
To copy to clipboard, switch view to plain text mode
Maybe it doesn't make sense read line by line and after scanner it....How do you do this, please?
thanks,
Bookmarks