-
Problem with using Vector
There is a text file named 'stoplist' containing a list of words, like this:
a
the
they
we
be
is
...
What I wanna do is to store them in a Vector. I've got the following code:
Vector stopList = new Vector();
File stoplist = new File("C:\\Text_Preprocessing\\stoplist.txt");
FileReader stopword = new FileReader(stoplist);
int s = stopword.read();
while (s != -1) {
StringBuffer StopString = new StringBuffer();
if (s!='\n') StopString.append((char)s);
else stopList.addElement(StopString.toString());
}
stopword.close();
However, it can not be run! Where is the problem?
-
You need to catch IOException.
Except I don't really see why you're going through all that trouble to read store lines. Here's a quicker way:
Code:
import java.util.*;
import java.io.*;
public class FileReaderExample {
public static void main(String[] args) {
ArrayList wordList = new ArrayList();
try {
BufferedReader in = new BufferedReader(new FileReader("someFile.txt"));
String line;
while ((line = in.readLine()) != null) {
wordList.add(line);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Useful links:
The Java(tm) Tutorial: Handling Errors Using Exceptions
Exceptions in Java
Java Exceptions
Exception Handling in Java
Java Olympus: Java Exceptions
Java Programming Notes: Exceptions
Last edited by destin; 01-14-2006 at 08:09 PM.
Similar Threads
-
Replies: 5
Last Post: 02-23-2005, 01:13 AM
-
Replies: 0
Last Post: 10-30-2002, 05:40 AM
-
Replies: 1
Last Post: 02-23-2002, 02:44 AM
-
Replies: 0
Last Post: 12-13-2001, 01:06 PM
-
Replies: 1
Last Post: 11-05-2000, 09:34 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|