BufferedInputStream;BufferedOutputStream。
带缓冲区的字节输入输出流,是处理流。以流为数据源。
public class IOTest13 {
public static void main(String[] args) {
File file=new File("a.txt");//源
InputStream is = null;
BufferedInputStream bis=null;
try {
is=new FileInputStream(file);//选择流
bis=new BufferedInputStream(is);
int temp;
while((temp=is.read())!=-1){ //循环读取内容
System.out.print((char)temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {//释放资源,只需处理流释放即可
if(bis!=null){
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class IOTest14 {
public static void main(String[] args) {
readFile("D:\\IDEACODE\\Test2\\b.txt");
}
static void readFile(String oldFile){
File file=new File(oldFile);
BufferedReader reader=null;
try {
reader=new BufferedReader(new FileReader(file));
String len=null;
while((len=reader.readLine())!=null){ //逐行读取
System.out.println(len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(reader!=null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class IOTest15 {
public static void main(String[] args) {
writerFile("g.txt");
}
static void writerFile(String filePath){
File file=new File(filePath);
BufferedWriter writer=null;
try {
writer=new BufferedWriter(new FileWriter(file));
writer.append("DOTA2");
writer.newLine();//换行
writer.append("哈哈");
writer.newLine();//换行
writer.append("kk");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(writer!=null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}