java 自学讲义 I/O部分

CoreJava看的属实有点累, 沉不下心来看, 找了一个比较简单的网站 HOW2J 感觉还不错

一.文件对象常用的一些方法:

        File f = new File("d:/LOLFolder/LOL.exe");
        System.out.println("当前文件是:" +f);
        //文件是否存在
        System.out.println("判断是否存在:"+f.exists());
         
        //是否是文件夹
        System.out.println("判断是否是文件夹:"+f.isDirectory());
          
        //是否是文件(非文件夹)
        System.out.println("判断是否是文件:"+f.isFile());
          
        //文件长度
        System.out.println("获取文件的长度:"+f.length());
          
        //文件最后修改时间
        long time = f.lastModified();
        Date d = new Date(time);
        System.out.println("获取文件的最后修改时间:"+d);
        //设置文件修改时间为1970.1.1 08:00:00
        f.setLastModified(0);
          
        //文件重命名
        File f2 =new File("d:/LOLFolder/DOTA.exe");
        f.renameTo(f2);
        System.out.println("把LOL.exe改名成了DOTA.exe");
         
        System.out.println("注意: 需要在D:\\LOLFolder确实存在一个LOL.exe,\r\n才可以看到对应的文件长度、修改时间等信息");
        File f = new File("d:/LOLFolder/skin/garen.ski");
  
        // 以字符串数组的形式,返回当前文件夹下的所有文件(不包含子文件及子文件夹)
        f.list();
  
        // 以文件数组的形式,返回当前文件夹下的所有文件(不包含子文件及子文件夹)
        File[]fs= f.listFiles();
  
        // 以字符串形式返回获取所在文件夹
        f.getParent();
  
        // 以文件形式返回获取所在文件夹
        f.getParentFile();
        // 创建文件夹,如果父文件夹skin不存在,创建就无效
        f.mkdir();
  
        // 创建文件夹,如果父文件夹skin不存在,就会创建父文件夹
        f.mkdirs();
  
        // 创建一个空文件,如果父文件夹skin不存在,就会抛出异常
        f.createNewFile();
        // 所以创建一个空文件之前,通常都会创建父目录
        f.getParentFile().mkdirs();
  
        // 列出所有的盘符c: d: e: 等等
        f.listRoots();
  
        // 刪除文件
        f.delete();
  
        // JVM结束的时候,刪除文件,常用于临时文件的删除
        f.deleteOnExit();

二.字节流

用字节流的方式读入文件, 写出文件(要记得关闭流):

    public static void main(String[] args) throws Exception {
        File fin = new File("tttxttt.txt");
        File fout = new File("tt.txt");
        FileInputStream in = new FileInputStream(fin);
        FileOutputStream out = new FileOutputStream(fout);
        byte[] b = new byte[(int)fin.length()];
        in.read(b);
        for(byte i : b){
            System.out.println(i);
        }
        out.write(b);
        out.close();
        in.close();
    }

关于关闭流, 如果采用的try-catch语句, 把流的定义放到try里面, 就不用自己写close, 会在用完之后自动关闭流, 这是从JDK7开始支持的技术try-with-resources, 所有的流都实现了AutoCloseable接口

三.字符流

用字符流的方式读写文件:

        File fin = new File("tttxttt.txt");
        File fout = new File("tt.txt");
        FileReader finreader = new FileReader(fin);
        FileWriter foutwriter = new FileWriter(fout);
        char[] c = new char[(int)fin.length()];
        finreader.read(c);
        foutwriter.write(c);
        for(char i : c){
            System.out.println(i);
        }
        finreader.close();
        foutwriter.close();

四.中文字符问题

用某个特定编码解析字符:

String str = new String(all,"GBK");

流的解析:

InputStreamReader isr = new InputStreamReader(new FileInputStream(f),Charset.forName("UTF-8"))

五.缓存流

如果内容比较多用字符流直接传效率比较低, 可以用缓存流, 每次读/写一行;

        File f = new File("tt.txt");
        FileReader freader = new FileReader(f);
        BufferedReader bufferReader = new BufferedReader(freader);
        while(true){
            String line = bufferReader.readLine();
            if(line == null) break;
            System.out.println(line);
        }

写的时候可以这样:

        File f = new File("tt.txt");
        PrintWriter pw = new PrintWriter(f);
        pw.println("dsffsda");
        pw.flush();
        pw.close();

有的时候,需要立即把数据写入到硬盘,而不是等缓存满了才写出去。 这时候就需要用到flush

六.数据流

用数据流可以指定数据类型来读写

package stream;
     
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
     
public class TestStream {
     
    public static void main(String[] args) {
    	write();
    	read();
    }

	private static void read() {
        File f =new File("d:/lol.txt");
		try (
                FileInputStream fis  = new FileInputStream(f);
                DataInputStream dis =new DataInputStream(fis);
        ){
            boolean b= dis.readBoolean();
            int i = dis.readInt();
            String str = dis.readUTF();
            
            System.out.println("读取到布尔值:"+b);
            System.out.println("读取到整数:"+i);
            System.out.println("读取到字符串:"+str);

        } catch (IOException e) {
            e.printStackTrace();
        }
		
	}

	private static void write() {
        File f =new File("d:/lol.txt");
		try (
                FileOutputStream fos  = new FileOutputStream(f);
                DataOutputStream dos =new DataOutputStream(fos);
        ){
            dos.writeBoolean(true);
            dos.writeInt(300);
            dos.writeUTF("123 this is gareen");
        } catch (IOException e) {
            e.printStackTrace();
        }
		
	}
}

七.对象流

用 OBJECTINPUTSTREAM,OBJECTOUTPUTSTREAM来读写对象
这样在文件中读写的是object流

八.控制台流

这样读入:

import java.util.Scanner;

class Test{
    public static void main(String[] args)throws Exception {
        Scanner cin = new Scanner(System.in);
        int x = cin.nextInt();
        System.out.println(x);
    }
}

或者这样:

int a = System.in.read();
System.out.println(a-'0');

输出的时候自然直接System.out.println就行啦~

附上一张流关系图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值