File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Writing ArrayList to text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Writing ArrayList to text file" Watch "Writing ArrayList to text file" New topic
Author

Writing ArrayList to text file

Dan Todd
Greenhorn

Joined: May 21, 2013
Posts: 2
I'm trying to write an ArrayList to a text file.



data is the name of the ArrayList.

Both the data.size() and the data.get() methods are returning 'method not found' errors, even though as ArrayList methods they should be working anyways. What's going on?
Stuie Clarky
Ranch Hand

Joined: Nov 09, 2012
Posts: 72

Can you post the full class? Can't tell much from the snippet.


S


OCAJP 7
Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 131

Welcome to Java Ranch!!

Did you create a class called ArrayList in your working folder?
So your program will use it instead for java.util.ArrayList class.


Are you better than me? Then please show me my mistakes..
asr chowdary
Ranch Hand

Joined: Mar 14, 2013
Posts: 35
Hi

the below code is working for me.

try {
DataOutputStream dos= new DataOutputStream(new BufferedOutputStream(new FileOutputStream("/tmp/visitcount/countt")));
//System.out.println("before"+countnum);
ArrayList<Integer> ar= new ArrayList<Integer>();
ar.add(77);
ar.add(88);
ar.add(99);
for(int i=0;i<ar.size();i++){
System.out.println("before"+ar.get(i));
dos.writeInt(ar.get(i));
System.out.println("After"+ar.get(i));
}
dos.close();
} catch (IOException e) {
e.printStackTrace();
}


can you try this code....in your code why for loop dosen't contains { and } and use outStream.close(); after loop is completed.
Dan Todd
Greenhorn

Joined: May 21, 2013
Posts: 2
I've cut out the irrelevant parts of the code (other cases). Still pretty long though.

Stuie Clarky
Ranch Hand

Joined: Nov 09, 2012
Posts: 72

In your dispatch method you are passing in a Namelist object called data. How is this Namelist created? Look at how you have implemented this class, make sure it is all working correctly as you are manipulating this in the method, not an ArrayList as you originally stated
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing ArrayList to text file
 
Similar Threads
List from Action to JSP in Struts 1
Not all content of ArrayList is written to the TextFile
help with magic square
Implementing a Hash Table
JTable as User's input (?)