java集合

时间:2014-12-18 00:05:03   收藏:0   阅读:308

java IO

主要内容

File类

以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。

以parent为父路径,child为子路径创建File对象。

常见方法:

bubuko.com,布布扣

eg:

File dir1 = new File("D:/IOTest/dir1");

if (!dir1.exists()) { // 如果D:/IOTest/dir1不存在,就创建为目录

????dir1.mkdir(); }

// 创建以dir1为父目录,名为"dir2"的File对象

File dir2 = new File(dir1, "dir2");

if (!dir2.exists()) { // 如果还不存在,就创建为目录

????dir2.mkdirs(); }

File dir4 = new File(dir1, "dir3/dir4");

if (!dir4.exists()) {

????dir4.mkdirs();

}

// 创建以dir2为父目录,名为"test.txt"的File对象

File file = new File(dir2, "test.txt"); ????

if (!file.exists()) { // 如果还不存在,就创建为文件

????file.createNewFile();}

Java IO原理

bubuko.com,布布扣

bubuko.com,布布扣

流的分类

(抽象基类)

字节流

字符流

输入流

InputStream

Reader

输出流

OutputStream

Writer

  1. Java的IO流共涉及40多个类,实际上非常规则,都是从如上4个抽象基类派生的。
  2. 由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。
  3. 字节流:以byte为单位传输
  4. 字符流:以char为单位传输

IO流体系

bubuko.com,布布扣

InputStream & Reader

OutputStream & Writer

文件流

读取文件

1.建立一个流对象,将已存在的一个文件加载进流。

2.创建一个临时存放数据的数组。

3.调用流对象的读取方法将流中的数据读入到数组中。

FileReader fr = null;

????try{

????????fr = new FileReader("c:\\test.txt");

????????char[] buf = new char[1024];

????????int len= 0;

????????while((len=fr.read(buf))!=-1){

????????????System.out.println(new String(buf ,0,len));}

????}catch (IOException e){

????????System.out.println("read-Exception :"+e.toString());}

????finally{

????????if(fr!=null){

????????????try{

????????????????fr.close();

????????????}catch (IOException e){

????????System.out.println("close-Exception :"+e.toString());

????????????} } }

?

写入文件

1.创建流对象,建立数据存放文件

2.调用流对象的写入方法,将数据写入流

3.关闭流资源,并将流中的数据清空到文件中。

FileWriter fw = null;

????try{

????????fw = new FileWriter("Test.txt");

????????fw.write("text");

????}

????catch (IOException e){

????????System.out.println(e.toString());

????}

????finally{

????????If(fw!=null)

????????try{

???????? fw.close();

????????}

????????catch (IOException e){

????????????System.out.println(e.toString());

}????

}

?

注意点:

?

处理流之一:缓冲流

BufferedReader br = null;

BufferedWriter bw = null;

try {

????//step1:创建缓冲流对象:它是过滤流,是对节点流的包装

????br = new BufferedReader(new FileReader("d:\\IOTest\\source.txt"));

????bw = new BufferedWriter(new FileWriter("d:\\IOTest\\destBF.txt"));

????String str = null;

????while ((str = br.readLine()) != null) { //一次读取字符文本文件的一行字符

????????bw.write(str); //一次写入一行字符串

????????bw.newLine(); //写入行分隔符

????}

????bw.flush(); //step2:刷新缓冲区

} catch (IOException e) {

????e.printStackTrace();

}

finally {

// step3: 关闭IO流对象

try {

????if (bw != null) {

????????bw.close(); //关闭过滤流时,会自动关闭它所包装的底层节点流

????}

} catch (IOException e) {

????e.printStackTrace();

}

try {

????if (br != null) {

????????br.close();

????}

} catch (IOException e) {

????e.printStackTrace();

????}

}

处理流之二:转换流

InputStreamReader

如: Reader isr = new

InputStreamReader(System.in,"ISO5334_1");//指定字符集

OutputStreamWriter

bubuko.com,布布扣

public void testMyInput() throws Exception{

FileInputStream fis = new FileInputStream("dbcp.txt");

FileOutputStream fos = new FileOutputStream("dbcp5.txt");

InputStreamReader isr = new InputStreamReader(fis,"GBK");

OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");

BufferedReader br = new BufferedReader(isr);

BufferedWriter bw = new BufferedWriter(osw);

String str = null;

while((str = br.readLine()) != null){

bw.write(str);

bw.newLine();

bw.flush();

} bw.close(); br.close();}

