Saturday, 10 January 2015

Simple way to read and write files in java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFile {

public static void main(String[] args) {

try {
File f1 = new File("loremipsum.txt");
File f2 = new File("target.txt");

InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);

byte[] buf = new byte[1024];

int len;

while (((len = in.read(buf)) > 0)) {

out.write(buf, 0, len);

}

in.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

1 comment:

  1. Online Gambling: Casino, Poker and Sports Betting
    Online gambling 먹튀검증사이트 is a business in 텐벳먹튀 the US 더굿 토토 that provides gamblers with youbet opportunities to place wagers and other bet types. It's a great idea to 가입시 꽁 머니 환전 have some

    ReplyDelete