文章目录
- FileReader文件字符输入流
- FileWriter文件字符输出流
- Writer append(char c)
- Writer append(CharSequence csq)
- Writer append(CharSequence csq, int start, int end)
- abstract void close()
- abstract void flush()
- static Writer nullWriter()
- void write(char[ ] cbuf)
- abstract void write(char[ ] cbuf, int off, int len)
- void write(int c)
- void write(String str)
- void write(String str, int off, int len)
- 文本文件的复制
FileReader文件字符输入流
int read();
读入到char。Reads a single character.
int read(char[ ] cbuf);
读入到char数组。Reads characters into an array.
int read(char[ ] cbuf, int off, int len);
读入到char数组的指定位置。Reads characters into a portion of an array.
long skip(long n);
跳过n个字符。
void close();
flush(),然后关闭流。
import java.io.*;
public class FileReaderTest01 {
public static void main(String[] args) {
try(Reader reader = new FileReader("C:\\Users\\dell\\Documents\\新建文件夹\\test.txt");
){
char[] cbuf = new char[64];
int readCount;
//测试FileReader类的skip()方法
reader.skip(2);
//测试FileReader类的read()方法
while ((readCount = reader.read(cbuf))!=-1){
System.out.println(new String(cbuf));//比较一下,如果不指定偏移量和读入的readCount
System.out.println(new String(cbuf,0,readCount));
}
} catch (IOException e) {
//TWR语句不手动调用close()
throw new RuntimeException(e);
}
}
}
控制台输出结果:

FileWriter文件字符输出流
Writer append(char c)
给writer附加某个字符。Appends the specified character to this writer.
Writer append(CharSequence csq)
将指定字符序列追加到写入器。Appends the specified character sequence to this writer.
Writer append(CharSequence csq, int start, int end)
将指定字符序列的子序列追加到写入器。Appends a subsequence of the specified character sequence to this writer.
String 继承于CharSequence。
abstract void close()
flush(),然后关闭流。Closes the stream, flushing it first.
abstract void flush()
Flushes the stream.
static Writer nullWriter()
返回一个空的写入器。Returns a new Writer which discards all characters.
void write(char[ ] cbuf)
写一个char数组。Writes an array of characters.
abstract void write(char[ ] cbuf, int off, int len)
写一个char数组的一部分。Writes a portion of an array of characters.
void write(int c)
写一个字符。Writes a single character.
void write(String str)
写一个字符串。Writes a string.
void write(String str, int off, int len)
写一个字符串的一部分。Writes a portion of a string.
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class FileWriterTest01 {
public static void main(String[] args) {
try (Writer writer = new FileWriter("C:\\Users\\dell\\Documents\\新建文件夹\\test.txt", true);//如同FileOutputStream,append设true表示续写
) {
//测试append(CharSequence csq)方法向流里追加内容
writer.append("11111");
//测试FileWriter类的write()方法
writer.write("22222");
writer.write("33333", 0, 2);
writer.write("44444".toCharArray(), 0, 3);
writer.append("a");
writer.append("ha");
//手动刷新流
writer.flush();
} catch (IOException e) {
//自动close(),调用flush()后关闭流
throw new RuntimeException(e);
}
}
}
文本文件的复制
注意仅限于文本文件。
import java.io.*;
public class FileReaderWriterCopyTest {
public static void main(String[] args) {
try(
Reader reader = new FileReader("C:\\Users\\dell\\Documents\\新建文件夹\\test.txt");
Writer writer = new FileWriter("C:\\Users\\dell\\Documents\\新建文件夹\\test1.txt");
) {
char[] cbuf = new char[64];
int readCount;
while ((readCount = reader.read(cbuf))!=-1){
//向writer追加读出的字符串
writer.append(new String(cbuf,0,readCount));
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
719

被折叠的 条评论
为什么被折叠?



