一、字节流
1、字节流写出数据
//字节流输出数据到文本
public void writeFile() throws IOException {
File file1=new File("E:\\iostream.txt");
while(file1.exists()) {
file1.createNewFile();
}
String string="io流,了解一下!";
byte[] bytes=string.getBytes();
FileOutputStream fos=new FileOutputStream(file1);
fos.write(bytes,0, bytes.length);
fos.close();
}
2、字节流读取文本数据
//字节流读取文本数据
public void readFile() throws IOException {
FileInputStream fis=new FileInputStream(new File("E:\\iostream.txt"));
byte[] bytes=new byte[2];
int len=0;
while ((len=fis.read(bytes))!=-1) {
System.out.print(new String(bytes,0,bytes.length));
}
fis.close();
}
3、字节流复制文本
//字节流复制文本
public void copyFile() throws IOException {
File file1=new File("E:\\iostream.txt");
String file2pathString=file1.getParent()+"file2.txt";
System.out.println(file2pathString);
File file2=new File(file2pathString);
while (file2.createNewFile()) {
break;
}
FileInputStream fis=new FileInputStream(file1);
FileOutputStream fos=new FileOutputStream(file2);
byte[] bytes=new byte[2];
int len=0;
while((len=fis.read(bytes))!=-1){
fos.write(bytes, 0, len);
fos.flush();
}
fos.close();
fis.close();
}
4、字节流复制图片
//字节流复制图片
public void copyImg() throws IOException {
File img1=new File("E:\\img1.jpg");
File img2=new File(img1.getParentFile()+"img2.jpg");
FileInputStream fis=new FileInputStream(img1);
FileOutputStream fos=new FileOutputStream(img2);
while (img2.createNewFile()) {
break;
}
byte[] bytes=new byte[1024];
int len=0;
while((len=fis.read(bytes))!=-1){
fos.write(bytes, 0, len);
fos.flush();
}
fos.close();
fis.close();
}
二、字符流
1、字符流读取文本
//字符流读取数据
public void charRead() throws IOException {
File file1=new File("E:\\iostream.txt");
FileReader fr=new FileReader(file1);
int text;
while ((text=fr.read())!=-1) {
System.out.print((char)text);
}
fr.close();
}
2、字符流写出数据
//字符流输出数据到文本
public void charWrite() throws IOException {
File file1=new File("E:\\charfile.txt");
while(file1.exists()) {
file1.createNewFile();
}
FileWriter fw=new FileWriter(file1);
String string="io流,了解一下!";
fw.write(string);
fw.flush();
fw.close();
}
3、字符流复制文本
//字符流复制文本
public void charCopy() throws IOException {
File file1=new File("E:\\charfile.txt");
File file2=new File(file1.getParent()+"charCopy.txt");
FileReader fr=new FileReader(file1);
FileWriter fw=new FileWriter(file2);
int text;
while((text=fr.read())!=-1) {
fw.write((char)text);
fw.flush();
}
fw.close();
fr.close();
}
三、缓冲流
练习:将以下文本信息进行排序
3.侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉
以咨之,然后施行,必得裨补阙漏,有所广益。
8.愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其
咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏,臣不胜受恩感激。
4.将军向宠,性行淑均,晓畅军事,试用之于昔日,先帝称之曰能,是以众议举宠为督。愚以为营中之事,悉以咨之,
必能使行阵和睦,优劣得所。
2.宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不
宜偏私,使内外异法也。
1.先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外
者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以
塞忠谏之路也。
9.今当远离,临表涕零,不知所言。
6.臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣
以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间,尔来二十有一年矣。
7.先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐付托不效,以伤先帝之明,故五月渡泸,深入不毛。
今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛
下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。
5.亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息
痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也。
//字符缓冲流排序文件
public void readBuffer() throws IOException {
File file1=new File("E:\\bufferfile.txt");
File file2=new File("E:\\bufferout.txt");
while (file2.exists()) {
file2.createNewFile();
}
BufferedReader br=new BufferedReader(new FileReader(file1));
BufferedWriter bw=new BufferedWriter(new FileWriter(file2));
String line;
Map<String, String> map=new HashMap<String, String>();
while((line=br.readLine())!=null) {
String[] strs=line.split("\\.");
map.put(strs[0], strs[1]);
}
for(int i=1;i<=map.size();i++) {
bw.write(map.get(i+""));
}
bw.close();
br.close();
}