package com.io; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* 字节流转为字符流读入
* @author ganhang
*
*/
public class InputStreamReaderDemo {
public static void main(String[] args) {
File file =new File("1.txt");
InputStream in;
try {
in = new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(in);
int len=-1;
StringBuffer sb=new StringBuffer();
char []b=new char[10];
while((len=isr.read(b))!=-1){
sb.append(b,0,len);
}
isr.close();
in.close();
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
/**
* 字符流转为字节写入
* @author ganhang
*
*/
public class OutputStreamWriterDemo {
public static void main(String[] args) {
File file=new File ("1.txt");
OutputStream out;
try {
out = new FileOutputStream(file,true);
OutputStreamWriter osw=new OutputStreamWriter(out);
String info="哈哈哈哈哈哈";
osw.write(info);
osw.close();
out.close();
System.out.println("写入成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}