标签:tput mac auto split 缓冲区 释放 set copy 文件中
BufferedInputStream
,BufferedOutputStream
BufferedReader
,BufferedWriter
public BufferedInputStream(InputStream in)
:创建一个 新的缓冲输入流。public BufferedInputStream(InputStream in, int size)
: 创建一个使用给定大小输入缓冲区的新缓冲字节输出流。public BufferedOutputStream(OutputStream out)
: 创建一个新的缓冲输出流。public BufferedOnputStream(OnputStream in, int size)
: 创建一个使用给定大小输出缓冲区的新缓冲字节输出流。public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileInputStream fis = new FileInputStream("a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int len = 0;
byte[] bytes = new byte[1024];
while((len = bis.read(bytes)) != -1) {
System.out.println(new String(bytes,0,len));
}
}
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("abc".getBytes());
bos.flush();
bos.close();
}
public BufferedReader(Reader in)
:创建一个 新的缓冲输入流。public BufferedReader(Reader in, int size)
: 创建一个使用给定大小输入缓冲区的新缓冲字符输出流。public BufferedWriter(Writer out)
: 创建一个新的缓冲输出流。public BufferedWriter(Writer out, int size)
: 创建一个使用给定大小输出缓冲区的新缓冲字符输出流。public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new FileReader("b.txt"));
String s;
while((s = br.readLine()) != null) {
System.out.println(s);
}
br.close();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
for(int i = 1;i < 5; i++) {
bw.write("您好");
bw.newLine();
}
bw.close();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
long s = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] bytes = new byte[1024];
int len = 0;
while((len = bis.read(bytes)) != -1) {
bos.write(bytes,0,len);
}
bos.close();
bis.close();
fos.close();
fis.close();
long e = System.currentTimeMillis();
System.out.println((e-s));
}
/*
练习:
对文本的内容进行排序
按照(1,2,3....)顺序排序
分析:
1.创建一个HashMap集合对象,可以:存储每行文本的序号(1,2,3,..);value:存储每行的文本
2.创建字符缓冲输入流对象,构造方法中绑定字符输入流
3.创建字符缓冲输出流对象,构造方法中绑定字符输出流
4.使用字符缓冲输入流中的方法readline,逐行读取文本
5.对读取到的文本进行切割,获取行中的序号和文本内容
6.把切割好的序号和文本的内容存储到HashMap集合中(key序号是有序的,会自动排序1,2,3,4..)
7.遍历HashMap集合,获取每一个键值对
8.把每一个键值对,拼接为一个文本行
9.把拼接好的文本,使用字符缓冲输出流中的方法write,写入到文件中
10.释放资源
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
HashMap<String, String> map = new HashMap<String,String>();
BufferedReader br = new BufferedReader(new FileReader("in.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt"));
String line;
while((line = br.readLine()) != null) {
String[] split = line.split("\\.");
map.put(split[0], split[1]);
}
for(String key : map.keySet()) {
String value = map.get(key);
line = key + "." +value;
bw.write(line);
bw.newLine(); //换行,可识别不同系统的换行 windows:\r\n linux:/n mac:/r
}
bw.close();
br.close();
}
标签:tput mac auto split 缓冲区 释放 set copy 文件中
原文地址:https://www.cnblogs.com/IamHzc/p/14583845.html