黑马程序员_io流的一些基本代码
------- android培训、java培训、期待与您交流! ----------
使用流,读取文本文件中的信息,将将信息显示在控制台. 考虑使用高效的方式读取.
public static void inPutStream(File file) throws IOException {
// 创建FileInputStream
FileInputStream fis = new FileInputStream(file);
// 创建byte数组,用于缓存
byte[] buffer = new byte[1024];
// 定义变量,用于获取字节的个数
int len;
// 循环读取
while ((len = fis.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, len));
}
// 关闭流,释放资源
fis.close();
使用流,将程序中的信息,保存到指定路径的指定文件中. 考虑快速的写出信息. 可以是一句字符串
public static void outputStream() throws IOException {
File file2 = new File("E:\\aa.txt");
String mess = "hello,word";
// 创建FileOutputStream
FileOutputStream fos = new FileOutputStream(file2);
// 写出到文本中,用write--》mess要进行转换用getByte(将String 编码为 byte 序列,并将结果存储到一个新的
// byte 数组中。)
fos.write(mess.getBytes());
}
使用流,实现文件的拷贝,要求可以拷贝任意格式的文件,并提高效率.
public static void copy(File file) throws IOException {
File file2 = new File("E:\\copy.zip");
// 创建字节输入流和输出流
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file2);
// 定义byte数组,用来缓存,实现高效读取
byte[] buffer = new byte[1024*8];
// 定义变量
int len;
// 循环读取
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
// 关闭流,释放资源
fis.close();
fos.close();
}
public static void main(String[] args) throws IOException {
// 创建File对象
File file = new File("E:\\qq.zip");
// inPutStream(file);
// outputStream();
copy(file);
}
在平常的copy中最好用字节流操作,不要用字符流操作,因为java中基本单位是字节,以字节来copy不会导致数据的流逝。不会造成copy后文件无法读取。
但是,读和写操作还是用字符流操作,因为字节来读取的话,像中文这些一个字符两三个字节的,有字节来读,会造成读半个字符,造成乱码。
字符流:
public static void main(String[] args) throws IOException {
File file = new File("E:\\a.txt");
//創建字符輸入流FileReader
FileReader fr = new FileReader(file);
//定義字符數組
char[] buffer = new char[1024*8];
//定義變量,用來記錄字符的個數
int len;
//循環記錄
while((len=fr.read(buffer))!=-1){
System.out.println(new String(buffer,0,len));
}
//關閉流,釋放資源
fr.close();
}
一次读取一个文本行:
public static void main(String[] args) throws IOException {
// 1. 创建字符输入流
FileReader fr = new FileReader("d:\\a.txt");
// 2. 创建字符缓冲输入流. 注意: 字符流缓冲流,不能直接关联文件,依赖一个普通字符输入流.
BufferedReader br = new BufferedReader(fr);
// 3. 实现高效的读取. 一次读取一行.
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
// 4. 关闭流
br.close();
}
一次写一个文本行:
public static void main(String[] args) throws IOException {
// 1. 创建普通的字符输出流.
FileWriter fw = new FileWriter("c:\\a.txt");
// 2. 创建字符输出流的缓冲流.
BufferedWriter bw = new BufferedWriter(fw);
// 3. 缓冲流高效的写出数据.,
bw.write("大家好!!!");
bw.newLine();
bw.write("恭喜发财!!!");
bw.newLine();
//4. 刷新缓冲
bw.flush();
bw.close();
}
合并文本:
public static void mergeFile4() throws FileNotFoundException, IOException {
FileInputStream fis1 = new FileInputStream("c:\\a.txt");
FileInputStream fis2 = new FileInputStream("c:\\b.txt");
FileInputStream fis3 = new FileInputStream("c:\\c.txt");
LinkedHashSet<FileInputStream> set = new LinkedHashSet<FileInputStream>();
set.add(fis1);
set.add(fis2);
set.add(fis3);
final Iterator<FileInputStream> it = set.iterator();
FileOutputStream fos = new FileOutputStream("c:\\z.txt");
SequenceInputStream seq = new SequenceInputStream(
new Enumeration<InputStream>() {
@Override
public boolean hasMoreElements() {
return it.hasNext();
}
@Override
public InputStream nextElement() {
return it.next();
}
});
int data;
while ((data = seq.read()) != -1) {
fos.write(data);
}
seq.close();
fos.close();
}