目录
字节流&&缓冲字节流
字节流输出写入数据
- 一次写一个字节数据
public class file {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("fos.txt",true);
fileOutputStream.write(99);
fileOutputStream.write(100);
fileOutputStream.write(101);
fileOutputStream.close();
}
}
- 一次写入一个字节数组的数据
public class file {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("fos.txt",true);
byte[] by = {102,103,104}; //一次写一个字节数组(多个字节)
byte[] by1 = "zp".getBytes();
fileOutputStream.write(by,0,2);
fileOutputStream.write(by1);
fileOutputStream.close();
}
}
字节输入流读取数据
- 一次读取一个字节的数据
public class file {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("fos.txt");
int line;
while ((line = fileInputStream.read())!=-1){
System.out.println(line);
}
}
}
- 一次读取一个字节数组的数据
public class file {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("fox.txt");
byte[] line = new byte[10];
fileInputStream.read(line);
System.out.println(new String(line));
}
}
}
缓冲字节流写入数据
public class file {
public static void main(String[] args) throws IOException {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("fox.txt",true));
bufferedOutputStream.write("woshinima".getBytes());
bufferedOutputStream.close();
}
}
缓冲字节流读取数据
public class file {
public static void main(String[] args) throws IOException {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("fox.txt"));
byte[] bytes = new byte[1024];
bufferedInputStream.read(bytes);
System.out.println(new String(bytes));
bufferedInputStream.close();
}
}
字符流&&缓冲字符流
为什么会出现字符流
因为字节流操作中文不是特别方便,所以java就提供字符流,使用字符流可以指定编码(GBK,UTF-8)
字符输出流写入数据
public class file {
public static void main(String[] args) throws IOException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("fox1.txt"),"GBK");
outputStreamWriter.write("中国");
outputStreamWriter.close();
}
}
字符输入流读取数据
public class file {
public static void main(String[] args) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("fox1.txt"),"GBK");
char[] bytes = new char[1024];
int line;
while ((line=inputStreamReader.read(bytes))!=-1){
System.out.println( new String(bytes));
}
inputStreamReader.close();
}
}
由于字符流是以字符为单位,所以读取的时候必须读取到字符数组,而字节流是以字节为单位,所以读取的时候必须读取到字节数组
字符缓冲流写入数据
public class file {
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("fox1.txt"));
bufferedWriter.write("zzzzzz");
bufferedWriter.close();
}
}
字符缓冲流读取数据
public class file {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("fox1.txt"));
char[] chars = new char[100];
int line;
while ((line=bufferedReader.read(chars))!=-1){
System.out.println(new String(chars));
}
}
}