Java - file to String issues

Status
Not open for further replies.

hafr1

Seniorius Lurkius
1
Hello all,

Warning newbie programmer!!!!

I'm trying to read a file into string (an ArrayList) It seems to be working SOMEWHAT

it's probably something extremely simple, I just need other eyes to look at it. :p

I get the words in here - and it seems to be working okay, but when I want to put it into a list, it just is shown as a billion letters, and is completely unreadable. However, when I system out print them they are shown as they should.

ty in advance

((WordManager))
public ArrayList<String> getAllWords() throws FileNotFoundException, IOException
{
BufferedInputStream bin = null;

ArrayList<String> allWords = new ArrayList<String>();

try
{
FileInputStream fin = new FileInputStream(file);
bin = new BufferedInputStream(fin);
byte[] contents = new byte[1024];
int bytesRead = 0;
String str;

while ((bytesRead = bin.read(contents)) != -1)
{
str = new String(contents, 0, bytesRead);
allWords.add(str);
}
}
finally
{
if (bin != null)
bin.close();
}
return allWords;
}



// second part of code

private void initLists()
{
WordManager wm = new WordManager();
try
{
ArrayList<String> all = wm.getAllWords();

DefaultListModel allModel = new DefaultListModel();
for (String name : all)
{
allModel.addElement(name);
System.out.println(name);
}
jList1.setModel(allModel);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}

}
 
Status
Not open for further replies.