学习的第32天。
总结(I/O):
字符编码:
UTF-8、GBK
字节流:
字节流的父类(抽象类):InputStream\OutputStream
字符流:
字符流的父类(抽象类):Reader\Writer
字符节点流:
桥转换流:InputStreamReader/OutputStreamWriter,可将字节流转换为字符流。
FileWriter,FileReader
字符过滤流:
BufferedWriter PrintWriter / BufferedReader
习题:
C13.1: 以下关于File类说法正确的是:
A、B、C.
C13.2: 将下列代码补充完整:
class TestMyFile{
public static void main(String[] args) throws Exception{
File file;
//创建一个File 对象表示当前目录下的“hello.txt”文件
file = new File("\\hello.txt");
//判断该文件是否存在
file.exists();
//如果该文件存在,则输出该文件的完整路径
if(file.exists){
file.getAbsolutePath();
}
}
}
C13.10: 想要从某个文件中获得一个字符输出流,则至少有以下三种方式,简述区别:
利用FileWriter类、利用PrintWriter类、利用FileOutputStream类,并通过OutputStreamWriter类获得Writer请简述这三种方式获得的Writer的区别。
FileWriter:获得字符流,编码为系统默认
PrintWriter:获得字符流,编码为系统默认,同时增加写八种基本类型、字符串、对象,以及缓冲区功能
桥转换获取Writer:获得字符流,能够定制编码方式
C13.11: 以下几种文件格式,应当使用:
.class可用字符流、.java可用字符流、.html可用字符流、.jpg和.mp3用字节流。
C13.12: 连线:
ObjectInputStream:读对象、字节流、读八种基本类型、缓冲
BufferOutputStream:缓冲功能、字节流
DataInputStream:字节流、读八种基本类型
PrintWriter:缓冲功能、写字符串并换行、字符流、写八种基本类型、写对象
PrintStream:缓冲功能、写字符串并换行、字节流、写八种基本类型、写对象
BufferedReader:字符流、缓冲、读入一行文本
C13.4: 完成功能:
事先在当前目录下准备好一个test.txt的文本文件,要求该文本文件是使用GBK编码的多行文本文件
class Test14 {
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("test.txt");
OutputStreamWriter osw = new OutputStreamWriter(os,"GBK");
PrintWriter pw = new PrintWriter(osw);
pw.println("窗前明月光");
pw.println("疑是地上霜");
pw.println("举头望明月");
pw.println("低头思故乡");
pw.close();
System.out.println("--创建完成--");
List<String> list= new ArrayList<String>();
InputStream is = new FileInputStream("test.txt");
InputStreamReader isr = new InputStreamReader(is,"GBK");
BufferedReader br = new BufferedReader(isr);
String line;
while((line=br.readLine())!=null){ //======================
list.add(line);
}
br.close();
System.out.println(list);
OutputStream os2 = new FileOutputStream("test2.txt");
Writer w2 = new OutputStreamWriter(os2,"UTF-8");
PrintWriter pw2 = new PrintWriter(w2);
for (int i = list.size()-1; i >= 0; i--) {
pw2.println(list.get(i));
}
pw2.close();
System.out.println("--test2创建完成--");
}
}
C13.17: 在当先目录下创建一个worldcup.txt
格式:
2006/意大利
2002/巴西
要求:在读入该文件的基础上,让用户输入一个年份,输出该年的世界冠军。如果该年没有举办世界杯则输出“没。”
class Test17 {
public static void main(String[] args) {
PrintWriter pw = null;
try {
OutputStream os = new FileOutputStream("worldcup.txt");
OutputStreamWriter osw = new OutputStreamWriter(os);
pw = new PrintWriter(osw);
pw.println("2010/中国");
pw.println("2006/意大利");
pw.println("2002/巴西");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
pw.close();
}
BufferedReader br = null;
List<String> list = new ArrayList<String>();
try {
InputStream is = new FileInputStream("worldcup.txt");
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
list.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {br.close();} catch (IOException e) {}
}
System.out.println(list);
Scanner input = new Scanner(System.in);
System.out.println("输入年份:");
String year = input.next();
if(!(list.contains(year))){
System.out.println("没有举办世界杯");
}
for (String s : list) {
if(s.contains(year)){
System.out.println(s.substring(5));
}
}
}
}
C13.18:
从命令行中读入一个文件名,判断该文件是否存在。
如果存在,则在原文件相同路径下创建一个文件名为“copy_原文件名”的新文件,该文件内容为原文件的拷贝。
class Test18 {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out.println("输入文件名");
String fName = input.next();
File file = new File(fName);
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
byte[] cache = new byte[100 * 4];
fis.read(cache);
FileOutputStream fos = new FileOutputStream(file.getParent()+"\\copy_"+file.getName());
fos.write(cache);
int len = 0;
while ((len = fis.read()) != -1) {
fos.write(len);
}
System.out.println("复制完毕");
}else{
System.out.println("没这个");
}
}