Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing ArrayList to text file

 
Dan Todd
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the full class? Can't tell much from the snippet.


S
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 135
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
asr chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've cut out the irrelevant parts of the code (other cases). Still pretty long though.

 
Stuie Clarky
Ranch Hand
Posts: 98
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic