1. IO是什么意思? data source是什么意思?
IO流就是以流的方式进行输入输出, 输入输出(IO)是指计算机同任何外部设备之间的数据传递
Data source提供原始数据的原始媒介,常见的有数据库,文件,其他程序,内存,网络连接,IO设备
2. 字节流和字符流有什么区别?输入流和输出流有什么区别?
字节流按照字节读取数据,字符流按照字符读取数据
输入流:数据源到程序 输出流:程序到目的地
3. 节点流和处理流有什么区别?
节点流可以直接从数据源或目的地读写数据,处于io操作的第一线,所有操作必须通过他们进行;
处理流或叫包装流,不直接连接到数据源和目的地,对其他流进行封装,目的主要是简化操作和提高性能。
4. word文档能使用字符流操作吗?为什么?
不能word存放数据是二进制,而字符流只能读取文本
5. 【上机】完成老师课堂上读入文件数据的代码:
static void testReader(){ File f = new File("d:/a.txt"); FileInputStream fis = null; try { fis = new FileInputStream(f); int m = 0; while((m=fis.read())!=-1){ char c = (char) m; System.out.print(c); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(fis!=null){ fis.close(); } } catch (IOException e) { e.printStackTrace(); } } } import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream;
public class Test { public static void main(String[] args) { // 读取一个分件 并输出 // 声明文件输入流 InputStream is = null; try { is = new FileInputStream("d:/bjsxt.txt"); // 使用流 byte[] bs = new byte[1024]; int len = is.read(bs); while (len != -1) { // 输出 System.out.println(new String(bs, 0, len)); // 重新读取 len = is.read(bs); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 关闭流 try { if (is != null) { is.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
6. 解释下面代码的含义:
while((m=fis.read())!=-1){//输入流读取目标源将每个字节赋给m变量,并且判断该字节是否为-1当满足时执行代码块里边代码 char c = (char) m;//将该次读取的字节强转成字符型赋给c变量 System.out.print(c);//输出该字节的字符表示 } |
7. 流对象使用完后,一般要调用close方法关闭,释放资源。 这种做法对吗?
对,流不使用的时候一定要close();因为不仅是堆内存数据,还涉及和外存上文件的连接
8. 【上机】完成老师写文件的操作:
static void testWrite(){ FileOutputStream fos = null; String str = "sffsdfsdfsdfsdfsdfsdfsdf"; try { fos = new FileOutputStream("d:/b.txt"); /*for(int i=0;i<str.length();i++){ fos.write(str.charAt(i)); }*/ fos.write(str.getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(fos!=null){ fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } |
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Test {
public static void main(String[] args) {
// 将字符串写到一个文件
// 声明文件输入流
OutputStream os = null;
try {
String str = "bjsxgwoainihaheihenalasayiersansiwuliuqibajiushi";
os = new FileOutputStream("d:/bjsxt1.txt");
// 使用流
byte[] bs = str.getBytes();
os.write(bs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
9. InputStream和OutputStream基本特点是?
Java语言中最基本的两个字节输入输出类,其他字节输入输出流类都继承这两个基本类,这两个基类是抽象类不能实例只能用他们的子类
10. Reader和Writer的基本特点是?
Java语言中最基本的两个字符输入输出类,其他字符输入输出流类都继承这两个基类,这两个基类是抽象类不能实例,只能用他们的子类
11. FileInputStream和OutputStream的基本作用是?
分别是inputstream和outputstream的子类,fileinputstream是文件字节输入流,他直接连接文件源来获取字节数据然后放入目的地,fileoutputstream是字节输出流负责将字节数据写入目标文件中
12. FileReader和FileWriter的作用是?
分别是reader和writer的子类,filereader是文件字符输入流,他直接连接文件源来获取字符数据然后放入目的地,filewriter是字符输出流负责将字符数据写入目标文件中
13. 【上机】完成文件的copy代码
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Test {
public static void main(String[] args) {
// 完成文件的复制
// 创建字节输入输出流
OutputStream os = null;
InputStream is = null;
try {
is = new FileInputStream("d:/bjsxt.txt");
os = new FileOutputStream("d:/bjsxt1.txt");
// 使用两个流
byte[] bs = new byte[1024];
int len = is.read(bs);
while (len != -1) {
// 写入文件
os.write(bs, 0, len);
// 重新获取
len = is.read(bs);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭两个流
try {
if (is != null) {
is.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
14. BufferInputStream和BufferedOutputStream的特点是?
Bufferedinputstream和bufferedoutputstream是处理流,读文件和写文件都使用了缓冲区,减少读写次数从而提高效率,当创建这两个缓冲流时,会创建内部缓冲数组,读数据时数据按块读入缓冲区,其后的读操作直接访问缓冲区,当写数据时首先写入缓冲区当缓冲区满时其中的数据写入所连接的输出流使用flush方法可以强制使缓冲区的数据全部写入输出流
15. 【上机】使用BufferedReader和BufferedWriter实现文本文件的拷贝。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
public class Test {
public static void main(String[] args) throws IOException {
// 字符输入输出流完成文件的拷贝
// 声明两个流
Reader r = new FileReader("d:/bjsxt.txt");
Writer w = new FileWriter("d:/bjsxt1.txt");
// 使用两个流
char[] cbuf = new char[1024];
int len = r.read(cbuf);
while (len != -1) {
// 写入文件
w.write(cbuf, 0, len);
// 再次读取
len = r.read(cbuf);
}
// 关闭两个流
w.close();
r.close();
}
}