IO流介绍
字节流
inputStream
inputSteam石java中IO库提供的一个接口 , 是一个字节流,以byte为单位读取
因为是接口所以我们使用实现类FileinputStream来实现
核心方法:
public int read() throws IOException {
return read0();
}
private native int readBytes(byte b[], int off, int len) throws IOException;
public int read(byte b[]) throws IOException {
return readBytes(b, 0, b.length);
}
public int read(byte b[], int off, int len) throws IOException {
return readBytes(b, off, len);
}
这么说太笼统了我们直接立马整出来一个栗子好吧前面的你看不懂可以先不看
public static void main( String[] args ){
String source = "E:\\hosts.txt";
String dest = "E:\\java\\host1.txt";
copyFile (source,dest);
}
public static void copyFile(String sourceFilePath, String destPath) {
try {
FileInputStream fileInputStream = new FileInputStream (sourceFilePath);
;
byte[] bytes = new byte[1024];
int len;
while ((len = fileInputStream.read (bytes)) != -1){
System.out.print(new String(bytes,0,len));
}
fileInputStream.close ();
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}
}
outputStream
因为是接口所以我们使用实现类FileoutputStream来实现
private native void write(int b, boolean append) throws IOException;
public void write(int b) throws IOException {
write(b, fdAccess.getAppend(fd));
}
private native void writeBytes(byte b[], int off, int len, boolean append)
throws IOException;
public void write(byte b[]) throws IOException {
writeBytes(b, 0, b.length, fdAccess.getAppend(fd));
}
public void write(byte b[], int off, int len) throws IOException {
writeBytes(b, off, len, fdAccess.getAppend(fd));
}
例子:
try {
FileInputStream fileInputStream = new FileInputStream (sourceFilePath);
FileOutputStream fileOutputStream = new FileOutputStream (destPath);
byte[] bytes = new byte[1024];
int len;
while ((len = fileInputStream.read (bytes)) != -1){
fileOutputStream.write (bytes , 0 , len);
}
fileInputStream.close ();
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}
字符流
字符流封装了更加适合操作哦文本字符的方法
Reader:字符输入流
public abstract class Reader implements Readable, Closeable
Reader是Java中IO库提供的另一个输入流接口,和InputStream的区别,InputStream是一个字节流,以byte为单位读取,而Reader是一个字符流,以char为单位读取
Reader主要用来读取文本字符
以实现类FileReader为例介绍
核心方法:
/**
* int read() throws IOException
* 每次读取一个字符 返回类型是int,范围0~65535
* 如果读取到文件结尾:返回-1
*/
// int read = fileReader.read();
/**
* int read(char cb[])
* 将输入流读取到char数组中
* 返回结果表示读取的数据个数
* 如果读取到文件结尾,返回-1
*
*/
char[] chars = new char[3];
// int read = fileReader.read(chars);
/**
* int read(char cbuf[], int offset, int length);
* 批量读取数据,
* 将数据读取到char数组中,从offerset位置开始,读取length长度
* 范围:offset+length<=char.length
* 返回值表示数据的个数
* 如果读取到文件结尾,返回-1
*/
// int read = fileReader.read(chars, 0, 3);
/**
* int read(java.nio.CharBuffer target)
* 将数据读取到charBuffer中
*/
//CharBuffer创建形式
CharBuffer buffer = CharBuffer.allocate(100);
// CharBuffer wrap = CharBuffer.wrap(new char[100]);
//从fileReader将内容读取到buffer中
int read = fileReader.read(buffer);
//读写模式切换
buffer.flip();
char[] chars1 = new char[read];
//将buffer中数据读取到char类型数组中
buffer.get(chars1);
//读取内容
String info = new String(chars1);
System.out.println(info);
Writer:字符输出流
是字符输出流,和OutputStream类似,不同点在于OutputStream是操作byte字节流,writer是操作char字符流
public abstract class Writer implements Appendable, Closeable, Flushable {
以实现类FileWrite为例说明
/**
* void write(int c)
* 每次写入单个字符
*/
fileWriter.write((char)'c');
char[] chars = new char[]{'c','h','i','n','a'};
/**
* void write(char cbuf[])
* 写入char数组
*/
fileWriter.write(chars);
/**
* void write(char cbuf[], int off, int len)
* 写入char数组 从off开始,读取len长度写入
*/
fileWriter.write(chars,0,chars.length);
/**
* void write(String str)
* 写入字符串到字符输出流
*/
fileWriter.write("hello");
/**
* void write(String str, int off, int len)
* 写入字符串数组
* 从off位置开始,写入len长度
*/
String s = "hello Tulun";
fileWriter.write(s,0,s.length());
//追加内容
/**
* Writer append(char c)
* 追加内容到输出流中
*/
fileWriter
.append('a')
.append('b')
.append('c');
/**
* Writer append(CharSequence csq)
*/
//创建一个buffer
CharBuffer buffer = CharBuffer.allocate(100);
//往buff写入内容
buffer.put("hello");
//读写模式切换
buffer.flip();
//将数据从CharSequence中写入字节输出流
fileWriter.append(buffer)
.append('c');
处理流
转换流InputSteamReader和OutputSteamWriter
将字节流转换为字符流
InputStreamReader isr = new InputStreamReader(new FileInputStream(Path));
BufferedReader
缓存流:效率变快
FileReader fileReader = new FileReader (path);//低级流
BufferedReader br = new BufferedReader (fileReader);//高级流也叫处理流
String len = null;
String res = "";//储存结果
while((len = br.readLine ()) != null){
System.out.print(len))
}