InputStream使用示例:
//第一种实现方式:每次按照一个字节接收直到文件结束
package com.wyz.test;
import java.io.FileInputStream;
import java.io.IOException;
public class test {
public static void main(String[] args) {
String filePath = "E:\\hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while ((readData = fileInputStream.read()) != -1){
System.out.print((char) readData);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//第二种实现方式:按照自定义数组的大小接收字节直至文件结束
package com.wyz.test;
import java.io.FileInputStream;
import java.io.IOException;
public class test {
public static void main(String[] args) {
String filePath = "E:\\hello.txt";
byte[] buff = new byte[8];
int readLength = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while ((readLength = fileInputStream.read(buff)) != -1){
System.out.print(new String(buff, 0, readLength));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
OutputStream使用示例:
package com.wyz.test;
import java.io.FileOutputStream;
import java.io.IOException;
public class test {
public static void main(String[] args) {
String filePath = "E:\\hello.txt";
int readData = 0;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filePath); //这样使用是覆盖文件原来的内容
// 下面一条语句是追加内容到文件末尾,不会覆盖文件之前的内容
// fileOutputStream = new FileOutputStream(filePath, true);
fileOutputStream.write('H');
String str = "Hello OutputStream!";
fileOutputStream.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileReader使用示例:
//第一种实现方式:每次按照一个字符接收直到文件结束
package com.wyz.test;
import java.io.FileReader;
import java.io.IOException;
public class test {
public static void main(String[] args) {
String path = "E:\\hello.txt";
FileReader fileReader = null;
int data = 0;
try {
fileReader = new FileReader(path);
while((data = fileReader.read()) != -1){
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileReader != null){
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//第二种实现方式:按照自定义数组的大小接收字符直至文件结束
package com.wyz.test;
import java.io.FileReader;
import java.io.IOException;
public class test {
public static void main(String[] args) {
String path = "E:\\hello.txt";
FileReader fileReader = null;
char[] buff = new char[8];
int readLength = 0;
try {
fileReader = new FileReader(path);
while((readLength = fileReader.read(buff)) != -1){
System.out.print(new String(buff, 0, readLength));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileReader != null){
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileWriter使用示例:
package com.wyz.test;
import java.io.FileWriter;
import java.io.IOException;
public class test {
public static void main(String[] args) {
String path = "E:\\hello.txt";
FileWriter fileWriter = null;
char[] chars = {'h', 'e', 'l', 'l', 'o'};
try {
fileWriter = new FileWriter(path);
fileWriter.write("H"); //写入单个字符
fileWriter.write(chars); //写入数组
fileWriter.write("Hello FileWriter!".toCharArray(), 0, 5); //写入数组的指定部分
fileWriter.write("Hello FileWriter!"); //写入指定字符串
fileWriter.write("Hello FileWriter!", 0, 5); //写入字符串的指定部分
} catch (IOException e) {
e.printStackTrace();
} finally {
//attention:一定要关闭流或者flush才能真正把数据写入到文件
try {
fileWriter.close(); //close()底层调用了flush()并关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}