1、在文件的读写操作中,可以自定义一个字节数组作为缓冲区,来提高读写的效率。而java也提供了内置缓冲区的缓冲流可以高效的一次读取很多字节。请使用缓冲流完成如下需求:将"D:\mn.jpg"图片文件(也可以是其他图片文件)拷贝到当前模块目录下。
public static void main(String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\itcast\\mn.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("mm.jpg"));
byte [] by = new byte[1024];
int res ;
while ((res = bis.read(by)) != -1){
bos.write(by,0,res);
}
bis.close();
bos.close();
}
2、请使用缓冲流将D盘下的某个音频文件,拷贝到当前模块目录下,并计算拷贝过程消耗的时间,将时间(单位毫秒)打印到控制台。
public static void main(String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\Desktop\\132.mp3"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("123.mp3"));
byte [] res = new byte[1024];
int a ;
long l = System.currentTimeMillis();
while ((a = bis.read(res)) != -1){
bos.write(res,0,a);
}
long l1 = System.currentTimeMillis();
bis.close();
bos.close();
System.out.println("共耗时:"+(l-l1)+"毫秒");
}
3、现获取到一段文本,如下:
String str = “流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。”;
需要将其保存到“D:\IO流概念.txt”文件中,请编写程序实现。
public static void main(String[] args) throws Exception {
String str = "流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。";
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\IO流概念.txt"));
//使用句号分割
String[] split = str.split("。");
for (String s : split) {
//行尾添加句号
bw.write(s+"。");
//换行
bw.newLine();
}
bw.close();
}
4、已知在“D:\字符流和字节流.txt”文件中,保存着一些文本信息。
如下:
字符流的由来:因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。字节流和字符流的区别:
(1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
(2)处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。
(3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件。
请编写程序将文本内容读取出来,保存为字符串格式,并打印到控制台。
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("D:\\字符流和字节流.txt"));
String a ;
while ((a = br.readLine()) != null){
System.out.println(a);
}
br.close();
}
5、现获取到一些人员的姓名信息,存储在ArrayList集合中,如下:
ArrayList list = new ArrayList<>();
list.add(“Tom”);
list.add(“Jerry”);
list.add(“Lucy”);
list.add(“Lily”);
list.add(“John”);
list.add(“Kevin”);
请编程程序,将这些信息保存至当前模块下的name.txt文件中,每个名字占据一行的位置。
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("name.txt"));
ArrayList<String> list = new ArrayList<>();
list.add("Tom");
list.add("Jerry");
list.add("Lucy");
list.add("Lily");
list.add("John");
list.add("Kevin");
for (String s : list) {
//内容写入
bw.write(s);
//换行
bw.newLine();
//刷新流
bw.flush();
}
//关闭流
bw.close();
}
6、File类,提供了一个length()方法,可以获取文件的大小,以字节为单位。但该方法不能用于获取文件夹的大小,请使用递归编写程序,对于给定的一个路径,比如D:\itcast,统计该文件夹的大小(包含子目录下的所有文件)。
public static void main(String[] args) {
File fi = new File("D:\\itcast");
long fa = fa(fi);
System.out.println(fa);
}
public static long fa(File fi){
long res = 0;
File[] files = fi.listFiles();
for (File file : files) {
if(file.isFile()){
//加上文件的大小
res += file.length();
}
if (file.isDirectory()){
//加上这个文件夹的大小,是文件夹就递归调用本方法
res += fa(file);
}
}
//返回目标文件夹的大小
return res;
}