标签:advance str exce mamicode pictures console exception byte res
// read the file to console
@Test
public void test2(){
FileReader fr = null;
try {
// step 1 : new the file
File file = new File("/Users/truman/Desktop/a.txt");
// step 2 : provide the IO STREAM
fr = new FileReader(file);
// step 3: read the file
int data = fr.read(); // read() : return a character or -1 if reach the end
while (data != -1) {
System.out.print((char) data);
data = fr.read();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// step 4: close the stream
try {
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void testAdvancedRead(){
FileReader fr = null;
try {
// I need a file
File file = new File("/Users/truman/Desktop/a.txt");
// create the stream
fr = new FileReader(file);
//****************************************************************
// read
char[] cbuf = new char[5];
int len ;
while ((len = fr.read(cbuf)) != -1){
for(int i = 0; i < len; i++){
System.out.print(cbuf[i]);
}
}
//****************************************************************
} catch (IOException e) {
e.printStackTrace();
} finally {
// close the stream
if(fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testWrite(){
FileWriter fw = null;
try {
// step 1: the file you wanna write to
File file = new File("/Users/truman/Desktop/b.txt");
// step 2 : create the writer
fw = new FileWriter(file);
// step 3: write()
fw.write("Hello");
} catch (IOException e) {
e.printStackTrace();
} finally {
// step 4 : close the resource
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testFileCopy(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// the image file
File image1 = new File("/Users/truman/Pictures/Lady.jpg");
File image2 = new File("/Users/truman/Desktop/YoungLady.jpg");
// create the fileInputStream and fileOutputStream
fis = new FileInputStream(image1);
fos = new FileOutputStream(image2);
byte[] bbuf = new byte[1024];
int len;
while ((len = fis.read(bbuf)) != -1){
fos.write(bbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// close the resources
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testBuffered(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(new File("/Users/truman/Pictures/loveActually.jpg")));
bos = new BufferedOutputStream(new FileOutputStream(new File("/Users/truman/Desktop/love.jpg")));
byte[] bbuf = new byte[1024];
int len;
while ((len = bis.read(bbuf)) != -1){
bos.write(bbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// close the resource
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
标签:advance str exce mamicode pictures console exception byte res
原文地址:https://www.cnblogs.com/nedrain/p/13288573.html