字符流
缓冲字符输出流
package FB;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class ZFOut {
public static void main(String[] args) {
try {
Writer w = new FileWriter("E:");
BufferedWriter bw = new BufferedWriter(w);
bw.write("asjkfkahs");
bw.close();
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
缓冲字符输入流
public class BufferedReaderPractice {
public static void main(String[] args) {
try {
// 文件输入流
Reader reader = new FileReader("");
// 不希望直接操作文件输入流,为了提升效率,使用缓冲输入流
BufferedReader br = new BufferedReader(reader);
// 缓冲输入流,比字符输入流,多出一个readLine方法,能够一次读一行
// 并且读到的是String类型,而不是char[]类型
String line = br.readLine();
System.out.println(line);
// 关闭资源
br.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
打印流
package FB;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class PrintIO {
public static void main(String[] args) {
try {
File f = new File("E:");
PrintWriter pw = new PrintWriter(f);
pw.print("alkhsfliAH");
pw.println("5614646");
pw.println("hakHFfwn");
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
输出转换流
能够将字节流,转换成字符流
package FB;
import java.io.*;
public class Inputst {
public static void main(String[] args) {
try {
OutputStream os = new FileOutputStream("E:);
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write("张三");
osw.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输入转换流
能够将字节流,转换成字符流
package FB;
import java.io.*;
public class OutputSt {
public static void main(String[] args) {
try {
InputStream is = new FileInputStream("E:");
InputStreamReader isr = new InputStreamReader(is);
StringBuilder sb = new StringBuilder();
char[] c = new char[512];
while (true){
int lin = isr.read(c);
if(lin==-1){
break;
}
sb.append(c,0,lin);
}
System.out.println(sb);
isr.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Properties对象
-
load(输入流)
加载输入流里面的配置,一般为properties文件
这种文件的格式为:键=值[换行]
本质上,与Map没有区别
使用场景上,在读配置文件时使用
package FB;
import java.io.*;
import java.util.Properties;
public class Peres {
public static void main(String[] args) {
try{
InputStream is = new FileInputStream("E:");
Properties p = new Properties();
p.load(is);
String username = p.getProperty("username");
String password = p.getProperty("password");
String monld = p.getProperty("monld","未知");
System.out.println(username);
System.out.println(password);
System.out.println(monld);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
总结
类型
-
InputStream
-
OutputStream
-
FileInputStream
-
FileOutputStream
-
BufferedInputStream
-
BufferedOutputStream
-
ObjectInputStream
-
ObjectOutputStream
-
Serializeble
-
Reader
-
Writer
-
FileReader
-
FileWriter
-
BufferedReader
-
BufferedWriter
-
PrintWriter
-
InputStreamReader
-
OutputStreamWriter
-
File
-
FileFilter
-
Properties
单词
Input
Output
Stream
Read
Write
Buffer
File
Serializable
Filter
Properties