一、作家の
輸入java.io.FileNotFoundException。
輸入java.io.FileOutputStreamは、
インポートにjava.io.IOException;
インポートにjava.io.OutputStream;
輸入java.io.OutputStreamWriterが、
インポートのjava.io.Writer;
パブリッククラスReaderWriter2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
OutputStream outStream=null;
Writer writer=null;
try {
outStream=new FileOutputStream("/Users/hanzhao/Desktop/daleilei.txt",true);
writer=new OutputStreamWriter(outStream);
writer.write("我");
writer.write("lalalalallalalal找找找找dfdsfsdafadsfd");
writer.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("file is not exist"+e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("writer is wrong"+e.getMessage());
}finally {
IOUtils.closeQuietly(outStream);
IOUtils.closeQuietly(writer);
}
}
}
二、リーダー
輸入java.io.FileInputStream;
輸入java.io.FileNotFoundException。
インポートにjava.io.IOException;
輸入java.io.InputStreamを。
輸入java.io.InputStreamReader。
インポートするjava.io.Reader;
パブリッククラスReaderWriterTest1 {
public static void main(String[] args) {
// reader inputstream
InputStream inStream=null;
Reader reader=null;
try {
inStream=new FileInputStream("/Users/hanzhao/Desktop/xiaoshuo.txt");
reader=new InputStreamReader(inStream);
int i;
//while((i=reader.read())>=0)
while((i=reader.read())!=-1){
char ch=(char)i;//强制转换
System.out.print(ch);
}
}catch(FileNotFoundException ex){
System.out.println("file is not found"+ex.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(reader);
}
}
}