1:什么是流?
字符串分解==============OutStream==========>write()方法写到文件中
2:描述I/O流的基本接口和类的结构
InputStream
OutputStream
3:代码示例:如何使用URL流来进行输入输出
try {
imageSource = new URL("http://mysite.com/~info");
} catch (MalformedURLException e) {
}
4:什么是Unicode?
是一种字符的编码方式
5:代码示例:如何使用Reader和Writer来进行输入输出
InputStreamReader ir = new InputStreamReader(System.in);
OutStreamReader or = new OutStreamReader(System.in);
6:什么是可序列化?如何实现可序列化?
表示一个数据可以按流式输出
实现java.io.Serializable接口
7:代码示例:如何读写对象流
//读
try {
String str = "123";
FileOutputStream f = new FileOutputStream("test.txt");
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(str);
f.close();
}catch(Exception e) {
e.printStackTrace();
}
//写
try {
FileInputStream f = new FileInputStream("test.txt");
ObjectInputStream s = new ObjectInputStream(f);
str =(String)s.readObject();
f.close();
}catch(Exception e){
e.printStackTrace();
}
8:简述File类的基本功能
处理文件和获取文件信息,文件或文件夹的管理
除了读写文件内容其他的都可以做
9:代码示例:如何使用随机文件读写类来读写文件内容
RW表示文件时可读写的
读:
try{
RandomAccessFile f = new RandomAccessFile("test.txt", "rw");
long len = 0L;
long allLen = f.length();
int i = 0;
while (len < allLen) {
String s = f.readLine();
if (i > 0) {
col.add(s);
}
i++;
//游标
len = f.getFilePointer();
}
}catch(Exception err){
err.printStackTrace();
}
写:
try{
RandomAccessFile f = new RandomAccessFile("test.txt", "rw");
StringBuffer buffer = new StringBuffer("\n");
Iterator it = col.iterator();
while (it.hasNext()) {
buffer.append(it.next() + "\n");
}
f.writeUTF(buffer.toString());
}catch(Exception err){
err.printStackTrace();
}
10:代码示例:如何使用流的基本接口来读写文件内容
try{
DataInputStream in =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("Test.java")
)
);
while ((currentLine = in.readLine()) != null){
System.out.println(currentLine);
}
}catch (IOException e){
System.err.println("Error: " + e);
}