标签:
根据给定的 File 对象构造一个 FileWriter 对象
根据给定的文件名构造一个 FileWriter 对象
实例01:
package cn.itcast04;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo01 {
public static void main(String[] args) throws IOException {
//创建IO对象
FileWriter fw = new FileWriter("d.txt" );
String s = "I love JAVA";
//写入字符串
fw.write( "Hello World");
//写入字符串的一部分
fw.write(s, 1,5);
//写入字符数组
char[] chars = new char[]{ ‘a‘, ‘c‘, ‘b‘, ‘e‘};
fw.write(chars);
//写入字符数组的一部分
fw.write(chars,0,3);
fw.close();
}
}
实例02:
package cn.itcast04;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo01 {
public static void main(String[] args) throws IOException {
File file= new File("d.txt" );
FileReader fr = new FileReader(file);
//读取单个字符
int c;
while((c=fr.read())!=-1)
{
System. out.println((char)c);
}
fr.close();
System. out.println("================================" );
File file2= new File("d.txt" );
FileReader fr2 = new FileReader(file2);
//读入一个数组
char[] chars = new char[( int)file2.length()];
int len;
while((len=fr2.read(chars))!=-1)
{
String s = new String(chars,0,len);
System. out.println(s);
}
fr2.close();
}
}
JavaIO(04)字符流--Writer and Reader
标签:
原文地址:http://www.cnblogs.com/qlwang/p/5613744.html