Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Thread Tools Search this Thread
Old 24-10-2010
Join Date: Oct 2010
Posts: 3
java coding

I am working on a java project,i want to sequentially read an ASCII file into an arraylist and then add a method to print out the array. i want to also be able to search through the ASCII file

can anyone help me with the coding. will realy b much appreciated
Reply With Quote
Old 27-10-2010
Join Date: Nov 2008
Posts: 1,192
Re: java coding

The first thing is I do not use an array but a class called ArrayList which lets you add elements without first identifying the length (we do not know a prior how many lines you have the file).
import java.util.ArrayList;
{Leef class 
public static void main (String [] arg) { 
File file = null; 
FileReader fr = null; 
ArrayList lines = new ArrayList (); 
try { 
file = new File ("C: / file.txt"); 
String line; 
fr = new FileReader (file); 
BufferedReader br = new BufferedReader (fr) 
while ((line = br.readLine ())!= null) { 
lineas.add (line); 
System.out.print ("The Vector is: \ n ");// UNDER THE DATA SHOW 
for (int j = 0, j <lineas.size () j + +) { 
line = (String) lineas.get (j); 
System.out.print (line + "\ n"); 
} / / END TRY 
catch (IOException e) { 
System.out.println (e); 
finally { 
try {/ / finally block is always executed, so if the file is closed 
if (fr! = null) {/ / at the end of the first try, and has an error before, would 
fr.close (), / / the 1st catch and then would leave, leaving it open. It is convenient 
} / / Close here, proving that not-for a previous error, as 
} Catch (IOException e) {/ / not be read or does not exist - null value. 
System.exit (0);} 
And if you are trying to read an ASCII file into an arraylist, then following code will help you :
FileReader myFile = new FileReader ("path");
  BufferedReader buffer = new BufferedReader (myFile);
  String string = file buffer.readLine (); 
  dateString.getBytes ("ASCII");
FileInputStream my file = new FileInputStream ("path");
 BufferedReader buffer = new BufferedReader (new InputStreamReader (myFile, "ASCII"));
 <Integer> ArrayList list = new ArrayList <Integer> ();
 list.add (buffer.read ());
Reply With Quote

  TechArena Community > Software > Software Development
Tags: , ,

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads for: "java coding"
Thread Thread Starter Forum Replies Last Post
How to use the pop_heap() in C++ coding Garrick Software Development 4 05-03-2010 05:28 PM
java coding for fibonacci series Jagadamba Software Development 3 17-11-2009 10:44 AM
Code Conventions for JAVA Coding kex Software Development 2 06-05-2009 11:23 PM
C++ coding faster? Chandrakant81 Software Development 3 17-02-2009 10:51 PM
Problem with C coding SANDESH49 Software Development 2 02-09-2008 01:23 PM

All times are GMT +5.5. The time now is 11:01 AM.