-
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 07:09 PM.
Similar Threads
-
Replies: 5
Last Post: 02-23-2005, 12:13 AM
-
Replies: 0
Last Post: 10-30-2002, 04:40 AM
-
Replies: 1
Last Post: 02-23-2002, 01:44 AM
-
Replies: 0
Last Post: 12-13-2001, 12:06 PM
-
Replies: 1
Last Post: 11-05-2000, 08: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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks