字符流
上一节学会了按字节
进行操作的–字符流操作方法,InputStream
,OutputStream
。
本节学习专门用于字符
的形式读取和写入数据的方法
Reader
字符输入流Writer
字符输出流
package stream;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class Test_Reader_Writer { //读取文件 public static void readfile(File f) { try (FileReader fr = new FileReader(f)) { char[] all = new char[(int)f.length()]; fr.read(all); for(char b:all) System.out.print(b); } catch(IOException e) { e.printStackTrace(); } } //写文件 public static void writefile(File f) { try(FileWriter fr = new FileWriter(f)){ String a = "today."; char []s2c = a.toCharArray(); fr.write(s2c); } catch(IOException e) { e.printStackTrace(); } } //加密文件 public static void encodeFile(File encodingFile, File encodedFile) { if(encodingFile.length()==0) return; char[] encoding = new char[(int)encodingFile.length()]; char[] encoded = new char[(int)encodingFile.length()]; try{ FileReader fr = new FileReader(encodingFile); fr.read(encoding); fr.close(); } catch(IOException e) { e.printStackTrace(); } //processing System.out.print("未加密的内容:"); for(char b:encoding) System.out.print(b); System.out.println(); for(int i=0; i
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Arrays; public class Main { public static void main(String[] args) { // int eachSize = 100 * 1024; // 100k // File srcFile = new File("f:/testfile/0321.pptx"); // OperateFile.splitFile(srcFile, eachSize); // OperateFile.mergeFile(8,eachSize); File f = new File("f:/testfile/file.txt"); Test_Reader_Writer.readfile(f); Test_Reader_Writer.writefile(f); File file_textConding = new File("f:/testfile/coding.txt"); File file_textConding_out = new File("f:/testfile/coded.txt"); Test_Reader_Writer.encodeFile(file_textConding, file_textConding_out); } }