java day21
字节流
字节流的输出流
FileInputStream类
package demo01;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class demo02InputStrean {
public static void main(String[] args) throws IOException {
FileInputStream fis= new FileInputStream(“a.txt”);
//byte[] bytes =new byte[2];
//int len= fis.read(bytes);
//System.out.println(“读取到的长度”+len);
/System.out.println(bytes);
System.out.println(bytes[0]);//取出数组的值
System.out.println(bytes[1]);
System.out.println(Arrays.toString(bytes));/
//System.out.println(new String(bytes));
len= fis.read(bytes);
System.out.println(“读取到的长度”+len);
System.out.println(new String(bytes));
len= fis.read(bytes);
System.out.println(len);
System.out.println(new String(bytes));
len= fis.read(bytes);
System.out.println(“读取到的长度”+len);
System.out.println(new String(bytes));
len= fis.read(bytes);
System.out.println(“读取到的长度”+len);
System.out.println(new String(bytes));//两个字节
byte[] bytes =new byte[9];
int len =0;
while((len=fis.read(bytes))!=-1) {
System.out.println(len);
System.out.println(new String(bytes));
}
}
}
字节流的输入流(InputStream)
package demo01;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class InputStrean {
public static void main(String[] args) throws IOException {
FileInputStream fis= new FileInputStream(“a.txt”);
int len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
}
}
}
字符流
字符流输入(Reader)
java . io.Reader抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输入流的基本共性功能方法
I public void close() :关闭此流并释放与此流相关联的任何系统资源
public int read() | :从输入流读取一一个字符
public int read(char[] cbuf) ]:从输入流中读取一些字符,并将它们存储到字符数组cbuf中
FileReader类
读取字符数据
package demo02;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo02Reaber {
public static void main(String[] args) throws IOException {
FileReader fis= new FileReader(“D:\exlipse\eclipse-workspace\Day21.java\a.txt”);
char[] cs = new char[14];
int len =0;
while((len =fis.read(cs))!=-1) {
System.out.println(len);
System.out.println(new String(cs));
}
}
}
使用字符数组读取
package demo02;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo02Reaber {
public static void main(String[] args) throws IOException {
FileReader fis= new FileReader(“D:\exlipse\eclipse-workspace\Day21.java\a.txt”);
char[] cs = new char[2];
int len =0;
while((len =fis.read(cs))!=-1) {
System.out.println(new String(cs));
}
}
}
字符输出流(Writer)
FileWriter类
package demo02;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo02Wreitre {
public static void main(String[] args) throws IOException {
FileWriter fw= new FileWriter(“D:\exlipse\eclipse-workspace\Day21.java\a.txt”);
fw.write(99);
fw.flush();
fw.write(100);
fw.flush();
fw.write(101);
fw.flush();
fw.write(102);
fw.flush();
fw.write(103);
fw.flush();
fw.close();
//fw.write(103);
}
}
2020080605024