二进制文件流练习题

题目描述

(1)搜索C盘,找到一个BMP文件
(2)使用FileInputStream和FileOutputStream及BufferedInputStream、BufferedOutputStream完成文件的复制。
(3)将该文件复制到D:\4.bmp中。
代码实现

import java.io.*;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.File;
import java.util.*;
public class copy {
	public static void main(String[] args){
		int b = 0;
		FileInputStream in = null;
		FileOutputStream out = null;
		int i = 0;
		try {
			in = new FileInputStream("c:\\aaa.bmp");
			out = new FileOutputStream("d:\\4.bmp");
			BufferedInputStream bIn = new BufferedInputStream(in);
			BufferedOutputStream bOut = new BufferedOutputStream(out);
			while ((b=bIn.read()) != -1) {
				bOut.write(b);
			}
			bIn.close();
			bOut.close();
		} catch (FileNotFoundException e) {
			System.out.println("找不到指定文件!");
			System.exit(-1);
		} catch(IOException e){
			System.out.println("文件复制错误!");
			System.exit(-1);
		}
		System.out.println("文件已复制");
	}
}

题目描述
编写程序,将c盘目录下的所有目录名写到d盘的name1.txt中。要求:使用FileWriter和BufferWriter完成。
代码实现

import java.io.*;
import  java.io.IOException;
import  java.io.FileReader;
import  java.io.FileWriter;
import  java.io.BufferedWriter;
public class mulu {
public static void main(String[] args) throws IOException{
		File dir=new  File("c:\\");
		File [] subfile=dir.listFiles();
		for(File file:subfile){
			String a=file.getName();
		FileWriter  fw=new FileWriter("d:\\name1.txt");
		BufferedWriter  bfw=new  BufferedWriter(fw);	
		bfw.write( a);
		bfw.close();
		FileReader in = new FileReader("d:\\name1.txt");
		BufferedReader bIn = new BufferedReader(in);
		String s = null;
		while ((s = bIn.readLine()) != null) {
		   System.out.println(s);
		} 
		bIn.close();
		}
	}
}

题目描述
编写程序,将d:\b.txt拷贝到e:\bb.txt,内容如下:使用字符流完成.。在拷贝过程中,将AA替换成BB。其中a.txt的内容如下:
CC DD BB DD
AA CC BB BB
AA DD BB CC
AA CC DD BB
代码实现

import java.io.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
public class kaobei {
	public static void main(String[] args)throws IOException {
		FileWriter out = new FileWriter("d:\\b.txt");
		BufferedWriter bout = new BufferedWriter(out);
		String str1=new String("CC DD BB  DD\n AA CC BB  BB\n AA DD BB  CC\n AA CC  DD BB");
		bout.write(str1);
		bout.close();
       FileReader in = new FileReader("d:\\b.txt");
       BufferedReader bIn = new BufferedReader(in);
		bIn.close();
		System.out.print(str1);
		String str2=new String("AA");
		str2=str1.replaceAll("AA", "BB");
		FileWriter out1 = new FileWriter("e:\\bb.txt");
		BufferedWriter bout1 = new BufferedWriter(out1);
		bout1.write(str2);
		bout1.close();
       FileReader in1 = new FileReader("e:\\bb.txt");
       BufferedReader bIn1 = new BufferedReader(in1);
		bIn1.close();
		System.out.print(str2);
	}
}

感谢读者朋友们阅读,喜欢可以关注一下哦!

你可能感兴趣的:(二进制文件流练习题)