千锋逆战班第32天

1.(File 类)以下关于 File 类说法正确的是:ABC

A. 一个 File 对象代表了操作系统中的一个文件或者文件夹

B. 可以使用 File 对象创建和删除一个文件

C. 可以使用 File 对象创建和删除一个文件夹

D. 当一个 File 对象被垃圾回收时,系统上对应的文件或文件夹也被删除
2.(File 类)将下列代码补充完整

class TestMyFile{

public static void main(String args[]) 
throws Exception{
File file;

//创建一个 File 对象表示当前目录下的“hello.txt”文件

//判断该文件是否存在

//如果该文件存在,则输出该文件的完整路径

}

}
public class Test2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file;
		file=new File("hello.txt");
		if(file.exists()) {
			System.out.println(file.getAbsolutePath());
		}
	}

}

10.(字符流、桥转换)要想从某个文件中获得一个字符输出流,则至少有以下三种方式:

A.利用 FileWriter 类

B.利用 PrintWriter 类

C.利用 FileOutputStream 类,并通过 OutputStreamWriter 类获得 Writer 请简述这三种方式获得 Writer 的区别。

A FileWriter 用来写入字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是可接受的。要自己指定这些值,可以先在FileOutputStream 上构造一个 OutputStreamWriter。

B PrintWriter 向文本输出流打印对象的格式化表示形式。此类实现在
PrintStream 中的所有 print 方法。它不包含用于写入原始字节的方法,对于这些字节,程序应该使用未编码的字节流进行写入。与 PrintStream 类不同,如果启用了自动刷新,则只有在调用 println、printf 或 format 的其中一个方法时才可能完成此操作,而不是每当正好输出换行符时才完成。这些方法使用平台自有的行分隔符概念,而不是换行符。此类中的方法不会抛出 I/O 异常,尽管其某些构造方法可能抛出异常。客户端可能会查询调用 checkError() 是否出现错误。

C FileOutputStream+FileOutputStreamWriter 文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。文件是否可用或能否可以被创建取决于基础平台。特别是某些平台一次只允许FileOutputStream(或其他文件写入对象)打开文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造方法将失败。FileOutputStream 用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用 FileWriter。
11.(字节流、字符流)以下几种文件格式,应当使用字节流还是字符流?

I…java 源文件 字节流

AI…class 字节码文件 字符流

BI…html 网页文件 字节流

IV. .jpg 图像文件 字节流

V…mp3 音乐文件 字符流
12.(过滤流)连线题。把过滤流和相应的功能用线连起来。注意,左右两边不是一一对应的关系。
ObjectInputStream(E) A字节流
ObjectOutputStream(F) B字符流
BufferInputStream(AG) C读八种基本类型
BufferedOutputStream(AG) D写八种基本类型
DataInputStream(AC) E读对象
DataOutputStream(AD) F写对象
PrintWriter(BJI) G缓冲功能
PrintStream(A) H读入一行文本
BufferedReader(BGH) I写字符串并换行
BufferedWriter(BGj) J写字符串
14.(字符流、桥转换)完成下面功能:

事先在当前目录下准备好一个 test.txt 的文本文件,要求该文本文件是使用 GBK 编码的多行文本文件。如:test.txt

窗前明月光疑是地上霜举头望明月低头思故乡

利用字节流+桥转换读入这个文本文件,按照行的顺序,以 UTF-8 编码方式,写到 test2.txt 文件中。例:test2.txt

低头思故乡举头望明月疑是地上霜窗前明月光

public class Test14 {

	public static void main(String[] args) throws IOException {
		InputStream is = new FileInputStream("Files//test.txt");
		
		InputStreamReader isr = new InputStreamReader(is,"UTF-8");
		
		BufferedReader br = new BufferedReader(isr);
		
		OutputStream os = new FileOutputStream("Files//test2.txt");
		
		OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
		
		PrintWriter pw = new PrintWriter(osw);
		
		String[] str = new String[]{br.readLine(),br.readLine(),br.readLine(),br.readLine()};
		
		for(int i = 0 ; i < 4 ; i++)
			pw.println(str[str.length - 1 - i]);	
			pw.flush();
			pw.close();	
	}
}

17.(字节流,BufferedReader)完成下面操作。

在当前目录下创建一个 worldcup.txt 的文本文件,其格式如下:

2006/意大利

2002/巴西

该文件采用“年份/世界杯冠军”的方式保存每一年世界杯冠军的信息。

要求:读入该文件的基础上,让用户输入一个年份,输出该年的世界杯冠军。如果该年没有举办世界杯,则输出“没有举办世界杯”

public class Test14 {
    static void write(String[] str) throws IOException {
        int year = 2006;
        for (int i = 0; i < str.length; i++) {
            str[i] = str[i] + "/" + year;
            year = year - 4;
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter("worldcup.txt"));
        for (int i = 0; i < str.length; i++) {
            bw.write(str[i]);
            bw.newLine();
        }
        bw.close();
    }
 
    static void read() throws IOException {
        Scanner a = new Scanner(System.in);
        String s = a.next();
        BufferedReader br = new BufferedReader(new FileReader("worldcup.txt"));
        boolean flag = false;
        String str = null;
        String strarr[] = new String[2];
        while ((str = br.readLine()) != null) {
 
            strarr = str.split("/", 2);
            if (strarr[1].equals(s)) {
                flag = true;
                System.out.println(strarr[0]);
            }
 
 
        }
        if (!flag) {
            System.out.println("没有举办过世界杯");
        }
 
        br.close();
 
    }
 
    public static void main(String args[]) throws Exception {
        String str[] = {"意大利", "巴西", "法国", "巴西"};
        write(str);
        read();
 
 
    }
}

18.(综合)

从命令行中读入一个文件名,判断该文件是否存在。如果该文件存在,则在原文件相同路径下创建一个文件名为“copy_原文件名”的新文件,该文件内容为原文件的拷贝。例如:读入 /home/java/photo.jpg

则创建一个文件 /home/java/copy_photo.jpg 新文件内容和原文件内容相同。

import java.io.*;

import java.util.Scanner;

public class TestFile {

	public static void main(String[] args) throws IOException {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个路径:");
		String str = input.next();
		
		File file = new File(str);
		
		InputStream is = new FileInputStream(str);
		
		InputStreamReader isr = new InputStreamReader(is,"UTF-8");
		
		BufferedReader br = new BufferedReader(isr);
		
		OutputStream os = new FileOutputStream(file.getParent() + "//copy_" + file.getName());
		
		OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
		
		PrintWriter pw = new PrintWriter(osw);
		if(file.isFile()){

			File file2 = new File(file.getParent() + "//copy_" + file.getName());
			file2.createNewFile();
			while(true){
				String str3 = br.readLine();
				if(str3 == null)
					break;
				pw.println(str3);
			}
			pw.flush();
			pw.close();
			
		}		
	}
}

发布了23 篇原创文章 · 获赞 0 · 访问量 1936

猜你喜欢

转载自blog.csdn.net/funager/article/details/104932996