JAva IOストリームの二IOストリーム操作
シーケンス:IOストリームの操作は主に2つの読み取りと書き込みに分けられる.一方、バッファクラス文字ストリームBufferedReader/WriterとバイトストリームBufferedInputStream/OutputStreamを追加しないで簡単な読み書きを行うことができます.もちろん、バッファクラスを追加すると効率が高くなります.一方,ストリーム変換が必要な場合,主に読み出し文字化の問題を解決し,バイトストリームが符号化指定できるため,バイトストリームから文字ストリームへの変換が必要である.
1.文字ストリームの読み書き+BufferedReader/Writerなし
2.バイトストリームの読み書き+BufferedInputStream/OutputStreamなし
3.文字ストリームの読み書き+BufferedReader/Writer
4.バイトストリームの読み書き+プラスBufferedInputStream/OutputStream
1.文字ストリームの読み書き+BufferedReader/Writerなし
public class CharIOTest {
/**
* .
* @throws FileNotFoundException
* @throws IOException
* @throws IOException
*/
@Test
public void readChangeTest() throws IOException {
File file = new File("C:\\Documents and Settings\\Administrator\\ \\1.txt");//
FileInputStream fis = new FileInputStream(file);//
InputStreamReader isr=new InputStreamReader(fis, "gbk");
char[] bb = new char[1024];//
StringBuffer sb = new StringBuffer();// , StringBuffer
int n;//
while ((n = isr.read(bb)) != -1) {
sb.append(new String(bb, 0, n));
}
isr.close();// ,
System.out.println(sb);
}
/**
* , 。
* : , 。
* @throws IOException
*/
@Test
public void readTest() throws IOException {
File file = new File("C:\\Documents and Settings\\Administrator\\ \\2.txt");//
FileReader fr=new FileReader(file);
// System.out.println(fr.getEncoding());
char[] bb = new char[1024];//
StringBuffer sb = new StringBuffer();// , StringBuffer
int n;//
while ((n = fr.read(bb)) != -1) {
sb.append(new String(bb, 0, n));
}
fr.close();// ,
System.out.println(sb);
}
/**
* .
* @throws IOException
*/
@Test
public void writeTest() throws IOException {
String writerContent = "hello world, ";//
File file = new File("C:\\Documents and Settings\\Administrator\\ \\2.txt");//
if (!file.exists()) {// ,
file.createNewFile();
}
FileWriter writer = new FileWriter(file);//
System.out.println(writer.getEncoding());
// , writer.getEncoding() , ,
writer.write(writerContent);
writer.flush();// ,
writer.close();// ,
}
}
2.バイトストリームの読み書き+BufferedInputStream/OutputStreamなし
public class ByteIOTest {
/**
*
* D D:\\xxx.ico , , E .
*
* @param args
* @throws Exception
*/
@Test
public void imageIOTest(String[] args) throws Exception {
FileInputStream in = new FileInputStream(new File("D:\\xxx.ico"));//
File file = new File("E:\\test.jpg");
if (!file.exists()) {// ,
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(new File("E:\\yuanwang.ico"));//
int n = 0;//
byte[] bb = new byte[1024];//
while ((n = in.read(bb)) != -1) {
out.write(bb, 0, n);// ,
}
out.close();//
in.close();
}
}
3.文字ストリームの読み書き+BufferedReader/Writer
public class BufferCharIOTest {
/**
* - , ->
windows gbk, gbk 。
myeclipse utf-8
,utf8 3 , gbk 2 。
, , , ,
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void readChangeTest() throws FileNotFoundException, IOException {
File file = new File("C:\\Documents and Settings\\Administrator\\ \\1.txt");//
//
//BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
InputStreamReader isr=new InputStreamReader(bis, "gbk");
char[] bb = new char[1024];//
StringBuffer sb = new StringBuffer();// , StringBuffer
int n;//
while ((n = isr.read(bb)) != -1) {
sb.append(new String(bb, 0, n));
}
isr.close();// ,
System.out.println(sb);
}
/**
* , 。
* : , 。
* @throws IOException
*/
@Test
public void readTest() throws FileNotFoundException, IOException {
File file = new File("C:\\Documents and Settings\\Administrator\\ \\1.txt");//
//
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = "";//
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();//
}
/**
* , ,
* 。
*
* @throws IOException
*/
@Test
public void writeTest() throws IOException {
File file = new File("C:\\Documents and Settings\\Administrator\\ \\2.txt");//
if (!file.exists()) {//
file.createNewFile();
}
//
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
//
bufferedWriter.write(" ");
bufferedWriter.newLine();//
bufferedWriter.write("hello world");
bufferedWriter.flush();//
bufferedWriter.close();//
}
}
4.バイトストリームの読み書き+プラスBufferedInputStream/OutputStream
public class BufferByteIOTest {
public void imageIOTest(String[] args) throws Exception {
//
BufferedInputStream in = new BufferedInputStream(new FileInputStream("F:\\xx.jpg"));
File file = new File("E:\\xx.jpg");
if (file != null) {
file.createNewFile();
}
//
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] bb = new byte[1024];//
int n;//
while ((n = in.read(bb)) != -1) {
out.write(bb, 0, n);//
}
out.close();//
in.close();
}
}