试水应用Java IO

目标1:能实现读写文件的功能

目标1.1 : 能写

采用OutputStream

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class IoOutputstringTest {
    public static void main(String[] args){
        File f1 = new File("C:\\Users\\Administrator\\Documents\\JAVAfile\\test.txt");      //创建文件对象
        try {
            FileOutputStream f2 = new FileOutputStream(f1); 
            f2.write('A');  //调用write方法写入
            f2.close();     //关闭输出流
        } catch (IOException e) {
            // TODO: handle exception
            System.out.println(e.toString());
        }
}
}

打开目录文件“C:\Users\Administrator\Documents\JAVAfile\test.txt”,感动到自己。
这里写图片描述

目标1.2 : 能读

采用InputStream

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class IoInputstringTest {
    public static void main(String[] args){
        int a;
        File f1 = new File("C:\\Users\\Administrator\\Documents\\JAVAfile\\test.txt");  //创建文件对象
        try {
            FileInputStream f2 = new FileInputStream(f1); 
            a =f2.read();               //调用read方法读取数据
            System.out.println(a);
            f2.close();     //关闭输出流
        } catch (IOException e) {
            // TODO: handle exception
            System.out.println(e.toString());
        }
}
}

看Eclipse的控制台,再次感动到。
这里写图片描述

总结一下遇到的问题

  • 进行IO操作时文件路径是要用“\\”隔开
  • 因为IO读写可能发生异常,因此要进行异常处理
  • try{}体中定义的是局部变量
  • API中InputStream和OutputStream只能操作字符(数组)和数字
  • InputStream中的read()方法只能返回一个整型值

目标2 : 操作含有“汉字”的文本文件

因为有了解到InputStream和OutputStream类在JAVA语言中用来处理以位(bit)为单位的流,一般用于处理二进制(binary file),虽然也可以操作文本文件,但是需要将读取的数据再转化成字符耗费一定的资源。更重要的是,字节流不能直接操作Unicode字符,即一旦文件中含有汉字,可能会出现乱码。

目标2.1 : 写“中文”

给自己加了一个小小的难度:处理字符串

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class IoWriterTest {
    public static void main(String[] args) {
        String s = "中国文化,博大,精深";
        String[] strings = s.split(",");   //将字符串s按分隔符“,”分割
        try {
            FileWriter  a1 = new FileWriter("C:\\Users\\Administrator\\Documents\\JAVAfile\\test1.txt");
            BufferedWriter a2 = new BufferedWriter(a1);
            for (String string : strings) {           //遍历字符串数组strings
                a2.write(string);
                a2.newLine();                           //换行
            }
            a2.flush();                                 //将缓冲区中的数据全部写入文件中
            a2.close();
        } catch (IOException e) {
            // TODO: handle exception
            System.out.println(e.toString());
        }
    }
}

感动。
这里写图片描述

目标2.2 : 读“中文”

给自己加了小小的难度:ArrayList的应用

public class IoReaderTest {
    public static void main(String[] args) {
        String thisline;
        ArrayList<String> s = new ArrayList<String>();
        try {
            FileReader fr = new FileReader("C:\\Users\\Administrator\\Documents\\JAVAfile\\test1.txt");
            BufferedReader bfr = new BufferedReader(fr);
            while ((thisline=bfr.readLine()) != null) {
                s.add(thisline);
            }
        } catch (IOException e) {
            // TODO: handle exception
        }

            System.out.println(s);
    }
}

感动。
这里写图片描述

总结一下遇到的问题

  • 因为总是操作字符(数组)会很麻烦,因此我查找更方便的方法。BufferedReader和BufferedWriter类提供了很好用的方法
    • BufferedReader:readline()方法:能直接读取一行
    • BufferedWriter:newLine()方法:能换行

计划IO应更新的

  • 在写入成功时也能在控制台看到提示信息
  • 粗略了解IO包中其他类和好用的其他方法
  • 其他的输入输出方式
  • 非阻塞IO的学习
    • 了解到原来的IO都是阻塞方式的,就是一个线程要等待一次IO命令完成后才可以执行另一条命令。非阻塞IO就是一个线程管理成百上千个socket连接,同时发起成百上千个读写操作,然后在循环中检查,看看谁的数据准备好了,就去操作谁。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值