package 练习;
import org.junit.Test;
import java.io.*;
public class InputStreamOutputStreamReader {
@Test
public void testInputStreamReader() {
FileInputStream fi = null;
InputStreamReader isr = null;
try {
File file = new File("F:\\java92\\src\\练习\\1.txt");
fi = new FileInputStream(file);
isr = new InputStreamReader(fi, "gbk");
char[] ch = new char[20];
int len;
while ((len = isr.read(ch)) != -1) {
for (int i = 0; i < len; i++) {
System.out.print(ch[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (isr != null) {
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fi != null) {
fi.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void testOutStreamWrite() {
FileInputStream fi = null;
FileOutputStream fis = null;
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
File file = new File("F:\\java92\\src\\练习\\1.txt");
File file2 = new File("F:\\java92\\src\\练习\\2.txt");
fi = new FileInputStream(file);
fis = new FileOutputStream(file2);
isr = new InputStreamReader(fi, "gbk");
osw = new OutputStreamWriter(fis, "utf8");
char[] ch = new char[20];
int len;
while ((len = isr.read(ch)) != -1) {
for (int i = 0; i < len; i++) {
osw.write(ch[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (osw != null) {
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (isr != null) {
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fi != null) {
fi.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis!=null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-33058.html