IO的两个规律:
1,名称定义规律,便于查找具体的对象。
2,IO流的操作规律,四个明确,可以解决使用哪个流对象的问题。
IO体系的组成。
File
InputStream
|
|
|
|
|
|
|
OutputStream
|
|
|
|
|
|
|
Reader
|
|
|
|
|
Writer
|
|
|
|
|
|
重点对象:
File开头的四个。
Buffered开头的四个。
转换流两个。
打印流两个。
File对象。
后面的都是额外功能。
1,IO的基础流对象。直接调用底层资源,操作数据的对象。
File开头的流对象。
2,根据io流的学习规律,后面出现的流对象无非是增加额外功能。
继续提供一些功能。
需求:写一个数据(整数)到文件中。
可以通过将整数转成字符串,变成字节数组写入目的地。
简化方式,之前学习过输出语句发现要输出的内容都原样不变的体现出来。
输出语句对应的对象PrintStream。
对象提供了很多打印的方法,打印方法的好处在于保证数据值的表现形式。
PrintWriter:字符打印流。
-----------
另一个需求:保证数据值字节原样性不变。
例如:写一个整数,源是四个字节,希望是目的也是四个字节。
需要可以操作基本类型数值的对象。
DataOutputStream DataInputStream
用于操作基本类型数据的流对象。
-----------
源和目的都是内存的流的。
字节流:
ByteArrayInputStream ByteArrayOutputStream
字符流
CharArrayReader CharArrayWriter
StringReader StringWriter
原理其实通过流的read,write方法对数组以及字符串进行操作。
关闭这些流都是无效的。因为并未调用系统资源。不需要抛出IOException。
-----------------
需求:对文件进行读或者写的操作,想从哪里读就从哪里读,想从哪里写就从哪里写。
RandomAccessFile
public class PrintStreamDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
PrintStream ps = new PrintStream("tempfile\\int.txt");
ps.print(98);
ps.close();
}
}
public class PrintWriterDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out,true);
String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line)){
break;
}
out.println(line.toUpperCase());
}
out.close();
}
}
public class DataStreamDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
readData();
}
public static void readData() throws IOException {
FileInputStream fis = new FileInputStream("tempfile\\data.txt");
DataInputStream dis = new DataInputStream(fis);
int num = dis.readInt();
System.out.println("num="+num);
dis.close();
}
public static void writeData() throws IOException {
FileOutputStream fos = new FileOutputStream("tempfile\\data.txt");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(97);
dos.close();
}
}
public class ByteArrayStreamDemo {
/**
* @param args
*/
public static void main(String[] args) {
ByteArrayInputStream bis = new ByteArrayInputStream("abcde".getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch = 0;
while((ch=bis.read())!=-1){
bos.write(ch);
}
System.out.println(bos.toString());
}
}
public class RandomAccessFileDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
readFile();
}
public static void readFile() throws IOException {
RandomAccessFile raf = new RandomAccessFile("tempfile\\random.txt", "r");
raf.seek(8*3);
byte[] buf = new byte[4];
raf.read(buf);
String name = new String(buf);
System.out.println("name="+name);
int age = raf.readInt();
System.out.println("age="+age);
raf.close();
}
public static void writeFile() throws IOException {
RandomAccessFile raf = new RandomAccessFile("tempfile\\random.txt", "rw");
raf.seek(4);
raf.write("王武".getBytes());
raf.writeInt(102);
System.out.println(raf.getFilePointer());
raf.close();
}
}