
// 【I/O】input output :Java对系统中的文件系统的支持 // 【i/o体系】: // 1.处理单位:字节流,字符流 // 2.处理流向:输入流,输出流 // 3.处理功能:节点流,处理流
// 字节流:适合处理音频,视频,图片 读取汉字 分成三个数字 // 字符流:通常适合用于处理汉字多的文本文件 读取汉字 分成一个数字 // write 不可以写空 append可以为空
// 字节流的读取:把A文件的内容读取到B文件
FileInputStream input=null; FileOutputStream output=null;
input=new FileInputStream(path); //文件找不到异常 FileNotFoundException output=new FileOutputStream(path); 覆盖
output=new FileOutputStream(path,true); 追加
int b=0;
while ((b =input.read())!=-1){//read I0异常 read 返回值为int -1代表没有值
System.out.println((char) b);//读取汉字会乱码 覆盖
output.write(b);
}
关闭输入流 input.close();
关闭输出流 output.close();
// 字节流的读取,字符串写入b文件
String s="hehehhe"; byte[] bs=s.getBytes(); output.write(bs);
// 字节流的读取:把A文件的内容读取到B文件
FileInputStream input = null;
FileOutputStream output = null;
// 读A文件
input = new FileInputStream(path);
output = new FileOutputStream(path);
// output=new FileOutputStream(path,true);//追加
int b = 0;
while ((b = input.read()) != -1) {//read I0异常 -1代表没有值
System.out.println((char) b);//读取汉字会乱码 覆盖
output.write(b);
// 关闭输入流 input.close();
// 关闭输出流 output.close();
字符流
public static void example5() throws IOException {
FileReader input = null;
FileWriter output = null;
input = new FileReader("F:\\idea\\IntelliJ IDEA 2020.1.1\\Project\\src\\com\\hzh\\javase\\day019\\a1.txt");
output = new FileWriter("F:\\idea\\IntelliJ IDEA 2020.1.1\\Project\\src\\com\\hzh\\javase\\day019\\b5.txt");
int b = 0;
while (b != -1) {
b = input.read();
System.out.println((char) b);// output.write(b);
// System.out.println(b);
//使用字符流汉字不乱码
}
input.close();
output.close();
}
// 字符流的缓冲流读取
public static void exampl6() throws IOException {
BufferedReader input = null;
BufferedWriter output = null;
input = new BufferedReader(new FileReader(path) {
});
output = new BufferedWriter(new FileWriter(path));
String str = null;
// input.readLine(); //跳过第一行
while ((str = input.readLine()) != null) {
System.out.println(str);
output.write(str);
output.newLine();//添加换行
// output.write(null);报错
//append不会叠加
output.append(null); //显示"null"字符串
output.append("\n");
}
input.close();
out.plush();//刷新
output.close();
}
}
3718

被折叠的 条评论
为什么被折叠?