补充:字符编码

计算机只能识别二进制数据,早期由来是电信号。为了方便应用计算机,让它可以识别各个国家的文字。就将各个国家的文字用数字来表示,并一一对应,形成一张表。这就是编码表。

处理流之三:标准输入输出流

?

System.out.println("请输入信息(退出输入eexit):");

//"标准"输入流(键盘输入)这个字节流包装成字符流,再包装成缓冲流

BufferedReader br = new BufferedReader(

????new InputStreamReader(System.in));

String s = null;

try {

????while ((s = br.readLine()) != null) { //读取用户输入的一行数据 --> 阻塞程序

????????if (s.equalsIgnoreCase("e") || s.equalsIgnoreCase("exit")) {

????????????System.out.println("安全退出!!");

????????????break;

????????}

????????//将读取到的整行字符串转成大写输出

????????System.out.println("-->:"+s.toUpperCase());

????????System.out.println("继续输入信息");

????}????

} catch (IOException e) {

????????e.printStackTrace();

} finally {

????try {

????????if (br != null) {

????????????br.close(); //关闭过滤流时,会自动关闭它包装的底层节点流

????????}????

????} catch (IOException e) {

????????e.printStackTrace();

????}????

}

?

????????

处理流之四:打印流(了解)

?

FileOutputStream fos = null;

????try {

????????fos = new FileOutputStream(new File("D:\\IO\\text.txt"));

????} catch (FileNotFoundException e) {

????????e.printStackTrace();

????}//创建打印输出流,设置为自动刷新模式(写入换行符或字节 ‘\n‘ 时都会刷新输出缓冲区)

????PrintStream ps = new PrintStream(fos,true);

????if (ps != null) {????// 把标准输出流(控制台输出)改成文件

????????System.setOut(ps);}

????for (int i = 0; i <= 255; i++) { //输出ASCII字符

????????System.out.print((char)i);

????????if (i % 50 == 0) { //50个数据一行

????????????System.out.println(); // 换行

????????}

????}

????ps.close();

}

?

处理流之五:数据流(了解)

boolean readBoolean()????????byte readByte()

char readChar()????????????float readFloat()

double readDouble()????????short readShort()

long readLong()????????????int readInt()

String readUTF() void readFully(byte[] b)

?

DataOutputStream dos = null;

????try {????//创建连接到指定文件的数据输出流对象

????????dos = new DataOutputStream(new FileOutputStream(

????????????????????"d:\\IOTest\\destData.dat"));

????????????dos.writeUTF("ab中国"); //写UTF字符串

????????????dos.writeBoolean(false); //写入布尔值

????????????dos.writeLong(1234567890L); //写入长整数

????????????System.out.println("写文件成功!");

????????} catch (IOException e) {

????????????e.printStackTrace();

????????} finally {????//关闭流对象

????????????try {

????????????if (dos != null) {

????????????// 关闭过滤流时,会自动关闭它包装的底层节点流

????????????dos.close();

????????????}

????????} catch (IOException e) {

????????????e.printStackTrace();

????????}????}

处理流之六:对象流

对象的序列化

使用对象流序列化对象

?

序列化:将对象写入到磁盘或者进行网络传输。

要求对象必须实现序列化

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test3.txt"));

Person p = new Person("韩梅梅",18,"中华大街",new Pet());

oos.writeObject(p);

oos.flush();

oos.close();

//反序列化:将磁盘中的对象数据源读出。

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test3.txt"));

Person p1 = (Person)ois.readObject();

System.out.println(p1.toString());

ois.close();

?

RandomAccessFile 类

?

读取文件内容

RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");????

raf.seek(5);

????byte [] b = new byte[1024];

????int off = 0;

????int len = 5;

????raf.read(b, off, len);

????????

????String str = new String(b, 0, len);

????System.out.println(str);

????????

????raf.close();

写入文件内容

?

RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

????raf.seek(5);

????????

????//先读出来

????String temp = raf.readLine();

????????

????raf.seek(5);

????raf.write("xyz".getBytes());

????raf.write(temp.getBytes());

????????

????raf.close();

流的基本应用小节

?

?

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!