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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading & Writing to text file using ArrayList incorrectly" Watch "Reading & Writing to text file using ArrayList incorrectly" New topic
Author

Reading & Writing to text file using ArrayList incorrectly

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi All,

I have been able to store two strings (M44110|T33010, M92603|T10350) using ArrayList<String>, which represent the result of some patient tests. Nevertheless, the order of these results have been stored correctly.

E.g. currently stored in file Snomed-Codes as -

[M44110|T33010, M92603|T10350][M44110|T33010, M92603|T10350]

Should be stored and printed out as -

M44110|T33010
M92603|T10350

Below is the detail of the source code of this program:

import java.io.*;
import java.io.IOException;
import java.lang.String;
import java.util.regex.*;
import java.util.ArrayList;

public class FindSnomedCode {

static ArrayList<String> allRecords = new ArrayList<String>();

static public ArrayList<String> getContents(File existingFile) {
StringBuffer contents = new StringBuffer();
BufferedReader input = null;
int line_number = 0;
int number_of_records = 0;
String snomedCodes = null;
ArrayList<String> complete_records = new ArrayList<String>();

try {
input = new BufferedReader( new FileReader(existingFile) );
String current_line = null;
while (( current_line = input.readLine()) != null) {

// Create a pattern to match for the "\"
Pattern p = Pattern.compile("\\\\");
// Create a matcher with an input string
Matcher m = p.matcher(current_line);
boolean beginning_of_record = m.find();
if (beginning_of_record) {
line_number = 0;
number_of_records++;
} else {
line_number++;
}

if ( (line_number == 2) && (current_line.length() != 0) ) {
snomedCodes = current_line;
System.out.println(snomedCodes);
complete_records.add(current_line);
}
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
finally {
try {
if (input!= null) {
input.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
return complete_records;
}


static public void setContents(File reformatFile, ArrayList<String> snomedCodes)
throws FileNotFoundException, IOException {

Writer output = null;
try {
output = new BufferedWriter( new FileWriter(reformatFile) );
for (String index : snomedCodes) {
output.write( snomedCodes.toString() );
}
}
finally {
if (output != null) output.close();
}
}

static public void printContents(File existingFile) {
StringBuffer contents = new StringBuffer();
BufferedReader input = null;
int line_number = 0;
int number_of_records = 0;
String snomedCodes = null;
try {
input = new BufferedReader( new FileReader(existingFile) );
String current_line = null;
while (( current_line = input.readLine()) != null)
System.out.println(current_line);
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
finally {
try {
if (input!= null) {
input.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}

public static void main (String args[]) throws IOException {
File currentFile = new File("D:\\dummy_patient.txt");
allRecords = getContents(currentFile);

File snomedFile = new File( "D:\\Snomed-Codes.txt");
setContents(snomedFile, allRecords);
printContents(snomedFile);
}
}


There are 4 patient records in the dummy_patient.txt file but only the 1st (M44110|T33010) & 3rd (M92603|T10350) records have results in them. The 2nd & 4th records have blank results.

Lastly, could someone explain to me the difference between java.util.List & java.util.ArrayList?

I am running Netbeans 5.0, jdk1.5.0_09 on Windows XP, SP2.

Many thanks,

Netbeans Fan.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Helen Tran:
Lastly, could someone explain to me the difference between java.util.List & java.util.ArrayList?


One is an interface and the other is a class that is a concrete implementation of that interface.

It's not entirely clear to me what you're trying to do. In the future please try to post code between code tags.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
If the problem is with storing the results I would suspect that



should be



And your naming of the variable "index" is suspect since it's not an index, it's the current element in the iteration. The above is equivalent to:



It does the exact same thing, it's just cleaner and clearer to do it with the enhanced for loop.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I just noticed you seem to want each on a new line too.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading & Writing to text file using ArrayList incorrectly