Reading & writing to file using ArrayList<String> incorrectly
Hi Java Experts,
I am running out of ideas on why my findSnomedCodes.java program is filling up the output file (Snomed-Codes.txt) with the
same information ([M44110|T33010, , M92603|T10350, ]) continuously. This program goes through a dummy patient result file
and pickup the 3rd line of each record and insert it into an ArrayList of String before writing it into a Snomed-Codes.txt
file. A followed up method prints out everything from Snomed-Codes.txt.
Below is the source code of findSnomedCodes.java program:
--
import java.io.*;
import java.io.IOException;
import java.lang.String;
import java.lang.Character;
import java.util.regex.*;
import java.util.ArrayList;
import java.util.List;
public class FindSnomedCode {
static List<String> complete_records = new ArrayList<String>();
static public void getContents(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) {
// 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) {
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();
}
}
}
static public void setContents(File reformatFile, List snomedCodes)
throws FileNotFoundException, IOException {
Writer output = null;
try {
output = new BufferedWriter( new FileWriter(reformatFile) );
while (snomedCodes.iterator().hasNext())
output.write( snomedCodes.toString() );
}
finally {
if (output != null) output.close();
}
}
static public void printContents(File existingFile) {
//...checks on existingFile are elided
StringBuffer contents = new StringBuffer();
BufferedReader input = null;
int line_number = 0;
int number_of_records = 0;
long total_number_of_lines = 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:\\AP Data Conversion\\1988\\dummy_test_records.txt");
getContents(currentFile);
File snomedFile = new File( "D:\\AP Data Conversion\\1988\\Snomed-Codes.txt");
setContents(snomedFile, complete_records);
printContents(snomedFile);
}
}
--
The output from Netbeans/Java is as follows:
M44110|T33010
M92603|T10350
Exception in thread "main" java.io.IOException: There is not enough space on the disk
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:260)
at sun.nio.cs.StreamEncoder$CharsetSE.writeBytes(StreamEncoder.java:336)
at sun.nio.cs.StreamEncoder$CharsetSE.implClose(StreamEncoder.java:427)
at sun.nio.cs.StreamEncoder.close(StreamEncoder.java:160)
at java.io.OutputStreamWriter.close(OutputStreamWriter.java:222)
at java.io.BufferedWriter.close(BufferedWriter.java:250)
at FindSnomedCode.setContents(FindSnomedCode.java:90)
at
FindSnomedCode.main(FindSnomedCode.java:134)
-
The content of Snomed-Codes.txt file is filled with continuous lines of [M44110|T33010, , M92603|T10350, ] as follows:
[M44110|T33010, , M92603|T10350, ][M44110|T33010, , M92603|T10350, ][M44110|T33010, , M92603|T10350, ][M44110|T33010, ,
M92603|T10350, ][M44110|T33010, , M92603|T10350, ][M44110|T33010, , M92603|T10350, ][M44110|T33010, , M92603|T10350,
][M44110|T33010, , M92603|T10350, ][M44110|T33010, , M92603|T10350, ][M44110|T33010, , M92603|T10350, ][M44110|T33010, ,
M92603|T10350, ][M44110|T33010, , M92603|T10350, ][M44110|T33010, , M92603|T10350, ][M44110|T33010, , M92603|T10350,
--
I must have done something wrong with the ArrayList.

