Hello all, i'm having problems with this code. The compiler says: The method put(String, BufferedWriter) in type Map<String, BufferedWriter> is not applicable for the arguments(String, FileWriter).
Also there is another error that says: Cannot Cast From BufferedWriter to FileWriter.
i understand that to use a BufferedWriter, you need a FileWriter, i'm not used to deal with maps and bufferedWriters together :/
Thanks in advance!
package pkg.mig;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Uf_file {
static Map<String, BufferedWriter> filemap;
static Map<String, Long> filelenghmap;
static Map<String, Integer> filesecuence;
static FileWriter out;
static BufferedWriter bw;
public static void writeUF(String ufName, String sys, String uf_row) {
if (filemap == null) {
filemap = new HashMap<String, BufferedWriter>();
if (filelenghmap == null) {
filelenghmap = new HashMap<String, Long>();
filesecuence = new HashMap<String, Integer>();
}
}
if (filelenghmap.get(ufName) == null) {
filelenghmap.put(ufName, new Long(0));
filesecuence.put(ufName, new Integer(0));
}
if (filemap.get(ufName) == null) {
try {
filemap.put(ufName, new FileWriter(getFileName(ufName, sys), true));
Utils.logger.info("Creando nuevo archivo: " + ufName +"-"+filesecuence.get(ufName));
} catch (IOException e) {
// TODO Auto-generated catch block
Utils.logger.severe(e.toString());
Utils.close_app(1);
}
}
out = (FileWriter) filemap.get(ufName);
try {
bw.write(uf_row);
filelenghmap.put(ufName, filelenghmap.get(ufName) + uf_row.length());
if (filelenghmap.get(ufName) > Long.parseLong(Utils.app_properties.getProperty("UF_Size"))) {
closeUF(ufName);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static String getFileName(String ufName, String sys) {
filesecuence.put(ufName, filesecuence.get(ufName) + 1);
return sys + "." + ufName + "." + Utils.getDateSystem()+"EXT-"+Utils.app_properties.getProperty("EXT") +"-"+
Utils.fNumber(Integer.toString(filesecuence.get(ufName)),3) + ".inp";
}
static void closeUF(String ufName) {
bw = (BufferedWriter) filemap.get(ufName);
try {
out.close();
filemap.remove(ufName);
filelenghmap.put(ufName, (long) 0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}