字节流不能直接操作字符,因为一个字符有两个字节,而字节流一次只能操作一个字节.所以便有了字符流的存在。相对于字节流,字符流快捷且空间大。
输出字符流:把要写入文件的字符序列(实际是unicode码元序列)转为指定编码方式下的字节序列,然后在写入文件中。
输入字符流:把要读取的字节序列按照指定编码方式转为相应的字符序列(实际是unicode码元序列),从而写入内存中。
@Test
public void test1() throws Exception {
//字节流不能写字符流 浪费空间
FileInputStream f = new FileInputStream("C:/Users/流/aa.txt");
int a;
while((a = f.read())!=-1){
System.out.println((char)a);
}
f.close();
}
@Test
public void test2() throws Exception {
FileWriter w = new FileWriter("C:/Users/流/aa.txt");
// w.write('A');
// w.write('哈'); // 写入字符
w.write("AASFDDVV哈哈那时候后发给你吧"); // 写入字符串
w.close();
}
@Test
public void test3() throws Exception {
FileReader r = new FileReader("C:/Users/流/aa.txt");
int a;
while((a = r.read())!=-1){
System.out.print((char)a);
}
r.close();
}
缓冲流(BufferedReader里的readLine()方法读取方便把快捷):
@Test
public void test4() throws Exception {
BufferedWriter bw = new BufferedWriter(
new FileWriter("C:/Users/流/aa.txt")
);
//FileWriter w = new FileWriter("C:/Users/流/aa.txt");
// w.write('A');
// w.write('哈'); // 写入字符
bw.write("AASFDDVV哈哈那时候后发给你吧"); // 写入字符串
bw.flush();
bw.close();
}
@Test
public void test5() throws Exception {
BufferedReader br = new BufferedReader(
new FileReader("C:/Users/流/aa.txt")
);
// FileReader r = new FileReader("C:/Users/流/aa.txt");
String mesg = br.readLine();
System.out.println(mesg);
br.close();
}
标准输入输出流
package edu.xalead.test;
import java.io.*;
public class 标准流 {
public static void main(String[] args) throws Exception {
InputStreamReader ir = new InputStreamReader(System.in); //字节流包装成字符流
BufferedReader br = new BufferedReader(ir);
for(;;) {
String mesg = br.readLine();
System.out.println(mesg);
}
}
}
@Test
public void test6() throws Exception {
InputStreamReader ir = new InputStreamReader(System.in); //字节流包装成字符流
BufferedReader br = new BufferedReader(ir);
String mesg = br.readLine();
System.out.println(mesg);
}
@Test
public void test7() throws Exception {
OutputStreamWriter ow = new OutputStreamWriter(System.out);
BufferedWriter bw = new BufferedWriter(ow);
bw.write("hello中国");
bw.flush();
//bw.close();
System.out.println("华泰股份体育局");
}
PrintStream是I/O(输入/输出)流的一种,属于输出流,通过PrintStream可以将文字打印到文件或者网络中去,通俗点就是可以通过它把一段文字保存到文件中,或者如果是网络链接的话可以将文字发送到网络的那端。创建新的打印流,就是创建一个新的PrintStream对象,通过newPrintStream(OutStream/或者其他参数)完成。
@Test
//字节流
public void test8() throws Exception {
PrintStream s = new PrintStream("C:/Users/流/b.txt");
s.println("helllo world!");
s.flush();
s.close();
}
@Test
//字符流
public void test9() throws Exception {
PrintWriter pw = new PrintWriter("C:/Users/流/b.txt");
pw.println(212 + "sgbb" + 2355646); //默认有回车换行符
pw.flush();
pw.close();
}
File类是操作文件和目录的(判断文件或目录是否存在)
@Test
public void test11() throws Exception {
File f = new File("C:\\afavg/sgs.txt");
if(f.exists()){
System.out.println("存在");
}else{
System.out.println("不存在");
//f.mkdir();
System.out.println(f.mkdir());
f.createNewFile();
}
}
@Test
public void test12() throws Exception {
File f = new File("C:\\afavg/sgs.txt");
System.out.println(f.isDirectory());
System.out.println(f.isFile());
}
@Test
public void test13() throws Exception {
File f = new File("C:\\Program Files (x86)");
if (f.isDirectory()) {
System.out.println("[" + f.getName() + "]");
} else {
System.out.println(f.getName());
// File[] ff = f.listFiles();
// if(ff!= null && ff.length>0 ){
// listFile(ff,++level);
// }
}
File[] fs = f.listFiles();
if(fs !=null && fs.length>0){
listFile(fs,1);
}
}
private void writeBlank(int level){
for(int i =0;i<level;i++){
System.out.print(" ");
}
}
private void listFile ( File[]fs,int level){
for(File f:fs){
writeBlank(level);
if(f.isDirectory()){
System.out.println("[" + f.getName() + "]");
File[] ff = f.listFiles();
if(ff != null && ff.length>0){
listFile(ff,++level);
}
}else{
System.out.println(f.getName());
}
}
}