It's not a secret anymore!*
The moose likes I/O and Streams and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark ""Exception in thread "main" java.lang.OutOfMemoryError"" Watch ""Exception in thread "main" java.lang.OutOfMemoryError"" New topic
Author

"Exception in thread "main" java.lang.OutOfMemoryError"

Carla carmona
Greenhorn

Joined: Oct 14, 2004
Posts: 14
Hi there !!!

I am using this program - I found it on the net - and what it basically does is to convert comma separeted files into xml files. Everything workes fine until I start using files with loads of data. For instance I have a 2,350 KB of data that gives me the following error: "Exception in thread "main" java.lang.OutOfMemoryError " , but when I take half of the files content off it works without any problems. Can someone out there help me, on way it happens ? I am posting the full program code bellow. Please Help !!!



/*
* CSVToXML.java
*
* @version 1.0
*
*
* @author Danny Ayers
* @created 23 February 2001
*/

import java.util.*;
import java.io.*;

/**
* Converts CSV data file into XML
*/
public class CSVToXML {

public final static String PROLOG = "<?xml version=\"1.0\"?>";
public final static String OPEN_START = "<";
public final static String OPEN_END = "</";
public final static String CLOSE = ">";
public final static String NEWLINE = "\n";
public final static String INDENT = "\t";

private String delimiter;
private int nfields;
private int nrows;
private String header;
private String rowname;
private String rootname;
private String[] fieldnames;


public static void main(String filenames[]) {

filenames = new String[]{"csvpro.txt","Source1.txt","dest.txt"};
if (filenames.length != 3) {
System.out.println("java CSVToXML [properties file] [source] [destination]");
System.exit(1);
}
new CSVToXML(filenames);
}

public CSVToXML(String filenames[]) {
Properties props = loadProperties(filenames[0]);
setParameters(props);
String source = loadString(filenames[1]);
String outFile = filenames[2];

ArrayList items = stringToArrayList(source, delimiter);
nrows = items.size() / nfields;
String output = convert(items);
saveString(outFile, output);
}

public Properties loadProperties(String filename) {
Properties props = new Properties();
FileInputStream instream;;
BufferedInputStream buffer;

try {
instream = new FileInputStream(filename);
buffer = new BufferedInputStream(instream);
props.load(buffer);
buffer.close();
instream.close();
} catch (Exception e) {
System.out.println(e);
}
return props;
}

public void setParameters(Properties props) {
delimiter = props.getProperty("delimiter");
rootname = props.getProperty("rootname");
rowname = props.getProperty("recordname");

String prolog = props.getProperty("prolog");
String dtd = props.getProperty("dtd");
String comment = props.getProperty("comment");

/*
header = PROLOG + NEWLINE
+ "<DOCTYPE " + rootname + " SYSTEM " + dtd + ">" + NEWLINE + NEWLINE
+ "<!-- " + comment + " -->" + NEWLINE;
*/

header = PROLOG + NEWLINE
+ "<!-- " + comment + " -->" + NEWLINE;

nfields = Integer.parseInt(props.getProperty("fields"));

fieldnames = new String[nfields];
String fieldref;

for (int field = 0; field < nfields; field++) {
fieldref = "field" + field;
fieldnames[field] = props.getProperty(fieldref);
}


}

public String loadString(String filename) {
StringBuffer text = new StringBuffer();
FileInputStream instream;
BufferedInputStream buffer;
int readint;
// Remove the buffered input stream (for now)
// Do not call read(buffer). Instead call readFully(buffer). You are not checking how many bytes were actually read in your logic



try {
instream = new FileInputStream(filename);
buffer = new BufferedInputStream(instream);
while ((readint = buffer.read()) != -1) {

text.append( (char) readint);
}
buffer.close();
instream.close();
} catch (Exception e) {
e.printStackTrace();
}
return text.toString();
}



public ArrayList stringToArrayList(String source, String delim) {
ArrayList alist = new ArrayList();
String word;
StringTokenizer st = new StringTokenizer(source, delim);
while (st.hasMoreTokens()) {
word = st.nextToken();
// word = stripSpaces(word);
alist.add(word);
}
return alist;
}

public String stripSpaces(String s) {
while (Character.isWhitespace(s.charAt(0))) {
s = s.substring(1, s.length());
}
return s;
}

public String convert(ArrayList items) {
StringBuffer output = new StringBuffer();
output.append(header);
output.append(NEWLINE);
output.append(OPEN_START + rootname + CLOSE);
output.append(NEWLINE);

Iterator values = items.iterator();
String value;
for (int rowcount = 0; rowcount < nrows; rowcount++) {
output.append(INDENT + OPEN_START + rowname + CLOSE + NEWLINE);

for (int field = 0; field < nfields; field++) {
output.append(INDENT + INDENT + OPEN_START + fieldnames[field] + CLOSE);
value = (String) values.next();
output.append(value);
output.append(OPEN_END + fieldnames[field] + CLOSE + NEWLINE);

}

output.append(INDENT + OPEN_END + rowname + CLOSE);
output.append(NEWLINE);
}
output.append(OPEN_END + rootname + CLOSE);
output.append(NEWLINE);
return output.toString();
}



public void saveString(String filename, String string) {

FileOutputStream outstream;
BufferedOutputStream buffer;
int length;
byte[] bytes;
try {
outstream = new FileOutputStream(filename);
buffer = new BufferedOutputStream(outstream);
bytes = string.getBytes();
length = string.length();
for (int i = 0; i < length; i++) {
buffer.write(bytes[i]);
}
buffer.close();
outstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}
Carla carmona
Greenhorn

Joined: Oct 14, 2004
Posts: 14
Thanks if you tried to help. Solucion found. Just called "java -Xmx250m" plus the of the command prompt parameters to increase the amount of memory available to the program and voila, problem solved. May the force be with you all.

Jose Cardoso
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "Exception in thread "main" java.lang.OutOfMemoryError"