本章概述:本章主要讲了BufferedWriter类,BufferedReader类,BufferedWriterDemo类的使用,以及从万维网上摘取百度网页的操作
目录
本章概述:
一、BufferedWriter的使用
1、BufferedWriterDemo
package com.tensent.class4.readerAndwriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* @Auther: Yu Panpan
* @Date: 2022/4/12 - 04 - 12 - 14:58
* @Description: com.tensent.class4.readerAndwriter
* @version: 1.0
*/
public class BufferedWriterDemo {
public static void main(String[] args) {
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try {
fileWriter = new FileWriter("d3.txt");
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(101);
bufferedWriter.write("www.baidu.com百度".toCharArray());
//写入一个换行
bufferedWriter.newLine();
bufferedWriter.write("今天天气不错!");
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
二、BufferedReader类
1、BufferedReaderDemo
package com.tensent.class4.readerAndwriter;
import com.sun.media.sound.SoftTuning;
import java.io.*;
/**
* @Auther: Yu Panpan
* @Date: 2022/4/12 - 04 - 12 - 14:58
* @Description: com.tensent.class4.readerAndwriter
* @version: 1.0
*/
public class BufferedReaderDemo {
public static void main(String[] args) {
FileReader fileReader = null;
BufferedReader reader = null;
try {
fileReader = new FileReader("a1.txt");
reader= new BufferedReader(fileReader);
// int length = 0;
// char[] chars = new char[1024];
// while((length = reader.read(chars))!=-1){
// System.out.println(new String(chars,0,length));
// }
// String s = reader.readLine();
// System.out.println(s);
// s = reader.readLine();
// System.out.println(s);
// s = reader.readLine();
// System.out.println(s);
// s = reader.readLine();
// System.out.println(s);
// s = reader.readLine();
// System.out.println(s);
String str = "";
while((str = reader.readLine())!=null){
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三、BufferedWriter的使用
1、BufferedWriterDemo
package com.tensent.class4.readerAndwriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* @Auther: Yu Panpan
* @Date: 2022/4/12 - 04 - 12 - 14:58
* @Description: com.tensent.class4.readerAndwriter
* @version: 1.0
*/
public class BufferedWriterDemo {
public static void main(String[] args) {
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try {
fileWriter = new FileWriter("d3.txt");
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(101);
bufferedWriter.write("www.baidu.com百度".toCharArray());
//写入一个换行
bufferedWriter.newLine();
bufferedWriter.write("今天天气不错!");
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、从万维网上摘取百度网页的操作
1、BaiduDemo
package com.tensent.class4.exercise;
import java.io.*;
import java.net.URL;
/**
* @Auther: Yu Panpan
* @Date: 2022/4/12 - 04 - 12 - 15:44
* @Description: com.tensent.class4.exercise
* @version: 1.0
*/
public class BaiduDemo {
public static void main(String[] args) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(new URL("http://www.baidu.com").openStream(),"utf-8"));
bufferedWriter = new BufferedWriter(new FileWriter("D:\\baidu.html"));
String str = "";
while(( str = bufferedReader.readLine() )!=null){
bufferedWriter.write(str);
bufferedWriter.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}