java IO流
字节流
InputStream/OutputStream(抽象类)
文件流:
FileInputStream:
read()
文件输入流,将文件中的内容读取到程序中
FileOutputStream:
write()
文件输出流,将程序中的内容写到文件中
缓冲流:
在内存中开辟一段缓冲区,减少对硬盘的操作,提高效率
缓冲区中的内容由系统自动刷新,当缓冲取满了之后,将缓冲区的内容一次性写入到文件中
BfferedInputStream:缓冲字节输出流
read()
BfferedOutputStream:基于文件输出流的基础上进行包装
write()
文件流
FileInputStream/FileOutputStream
文件输入流
public static void main(String[] args) {
File file = new File("D:/Desktop/test/test01.txt");
try {
if (!file.exists()) {
file.createNewFile();
}
OutputStream o = new FileOutputStream(file,true);
o.write('a');
String str = "fadskj;as'dsfzsdfi";
o.write(str.getBytes());
o.write(str.getBytes(),0,str.length());
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
文件输出流
public static void main(String[] args) {
File file = new File("D:/Desktop/test/test01.txt");
if (!file.exists()) {
return;
}
try {
InputStream i = new FileInputStream(file);
int read = i.read();
System.out.println((char)read);
byte[] b = new byte[2048];
int read2 = i.read(b);
System.out.println(read2);
String str = new String(b,0,read2);
System.out.println(str);
int read3 = i.read(b,100,50);
System.out.println(read3);
} catch (Exception e) {
e.printStackTrace();
}
}
}
文件流复制
public static void main(String[] args) {
File file1 = new File("D:/Desktop/test/test01.txt");
File file2 = new File("D:/Desktop/test/test01_copy1.txt");
if (!file1.exists()) {
return;
}
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
byte[] b = new byte[2048];
while(true){
int read = fis.read(b);
if (read == -1) {
break;
}
fos.write(b, 0, read);
}
} catch (Exception e) {
e.printStackTrace();
}
}
缓冲字节流
BfferedInputStream/BfferedOutputStream
缓冲输入流
public static void main(String[] args) {
File file = new File("D:/Desktop/test/test001.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(file,true);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write('a');
bos.write("dfaasdfa".getBytes());
bos.flush();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
缓冲输出流
public static void main(String[] args) {
File file = new File("D:/Desktop/test/test001.txt");
if (!file.exists()) {
return;
}
try (
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
){
int read = bis.read();
System.out.println(read);
byte[] b = new byte[2048];
int read2 = bis.read(b);
String str = new String(b,0,read2);
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
缓冲流复制
public static void main(String[] args) {
File file1 = new File("D:/Desktop/test/test01.txt");
File file2 = new File("D:/Desktop/test/test01_copy01.txt");
if (!file1.exists()) {
return;
}
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try (
FileInputStream fis = new FileInputStream(file1);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(file2);
BufferedOutputStream bos = new BufferedOutputStream(fos);
){
byte[] b = new byte[2048];
while(true){
int read = fis.read(b);
if (read == -1) {
break;
}
fos.write(b, 0, read);
}
} catch (Exception e) {
e.printStackTrace();
}
}
用完关闭类的方式
FileOutputStream fos = null;
try {
fos = new FileOutputStream("test.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try (
FileOutputStream fos2 = new FileOutputStream("test.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos2);
){
} catch (Exception e) {
}
字符流
Reader/Writer
字符流:
InputStreamReader/OutputStreamWriter
缓冲字符流
BufferedReader/PrintWriter
字符输入流
public static void main(String[] args) {
File file = new File("D:/Desktop/test/test002.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try (
FileOutputStream fos = new FileOutputStream(file,true);
OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
){
osw.write("adskfjdfifasijenii");
String str = "法律等放假安排骄傲的风景";
osw.write(str.toCharArray());
} catch (Exception e) {
e.printStackTrace();
}
}
字符输出流
public static void main(String[] args) {
File file = new File("D:/Desktop/test/test002.txt");
if (!file.exists()) {
return;
}
try (
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis,"GBK");
){
int read = isr.read();
char[] c = new char[2048];
int read2 = isr.read(c);
String str = new String(c,0,read2);
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
缓冲字符流
BufferedReader:缓冲字符输入流
PrintWriter:缓冲字符输出流
可以实现数据的按行读写
缓冲字符输出流
public static void main(String[] args) {
File file = new File("D:/Desktop/test/test1009.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try (
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
PrintWriter pw = new PrintWriter(osw,true);
){
pw.println("afsdfalskdfjoiasjj");
pw.println("地方静安寺女你打开复返");
} catch (Exception e) {
e.printStackTrace();
}
}
缓冲字符输入流
public static void main(String[] args) {
File file = new File("./src/_20211009/Demo04.java");
if (!file.exists()) {
return;
}
try (
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "GBK");
BufferedReader br = new BufferedReader(isr);
){
String readLine = null;
while((readLine = br.readLine()) != null){
System.out.println(readLine);
}
} catch (Exception e) {
e.printStackTrace();
}
}