Ahem :)
Wednesday, September 28, 2011
Java CSV Reader
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
public class CSVReader {
public HashMap getServerThesh() {
HashMap<String, String> serverThesh = new HashMap<String, String>();
try {
File file = new File("GD.csv");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) {
String line = dis.readLine();
String[] info = line.split(",");
serverThesh.put(info[0], info[1]);
}
return serverThesh;
} catch (Exception e) {
e.printStackTrace();
}
return serverThesh;
}
public static void main(String[] args) {
System.out.println(new CSVReader().getServerThesh().keySet().size());
}
}
Java File Transfer
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
public class FileTransfer {
public static void main(String[] args) {
File f;
f = new File("myfile.txt");
File scpFile = new File("/tmp/myfile.txt");
if (!f.exists()) {
try {
f.createNewFile();
renameTo(f,scpFile);
// copyFile(f, scpFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void copyFile(File in, File out) throws Exception {
FileInputStream fis = new FileInputStream(in);
FileOutputStream fos = new FileOutputStream(out);
try {
byte[] buf = new byte[1024];
int i = 0;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
} catch (Exception e) {
throw e;
} finally {
if (fis != null)
fis.close();
if (fos != null)
fos.close();
}
}
public static void renameTo(File in, File out) {
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
inChannel = new FileInputStream(in).getChannel();
outChannel = new FileOutputStream(out).getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
in.delete();
} catch (Exception e) {
//LogManager.error(e.getMessage(), e);
} finally {
try {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
} catch (Exception e) {
// LogManager.error(e.getMessage(), e);
}
}
}
}
Tuesday, August 17, 2010
My 50 cents
All this while I've been googling to pull some small code snippets to integrate them in several scenarios encountered during everyday coding life.
Just realized that I never contributed my 50 cents in this web.
So, Here it is.. All my "ready" code pieces which might help any do their bit easily.
Just realized that I never contributed my 50 cents in this web.
So, Here it is.. All my "ready" code pieces which might help any do their bit easily.
Subscribe to:
Posts (Atom)