java学习之IO字符流

 package com.io;

 import java.io.*;
/**
* 文件字符流的读取
* @author ganhang
*
*/
public class FileReaderDemo {
public static void read(){
File file =new File("1.txt");
try {
FileReader fr=new FileReader(file);
StringBuffer sb=new StringBuffer();
char [] cbuf=new char[10];
int len=-1;
while((len=fr.read(cbuf))!=-1){
sb.append(cbuf,0,len);
}
fr.close();
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
public static void main(String[] args) {
read();
}
}
 package com.io;

 import java.io.*;
/**
* 文件字符流的写入
* @author ganhang
*
*/
public class FileWriterDemo {
public static void write(){
File file =new File("1.txt");
try {
FileWriter fr = new FileWriter(file,true);
String str="哈哈哈哈";
fr.write(str);
fr.close();
System.out.println("写入成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
write();
}
}
上一篇:XHTML CSS基础知识


下一篇:jquery ui时间滚轴